Add Euclidean Distance Formula (#3047)

This commit is contained in:
Raghav Taneja 2022-05-11 14:23:56 -05:00 committed by GitHub
parent 10f41eeee9
commit 239b274069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View 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;
}
}

View File

@ -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);
}
}