EuclideanDistance returns the Euclidean distance between points in any `n` dimensional Euclidean space.
(p1 EuclideanPoint, p2 EuclideanPoint)
| 20 | // EuclideanDistance returns the Euclidean distance between points in |
| 21 | // any `n` dimensional Euclidean space. |
| 22 | func EuclideanDistance(p1 EuclideanPoint, p2 EuclideanPoint) (float64, error) { |
| 23 | n := len(p1) |
| 24 | |
| 25 | if len(p2) != n { |
| 26 | return -1, ErrDimMismatch |
| 27 | } |
| 28 | |
| 29 | var total float64 = 0 |
| 30 | |
| 31 | for i, x_i := range p1 { |
| 32 | // using Abs since the value could be negative but we require the magnitude |
| 33 | diff := math.Abs(x_i - p2[i]) |
| 34 | total += diff * diff |
| 35 | } |
| 36 | |
| 37 | return math.Sqrt(total), nil |
| 38 | } |
no outgoing calls