Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathDistanceFormula.java
More file actions
Latest commit
47 lines (36 loc) · 1.24 KB
/
Copy pathDistanceFormula.java
File metadata and controls
47 lines (36 loc) · 1.24 KB
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
packagecom.thealgorithms.maths;
publicfinalclassDistanceFormula {
privateDistanceFormula() {
}
publicstaticdoubleeuclideanDistance(doublex1, doubley1, doublex2, doubley2) {
doubledX = Math.pow(x2 - x1, 2);
doubledY = Math.pow(y2 - x1, 2);
returnMath.sqrt(dX + dY);
}
publicstaticdoublemanhattanDistance(doublex1, doubley1, doublex2, doubley2) {
returnMath.abs(x1 - x2) + Math.abs(y1 - y2);
}
publicstaticinthammingDistance(int[] b1, int[] b2) {
intd = 0;
if (b1.length != b2.length) {
return -1; // error, both arrays must have the same length
}
for (inti = 0; i < b1.length; i++) {
d += Math.abs(b1[i] - b2[i]);
}
returnd;
}
publicstaticdoubleminkowskiDistance(double[] p1, double[] p2, intp) {
doubled = 0;
doubledistance = 0.0;
if (p1.length != p2.length) {
return -1; // error, both arrays must have the same length
}
for (inti = 0; i < p1.length; i++) {
distance += Math.abs(Math.pow(p1[i] - p2[i], p));
}
distance = Math.pow(distance, (double) 1 / p);
d = distance;
returnd;
}
}