Add Euclidean Distance Formula (#3047)
This commit is contained in:
parent
10f41eeee9
commit
239b274069
11
src/main/java/com/thealgorithms/maths/DistanceFormula.java
Normal file
11
src/main/java/com/thealgorithms/maths/DistanceFormula.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
public class DistanceFormula {
|
||||
public static double distance(double x1, double y1, double x2, double y2)
|
||||
{
|
||||
double dX = Math.pow(x2-x1, 2);
|
||||
double dY = Math.pow(y2-x1, 2);
|
||||
double d = Math.sqrt(dX+dY);
|
||||
return d;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class DistanceFormulaTest
|
||||
{
|
||||
@Test
|
||||
void test1()
|
||||
{
|
||||
Assertions.assertEquals(DistanceFormula.distance(1,1,2,2), 1.4142135623730951);
|
||||
}
|
||||
@Test
|
||||
void test2()
|
||||
{
|
||||
Assertions.assertEquals(DistanceFormula.distance(1,3,8,0), 7.0710678118654755);
|
||||
}
|
||||
@Test
|
||||
void test3()
|
||||
{
|
||||
Assertions.assertEquals(DistanceFormula.distance(2.4,9.1,55.1,100), 110.91911467371168);
|
||||
}
|
||||
@Test
|
||||
void test4()
|
||||
{
|
||||
Assertions.assertEquals(DistanceFormula.distance(1000,13,20000,84), 19022.067605809836);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user