How to calculate the Euclidean distance between two points in TypeScript

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 CalculateEuclideanDistance(x1: number, y1: number, x2: number, y2: number): number {
  // Calculate Euclidean distance between two 2D points
  return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}

const x1: number = 3.0;
const y1: number = 4.0;
const x2: number = 5.0;
const y2: number = 9.0;

const distance: number = CalculateEuclideanDistance(x1, y1, x2, y2);
console.log(`Euclidean Distance: ${distance.toFixed(5)}`);




/*
run:

"Euclidean Distance: 5.38516" 

*/


 



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