在 C++ 中计算两点之间的距离
本文将介绍如何在 C++ 中计算两点之间的距离。
在 C++ 中使用 std::sqrt
和 std::pow
函数计算两点之间的距离
通常,我们可以应用勾股定理来计算两点之间的距离。因此,如果我们知道两个点的 x
和 y
参数,它们之间的距离等于水平和垂直距离总和的平方根,每个距离都增加到 2
的幂。水平和垂直距离本身可以通过围绕连接两点的线构建一个直角三角形来轻松计算,其中后者是斜边。
在这种情况下,我们利用 C++ 中 std
命名空间下的 C 标准数学库函数,即计算给定参数的平方根的 sqrt
函数和计算给定参数的平方根的 pow
函数。力量。请注意,这两个函数都以 float
或 double
类型返回浮点数。
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using std::cout; using std::endl;
using std::vector; using std::copy;
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;
}
输出:
Distance between points (3, 4) and (4, 3) is 1.41421
请注意,我们可以为坐标用浮点数表示的点实现函数的重载变体,如以下示例代码所示。
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using std::cout; using std::endl;
using std::vector; using std::copy;
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;
}
输出:
Distance between points (4, 4.5) and (9, 4.5) is 5
在 C++ 中使用 std::hypot
函数计算两点之间的距离
或者,我们可以使用数学库函数 - std::hypot
,它计算两个或三个数字的平方和的平方根。此方法是计算距离的推荐方法,因为它保证了最终用户难以自行实现的稳健实现和错误报告。请注意,我们需要将对应坐标的差值作为参数传递,并将返回值作为计算结果。
#include <iostream>
#include <vector>
#include <iomanip>
#include <cmath>
using std::cout; using std::endl;
using std::vector; using std::copy;
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;
}
输出:
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