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
示例
#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)
廣告