How to calculate the Euclidean distance between two points in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>

// 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
double calculateEuclideanDistance(double x1, double y1, double x2, double y2) {
   return std::sqrt(std::pow(x2 - x1, 2) + std::pow(y2 - y1, 2));
}
int main() {
   double x1 = 3.0, y1 = 4.0;
   double x2 = 5.0, y2 = 9.0;
   
   double distance = calculateEuclideanDistance(x1, y1, x2, y2);
   
   std::cout << "Euclidean Distance: " << distance << std::endl;
}


/*
run:
  
Euclidean Distance: 5.38516
  
*/

 



answered Oct 11, 2025 by avibootz
...