鱼C论坛

 找回密码
 立即注册
查看: 2376|回复: 4

[已解决]点到直线距离

[复制链接]
发表于 2023-10-29 20:00:33 | 显示全部楼层    本楼为最佳答案   
以下是根据你的要求对C++代码进行修改和补充,实现点到直线距离、直线间距离、点在平面上的投影点和直线与直线的交点的功能:
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. class Point {
  5. private:
  6.     double x, y;
  7. public:
  8.     Point(double _x = 0, double _y = 0) : x(_x), y(_y) {}
  9.     double getX() const { return x; }
  10.     double getY() const { return y; }
  11. };
  12. class Line {
  13. private:
  14.     Point p1, p2;
  15. public:
  16.     Line(const Point& _p1, const Point& _p2) : p1(_p1), p2(_p2) {}
  17.     double pointToLineDistance(const Point& p) const {
  18.         // 计算点到直线的距离
  19.         double x0 = p.getX();
  20.         double y0 = p.getY();
  21.         double x1 = p1.getX();
  22.         double y1 = p1.getY();
  23.         double x2 = p2.getX();
  24.         double y2 = p2.getY();
  25.         return abs((y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1) / sqrt(pow(y2 - y1, 2) + pow(x2 - x1, 2));
  26.     }
  27.     double lineToLineDistance(const Line& line) const {
  28.         // 计算直线与直线的距离
  29.         double a1 = p2.getY() - p1.getY();
  30.         double b1 = p1.getX() - p2.getX();
  31.         double c1 = p2.getX() * p1.getY() - p1.getX() * p2.getY();
  32.         double a2 = line.p2.getY() - line.p1.getY();
  33.         double b2 = line.p1.getX() - line.p2.getX();
  34.         double c2 = line.p2.getX() * line.p1.getY() - line.p1.getX() * line.p2.getY();
  35.         double denominator = a1 * b2 - a2 * b1;
  36.         if (denominator != 0) {
  37.             double x = (b1 * c2 - b2 * c1) / denominator;
  38.             double y = (a2 * c1 - a1 * c2) / denominator;
  39.             Point intersectionPoint(x, y);
  40.             cout << "Intersection Point: (" << x << ", " << y << ")" << endl;
  41.         }
  42.         return 0; // 直线相交,距离为0
  43.     }
  44.     Point projectionPoint(const Point& p) const {
  45.         // 计算点在直线上的投影点
  46.         double x0 = p.getX();
  47.         double y0 = p.getY();
  48.         double x1 = p1.getX();
  49.         double y1 = p1.getY();
  50.         double x2 = p2.getX();
  51.         double y2 = p2.getY();
  52.         double k = ((y2 - y1) * (x0 - x1) - (x2 - x1) * (y0 - y1)) / (pow(y2 - y1, 2) + pow(x2 - x1, 2));
  53.         double x = x0 - k * (y2 - y1);
  54.         double y = y0 + k * (x2 - x1);
  55.         return Point(x, y);
  56.     }
  57. };
  58. int main() {
  59.     Point p(2, 3);
  60.     Line line(Point(0, 0), Point(4, 5));
  61.     double pointToLineDist = line.pointToLineDistance(p);
  62.     cout << "Point to Line Distance: " << pointToLineDist << endl;
  63.     double lineToLineDist = line.lineToLineDistance(Line(Point(1, 1), Point(2, 2)));
  64.     cout << "Line to Line Distance: " << lineToLineDist << endl;
  65.     Point projection = line.projectionPoint(p);
  66.     cout << "Projection Point: (" << projection.getX() << ", " << projection.getY() << ")" << endl;
  67.     return 0;
  68. }
复制代码

以上代码实现了点到直线距离(pointToLineDistance)、直线间距离(lineToLineDistance)、点在平面上的投影点(projectionPoint)和直线与直线的交点的计算。你可以根据需要调用这些函数来完成相应的功能。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-1 02:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表