How to calculate the Euclidean distance between two points in Scala

1 Answer

0 votes
// The Euclidean distance is a measure of the straight-line distance 
// between two points in a 2D or 3D space

object EuclideanDistance {

  // CalculateEuclideanDistance computes the Euclidean distance between two 2D points
  def calculateEuclideanDistance(x1: Double, y1: Double, x2: Double, y2: Double): Double = {
    math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2))
  }

  def main(args: Array[String]): Unit = {
    val x1 = 3.0
    val y1 = 4.0
    val x2 = 5.0
    val y2 = 9.0

    val distance = calculateEuclideanDistance(x1, y1, x2, y2)
    println(f"Euclidean Distance: $distance%.5f")
  }
}


/*
run:

Euclidean Distance: 5.38516

*/

 



answered Oct 13 by avibootz
...