How to calculate the Euclidean distance between two points in PHP

1 Answer

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

// Function to calculate Euclidean distance
function CalculateEuclideanDistance($x1, $y1, $x2, $y2) {
    return sqrt(pow($x2 - $x1, 2) + pow($y2 - $y1, 2));
}

$x1 = 3.0;
$y1 = 4.0;
$x2 = 5.0;
$y2 = 9.0;

$distance = CalculateEuclideanDistance($x1, $y1, $x2, $y2);

echo "Euclidean Distance: " . number_format($distance, 5) . "\n";



/*
run:

Euclidean Distance: 5.38516

*/

 



answered Oct 12, 2025 by avibootz
edited Oct 13, 2025 by avibootz
...