Distance Formula in C++

Description: Calculates the distance between two sets of points on a Cartesian Plane grid system. Great for 2D games. Requires you to include <cmath>
Tested Platform: Visual Studio 2010, Windows 7
Language: C/C++
// Takes two sets of points and calculates the distance between them
// on a Cartesian coordinate system. 

double distance(int x1, int y1, int x2, int y2) {
     double xPoints = pow((x2 - x1), 2.0);
     double yPoints = pow((y2 - y1), 2.0);

     return sqrt(xPoints + yPoints);
}

Submitted: January 29, 2013

Return to the snippets listing