C++ 两条直线交点程序


给定对应于直线 AB 的点 A 和 B,以及对应于直线 PQ 的点 P 和 Q;任务是找到这两条直线的交点。

注意 - 点在 X 和 Y 坐标的二维平面上给出。

这里 A(a1, a2)、B(b1, b2) 和 C(c1, c2)、D(d1, d2) 是形成两条不同直线的坐标,P(p1, p2) 是交点。(仅用于交点的示意图解释)

如何找到交点 -

让我们将上图视为 -

示例

So using the (a1, a2), (b1, b2), (c1, c2), (d1, d2)
We will calculate :
A1 = b2 - a2
B1 = a1 - b1
C1 = (A1 * a1) + (B1 * a1)
A2 = d2 - c2
B2 = c1 - d1
C2 = (A2 * c1) + (B2 * c2)
Let the given lines be:
1. A1x + B1y = C1
2. A2x + B2y = C2
Now, to find the point of intersection, we have to solve these 2 equations. We will multiply 1 by B1 and 2 by B2, so we will get:
A1B2x +B1B2y = C1B2
A1B1x +B2B1y = C1B1

Subtracting these we get,
(A1B2 - A2B1)x = C1B2-C2B1

这给了我们 x 的值,类似地,我们将得到 y 的值,它将是交点 p1(即 x)和 p2(即 y)。

注意 - 上述公式将给出两条直线的交点,但如果给出线段而不是直线,则必须重新检查该点,因此计算出的结果必须位于线段上。

  • min (x1, x2) <= x <= max (x1, x2)
  • min (y1, y2) <= y <= max (y1, y2)

我们用来解决上述问题的方法 -

  • 获取输入值。
  • 找到行列式,即 a1 * b2 - a2 * b1
  • 检查行列式是否 = 0,如果是则直线平行
  • 如果行列式不为零,则 x = (c1 * b2 - c2 * b1) 且 y = (a1 * c2 - a2 * c1)
  • 返回并打印结果。

算法

Start
Step 1-> Declare function to print the x and y coordinates
   void display(mk_pair par)
   Print par.first and par.second
Step 2-> declare function to calculate the intersection point
   mk_pair intersection(mk_pair A, mk_pair B, mk_pair C, mk_pair D)
   Declare double a = B.second - A.second
   Declare double b = A.first - B.first
   Declare double c = a*(A.first) + b*(A.second)
   Declare double a1 = D.second - C.second
   Declare double b1 = C.first - D.first
   Declare double c1 = a1*(C.first)+ b1*(C.second)
   Declare double det = a*b1 - a1*b
   IF (det = 0)
      return make_pair(FLT_MAX, FLT_MAX)
   End
   Else
      Declare double x = (b1*c - b*c1)/det
      Declare double y = (a*c1 - a1*c)/det
      return make_pair(x, y)
   End
Step 3-> In main()
   Declare and call function for points as mk_pair q = make_pair(2, 1)
   IF (inter.first = FLT_MAX AND inter.second = FLT_MAX)
      Print “given lines are parallel“
   End
   Else
      Call display(inter)
   End
Stop

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例

 实时演示

#include <bits/stdc++.h>
using namespace std;
#define mk_pair pair<double, double>
//display the x and y coordinates
void display(mk_pair par) {
   cout << "(" << par.first << ", " << par.second << ")" << endl;
}
mk_pair intersection(mk_pair A, mk_pair B, mk_pair C, mk_pair D) {
   // Line AB represented as a1x + b1y = c1
   double a = B.second - A.second;
   double b = A.first - B.first;
   double c = a*(A.first) + b*(A.second);
   // Line CD represented as a2x + b2y = c2
   double a1 = D.second - C.second;
   double b1 = C.first - D.first;
   double c1 = a1*(C.first)+ b1*(C.second);
   double det = a*b1 - a1*b;
   if (det == 0) {
      return make_pair(FLT_MAX, FLT_MAX);
   } else {
      double x = (b1*c - b*c1)/det;
      double y = (a*c1 - a1*c)/det;
      return make_pair(x, y);
   }
}
int main() {
   mk_pair q = make_pair(2, 1);
   mk_pair r = make_pair(2, 7);
   mk_pair s = make_pair(4, 4);
   mk_pair t = make_pair(6, 4);
   mk_pair inter = intersection(q, r, s, t);
   if (inter.first == FLT_MAX && inter.second==FLT_MAX) {
      cout << "The given lines AB and CD are parallel.\n";
   } else {
      cout << "The intersection of the given lines AB and CD is: ";
      display(inter);
   }
   return 0;
}

输出

The intersection of the given lines AB and CD is: (2, 4)

更新于: 2019-12-23

4K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告