Skip to content

Commit 239b274

Browse files
authored
Add Euclidean Distance Formula (TheAlgorithms#3047)
1 parent 10f41ee commit 239b274

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.thealgorithms.maths;
2+
3+
public class DistanceFormula {
4+
public static double distance(double x1, double y1, double x2, double y2)
5+
{
6+
double dX = Math.pow(x2-x1, 2);
7+
double dY = Math.pow(y2-x1, 2);
8+
double d = Math.sqrt(dX+dY);
9+
return d;
10+
}
11+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class DistanceFormulaTest
7+
{
8+
@Test
9+
void test1()
10+
{
11+
Assertions.assertEquals(DistanceFormula.distance(1,1,2,2), 1.4142135623730951);
12+
}
13+
@Test
14+
void test2()
15+
{
16+
Assertions.assertEquals(DistanceFormula.distance(1,3,8,0), 7.0710678118654755);
17+
}
18+
@Test
19+
void test3()
20+
{
21+
Assertions.assertEquals(DistanceFormula.distance(2.4,9.1,55.1,100), 110.91911467371168);
22+
}
23+
@Test
24+
void test4()
25+
{
26+
Assertions.assertEquals(DistanceFormula.distance(1000,13,20000,84), 19022.067605809836);
27+
}
28+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy