From 239b27406997029745e08cd338de7956b9724a06 Mon Sep 17 00:00:00 2001 From: Raghav Taneja <97575679+RaghavTaneja@users.noreply.github.com> Date: Wed, 11 May 2022 14:23:56 -0500 Subject: [PATCH] Add Euclidean Distance Formula (#3047) --- .../thealgorithms/maths/DistanceFormula.java | 11 ++++++++ .../maths/DistanceFormulaTest.java | 28 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/DistanceFormula.java create mode 100644 src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java diff --git a/src/main/java/com/thealgorithms/maths/DistanceFormula.java b/src/main/java/com/thealgorithms/maths/DistanceFormula.java new file mode 100644 index 00000000..cc6ebc9e --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/DistanceFormula.java @@ -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; + } +} diff --git a/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java b/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java new file mode 100644 index 00000000..e54f1263 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/DistanceFormulaTest.java @@ -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); + } +}