迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中计算两点之间的距离

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文将介绍如何在 C++ 中计算两点之间的距离。


在 C++ 中使用 std::sqrtstd::pow 函数计算两点之间的距离

通常,我们可以应用勾股定理来计算两点之间的距离。因此,如果我们知道两个点的 xy 参数,它们之间的距离等于水平和垂直距离总和的平方根,每个距离都增加到 2 的幂。水平和垂直距离本身可以通过围绕连接两点的线构建一个直角三角形来轻松计算,其中后者是斜边。

在这种情况下,我们利用 C++ 中 std 命名空间下的 C 标准数学库函数,即计算给定参数的平方根的 sqrt 函数和计算给定参数的平方根的 pow 函数。力量。请注意,这两个函数都以 floatdouble 类型返回浮点数。

#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;
}

输出:

Distance between points (3, 4) and (4, 3) is 1.41421

请注意,我们可以为坐标用浮点数表示的点实现函数的重载变体,如以下示例代码所示。

#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;
}

输出:

Distance between points (4, 4.5) and (9, 4.5) is 5

在 C++ 中使用 std::hypot 函数计算两点之间的距离

或者,我们可以使用数学库函数 - std::hypot,它计算两个或三个数字的平方和的平方根。此方法是计算距离的推荐方法,因为它保证了最终用户难以自行实现的稳健实现和错误报告。请注意,我们需要将对应坐标的差值作为参数传递,并将返回值作为计算结果。

#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;
}

输出:

Distance between points (4, 4.5) and (9, 4.5) is 5

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便