鱼C论坛

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

[已解决]点到直线距离

[复制链接]
发表于 2023-10-29 19:59:50 | 显示全部楼层
首先,我们需要定义 Point 类来表示二维平面上的一个点。Point 类可以有两个成员变量 x 和 y,分别表示点的横坐标和纵坐标。我们可以为 Point 类添加一个构造函数来初始化点的坐标:

  1. class Point {
  2. public:
  3.     double x;
  4.     double y;

  5.     Point(double xCoord, double yCoord) {
  6.         x = xCoord;
  7.         y = yCoord;
  8.     }
  9. };
复制代码


接下来,我们需要从 Point 类派生出 Line 类,用两个点来确定一条直线。Line 类可以有两个成员变量 p1 和 p2,分别表示直线上的两个点。我们也可以为 Line 类添加一个构造函数来初始化直线的两个点:

  1. class Line {
  2. public:
  3.     Point p1;
  4.     Point p2;

  5.     Line(Point point1, Point point2) {
  6.         p1 = point1;
  7.         p2 = point2;
  8.     }
  9. };
复制代码


现在我们可以实现 Line 类的成员函数来完成题目要求的功能:

1. 点到直线距离:我们可以使用点到直线的距离公式来计算点到直线的距离。假设点的坐标为 (x0, y0),直线上两个点的坐标分别为 (x1, y1) 和 (x2, y2),则点到直线的距离公式为:

   ![点到直线距离公式](https://i.stack.imgur.com/7RJ6q.png)

   我们可以在 Line 类中添加一个成员函数来计算点到直线的距离:

  
  1.    double distanceToPoint(Point point) {
  2.        double numerator = abs((p2.y - p1.y) * point.x - (p2.x - p1.x) * point.y + p2.x * p1.y - p2.y * p1.x);
  3.        double denominator = sqrt(pow(p2.y - p1.y, 2) + pow(p2.x - p1.x, 2));
  4.        return numerator / denominator;
  5.    }
  6.    
复制代码


2. 直线间距离:直线间的距离可以通过计算两条直线的最短距离来得到。如果两条直线相交,则它们的距离为 0。我们可以在 Line 类中添加一个静态成员函数来计算直线间的距离:

  
  1.    static double distanceBetweenLines(Line line1, Line line2) {
  2.        // 计算两条直线的最短距离
  3.        // 如果两条直线相交,距离为 0
  4.        // 返回距离值
  5.    }
  6.    
复制代码


   在这个函数中,你可以使用数学公式来计算两条直线的最短距离。

3. 点在平面上的投影点:点在平面上的投影点可以通过计算点到直线的垂直距离来得到。我们可以在 Line 类中添加一个成员函数来计算点在直线上的投影点:

  
  1.    Point projectionOfPoint(Point point) {
  2.        double t = ((point.x - p1.x) * (p2.x - p1.x) + (point.y - p1.y) * (p2.y - p1.y)) / (pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
  3.        double x = p1.x + t * (p2.x - p1.x);
  4.        double y = p1.y + t * (p2.y - p1.y);
  5.        return Point(x, y);
  6.    }
  7.    
复制代码


4. 直线与直线的交点:两条直线的交点可以通过联立两条直线的方程来求解。我们可以在 Line 类中添加一个静态成员函数来计算两条直线的交点:

  
  1.    static Point intersectionOfLines(Line line1, Line line2) {
  2.        // 联立两条直线的方程,求解交点的坐标
  3.        // 返回交点的坐标
  4.    }
  5.    
复制代码


   在这个函数中,你可以使用数学公式来求解两条直线的交点。

最后,我们可以在主函数中创建 Point 和 Line 对象,并调用相应的成员函数来测试实现的功能:

  1. int main() {
  2.     Point point1(1.0, 2.0);
  3.     Point point2(3.0, 4.0);
  4.     Line line(point1, point2);

  5.     Point point3(5.0, 6.0);
  6.     double distance = line.distanceToPoint(point3);
  7.     Point projection = line.projectionOfPoint(point3);

  8.     // 输出点到直线的距离和投影点的坐标
  9.     // ...

  10.     // 创建另一条直线
  11.     Line line2(point4, point5);

  12.     double distanceBetweenLines = Line::distanceBetweenLines(line, line2);
  13.     Point intersection = Line::intersectionOfLines(line, line2);

  14.     // 输出直线间的距离和交点的坐标
  15.     // ...

  16.     return 0;
  17. }
复制代码


以上是一个可能的实现,你可以根据自己的需求进行调整和优化。希望能帮到你!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-30 06:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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