-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.js
executable file
·98 lines (94 loc) · 3.95 KB
/
distance.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
********************************************************************************************
* name: Distance
********************************************************************************************
* desc: module is responsible for distance metric functionc
********************************************************************************************
* code: written by Gil Tamari, you may not use it without my permission
* date: sep-2012
********************************************************************************************
**/
var Distance = function(){}
Distance.prototype = {
/************************************************************************
}
* name: euclidean
* desc: calculating euclid metric between 2 vectors, using Pythagorean formula
*************************************************************************
* in: v1, v2 - Array[Double], each is an n-vector
*************************************************************************
* out: distance - Double
*************************************************************************
*/
euclidean: function(v1, v2) {
var total = 0,
i
for (i = 0; i < v1.length; i++) {
total += Math.pow(v2[i] - v1[i], 2)
}
return Math.sqrt(total)
},
/************************************************************************
}
* name: manhattan distance
* desc: calculating manhattan distance between 2 vectors, which is the sum of abs delts
*************************************************************************
* in: v1, v2 - Array[Double], each is an n-vector
*************************************************************************
* out: distance - Double
*************************************************************************
*/
manhattan: function(v1, v2) {
var total = 0,
i
for (i = 0; i < v1.length; i++) {
total += Math.abs(v2[i] - v1[i])
}
return total
},
/************************************************************************
}
* name: haversine
* desc: Using Haversine distance , the earth, being a "punched" ellipsoid
* and not flat, needs a different distance measure. Haversine is an approximation
* to a sphere. Google maps uses mercator projection, so it fitts.
* we use trigo to compensate for the curvature of the earth
* notice we're using 2 dimensional vectors, no n-vectors here
*************************************************************************
* in: v1, v2 - Array[Double], each is an 2-vector
*************************************************************************
* out: distance - Double
*************************************************************************
*/
haversine: function(point_a,point_b){
var R = 6367.5, // the earth's radius
dLat = (point_a[0] - point_b[0]) * Math.PI/180,
dLong = (point_a[1] - point_b[1]) * Math.PI/180,
lat1 = point_a[0] * Math.PI/180,
lat2 = point_b[0] * Math.PI/180,
a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLong/2) * Math.sin(dLong/2) * Math.cos(lat1) * Math.cos(lat2),
c = 2 * Math.atan2(Math.sqrt(a),Math.sqrt(1-a)),
d = R*c;
return d;
},
/************************************************************************
}
* name: max
* desc: returns the biggest manhattan delta between 2 vectors
*************************************************************************
* in: v1, v2 - Array[Double], each is an n-vector
*************************************************************************
* out: distance - Double
*************************************************************************
*/
max: function(v1, v2) {
var max = 0,
i
for (i = 0; i < v1.length; i++) {
max = Math.max(max , Math.abs(v2[i] - v1[i]))
}
return max
}
}
if (module && module.exports)
module.exports = Distance