How to Calculate Distance Between Two Points in C++
-
Use
std::sqrt
andstd::pow
Functions to Calculate Distance Between Two Points in C++ -
Use the
std::hypot
Function to Calculate Distance Between Two Points in C++
This article will introduce how to calculate distance between two points in C++.
Use std::sqrt
and std::pow
Functions to Calculate Distance Between Two Points in C++
Generally, we can calculate the distance between two points by applying the Pythagorean theorem. So, if we know x
and y
parameters of two points, the distance between them equals the square root from the sum of horizontal and vertical distances, each raised to the power of 2
. Horizontal and vertical distances themselves can be easily calculated by constructing a right-angled triangle around the line connecting the two points, where the latter is the hypotenuse.
In this case, we utilize C standard math library functions available in C++ under the std
namespace, namely, the sqrt
function that computes the square root of the given argument and pow
, which calculates the number raised to the given power. Note that both of these functions return floating-point numbers as float
or double
types.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using std::copy;
using std::cout;
using std::endl;
using std::vector;
double calculateDistance(std::pair<int, int> &x, std::pair<int, int> &y) {
return sqrt(pow(x.first - y.first, 2) + pow(x.second - y.second, 2));
}
double calculateDistance(std::pair<double, double> &x,
std::pair<double, double> &y) {
return sqrt(pow(x.first - y.first, 2) + pow(x.second - y.second, 2));
}
int main() {
vector<std::pair<int, int>> vec = {
{3, 4},
{4, 3},
};
cout << "Distance between points (" << vec[0].first << ", " << vec[0].second
<< ") and (" << vec[1].first << ", " << vec[1].second << ") is "
<< calculateDistance(vec[0], vec[1]) << endl;
return EXIT_SUCCESS;
}
Output:
Distance between points (3, 4) and (4, 3) is 1.41421
Notice that we can implement an overloaded variant of the function for points with coordinates represented with floating-point numbers, as demonstrated in the following example code.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using std::copy;
using std::cout;
using std::endl;
using std::vector;
double calculateDistance(std::pair<int, int> &x, std::pair<int, int> &y) {
return sqrt(pow(x.first - y.first, 2) + pow(x.second - y.second, 2));
}
double calculateDistance(std::pair<double, double> &x,
std::pair<double, double> &y) {
return sqrt(pow(x.first - y.first, 2) + pow(x.second - y.second, 2));
}
int main() {
vector<std::pair<double, double>> vec2 = {
{4.0, 4.5},
{9.0, 4.5},
};
cout << "Distance between points (" << vec2[0].first << ", " << vec2[0].second
<< ") and (" << vec2[1].first << ", " << vec2[1].second << ") is "
<< calculateDistance(vec2[0], vec2[1]) << endl;
return EXIT_SUCCESS;
}
Output:
Distance between points (4, 4.5) and (9, 4.5) is 5
Use the std::hypot
Function to Calculate Distance Between Two Points in C++
Alternatively, we can use a math library function - std::hypot
which calculates the square root of the sum of the squares of two or three numbers. This method is recommended way to calculated the distance as it guarantees the robust implementation and error reporting that would be harder for the end-user to implement on her own. Notice that we need to pass the difference of corresponding coordinates as arguments and use the return value as the calculation result.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using std::copy;
using std::cout;
using std::endl;
using std::vector;
int main() {
vector<std::pair<double, double>> vec2 = {
{4.0, 4.5},
{9.0, 4.5},
};
cout << "Distance between points (" << vec2[0].first << ", " << vec2[0].second
<< ") and (" << vec2[1].first << ", " << vec2[1].second << ") is "
<< std::hypot(vec2[0].first - vec2[1].first,
vec2[0].second - vec2[1].second)
<< endl;
return EXIT_SUCCESS;
}
Output:
Distance between points (4, 4.5) and (9, 4.5) is 5
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook