-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistance.go
39 lines (34 loc) · 928 Bytes
/
distance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* gouda/point: an n-dimensional point & tools
*
* Copyright (C) 2018 Pawel Foremski <[email protected]>
* Licensed to you under GNU GPL v3
*/
package point
import "math"
// Euclidean() returns Euclidean distance between a and b
func Euclidean(a, b *Point) float64 {
dist := 0.0
for axis := 0; axis < len(a.V); axis++ {
dist += math.Pow(a.V[axis] - b.V[axis], 2.0)
}
return math.Sqrt(dist)
}
// Maxdiff() returns the maximum difference between a and b on any axis
func Maxdiff(a, b *Point) float64 {
dist := 0.0
for axis := 0; axis < len(a.V); axis++ {
diff := math.Abs(a.V[axis] - b.V[axis])
if diff > dist { dist = diff }
}
return dist
}
// Taxicab() returns the taxicab distance between a and b;
// see https://en.wikipedia.org/wiki/Taxicab_geometry
func Taxicab(a, b *Point) float64 {
dist := 0.0
for axis := 0; axis < len(a.V); axis++ {
dist += math.Abs(a.V[axis] - b.V[axis])
}
return dist
}