Simply put, Euclidean distance is the shortest distance between two points. The kind of distance you would measure with a ruler. e.g. Calculate distance C in the diagram below.

## Euclidean distance formula

Euclidean distance is calculated using the following formula:

For example, the distance between points P (1,2) and Q (3,4) is:

√ (P _{1} – Q _{1} ) ^{2} + (P _{2} – Q _{2} ) ^{2} |

= √ (1-3) ^{2} + (2-4) ^{2} |

= √ (-2) ^{2} + (-2) ^{2} |

= √ 4 + 4 ≈ 2.828 |

To calculate the distance in multiple dimensions, simply sum the square of each extra point. For example, The distance between points P (1,2,3) and Q (4,5,6) is:

√ (P _{1} – Q _{1} ) ^{2} + (P _{2} – Q _{2} ) ^{2} + (P _{3} – Q _{3} ) ^{2} |

= √ (1-4) ^{2} + (2-5) ^{2} + (3-6) ^{2} |

= √ (-3) ^{2} + (-3) ^{2} (-2) ^{2} + (-3) ^{2} |

= √ 9 + 9 + 9 |

= √ 27 ≈ 5.196 |

## Ruby implementation

To recap, we want to implement the following formula for Euclidean distance in Ruby.

The main point to note is that the function should take two arrays, each with an arbitrary length. Each element of the array represents a point in a multidimensional space. For the sake of clarity, this example excludes error handling (e.g. arrays of differing lengths ).

## References

- Wikipedia:Euclidean distance
- Programming Collective Intelligence by Toby Segaran

## Futher reading

You can also use Euclidean distance to:

- calculate the similarity between two documents
- or calculate the proximity of two locations

I’m working through the examples in the Collective Intelligence book by Segaran. You can find them at my Collective Intelligence github repository