给定与AB线对应的A点和B点,以及与PQ线对应的P点和Q点,求出这些线的交点。这些点在二维平面上以其X和Y坐标给出。
例如:
Input : A = (1, 1), B = (4, 4) C = (1, 8), D = (2, 4) Output : The intersection of the given lines AB and CD is: (2.4, 2.4) Input : A = (0, 1), B = (0, 4) C = (1, 8), D = (1, 4) Output : The given lines AB and CD are parallel.
首先,假设我们有两点(x 1. Y 1. )和(x) 2. Y 2. )现在,我们找到由这些点构成的直线方程。
让给定的行为:
- A. 1. x+b 1. y=c 1.
- A. 2. x+b 2. y=c 2.
我们现在必须解这两个方程来找到交点。要解决这个问题,我们乘以1。b 2. b写的2 1. 这给了我们, A. 1. B 2. x+b 1. B 2. y=c 1. B 2. A. 2. B 1. x+b 2. B 1. y=c 2. B 1.
减去这些, (a) 1. B 2. –a 2. B 1. )x=c 1. B 2. –c 2. B 1.
这给了我们x的值。同样,我们可以找到y的值。(x,y)给了我们交点。
注: 这给出了两条直线的交点,但如果我们得到的是线段而不是直线,我们还必须重新检查这样计算的点是否实际位于两条线段上。 如果线段由点(x)指定 1. Y 1. )和(x) 2. Y 2. ),然后要检查(x,y)是否在线段上,我们只需要检查一下
- 最小(x) 1. 十、 2. )<=x<=max(x 1. 十、 2. )
- 明(y) 1. Y 2. )<=y<=最大值(y 1. Y 2. )
上述实现的伪代码:
determinant = a1 b2 - a2 b1 if (determinant == 0) { // Lines are parallel } else { x = (c1b2 - c2b1)/determinant y = (a1c2 - a2c1)/determinant }
首先直接求出斜率,然后求出直线的截距,就可以得到它们。
C++
// C++ Implementation. To find the point of // intersection of two lines #include <bits/stdc++.h> using namespace std; // This pair is used to store the X and Y // coordinates of a point respectively #define pdd pair<double, double> // Function used to display X and Y coordinates // of a point void displayPoint(pdd P) { cout << "(" << P.first << ", " << P.second << ")" << endl; } pdd lineLineIntersection(pdd A, pdd B, pdd C, pdd D) { // Line AB represented as a1x + b1y = c1 double a1 = B.second - A.second; double b1 = A.first - B.first; double c1 = a1*(A.first) + b1*(A.second); // Line CD represented as a2x + b2y = c2 double a2 = D.second - C.second; double b2 = C.first - D.first; double c2 = a2*(C.first)+ b2*(C.second); double determinant = a1*b2 - a2*b1; if (determinant == 0) { // The lines are parallel. This is simplified // by returning a pair of FLT_MAX return make_pair(FLT_MAX, FLT_MAX); } else { double x = (b2*c1 - b1*c2)/determinant; double y = (a1*c2 - a2*c1)/determinant; return make_pair(x, y); } } // Driver code int main() { pdd A = make_pair(1, 1); pdd B = make_pair(4, 4); pdd C = make_pair(1, 8); pdd D = make_pair(2, 4); pdd intersection = lineLineIntersection(A, B, C, D); if (intersection.first == FLT_MAX && intersection.second==FLT_MAX) { cout << "The given lines AB and CD are parallel." ; } else { // NOTE: Further check can be applied in case // of line segments. Here, we have considered AB // and CD as lines cout << "The intersection of the given lines AB " "and CD is: " ; displayPoint(intersection); } return 0; } |
JAVA
// Java Implementation. To find the point of // intersection of two lines // Class used to used to store the X and Y // coordinates of a point respectively class Point { double x,y; public Point( double x, double y) { this .x = x; this .y = y; } // Method used to display X and Y coordinates // of a point static void displayPoint(Point p) { System.out.println( "(" + p.x + ", " + p.y + ")" ); } } class Test { static Point lineLineIntersection(Point A, Point B, Point C, Point D) { // Line AB represented as a1x + b1y = c1 double a1 = B.y - A.y; double b1 = A.x - B.x; double c1 = a1*(A.x) + b1*(A.y); // Line CD represented as a2x + b2y = c2 double a2 = D.y - C.y; double b2 = C.x - D.x; double c2 = a2*(C.x)+ b2*(C.y); double determinant = a1*b2 - a2*b1; if (determinant == 0 ) { // The lines are parallel. This is simplified // by returning a pair of FLT_MAX return new Point(Double.MAX_VALUE, Double.MAX_VALUE); } else { double x = (b2*c1 - b1*c2)/determinant; double y = (a1*c2 - a2*c1)/determinant; return new Point(x, y); } } // Driver method public static void main(String args[]) { Point A = new Point( 1 , 1 ); Point B = new Point( 4 , 4 ); Point C = new Point( 1 , 8 ); Point D = new Point( 2 , 4 ); Point intersection = lineLineIntersection(A, B, C, D); if (intersection.x == Double.MAX_VALUE && intersection.y == Double.MAX_VALUE) { System.out.println( "The given lines AB and CD are parallel." ); } else { // NOTE: Further check can be applied in case // of line segments. Here, we have considered AB // and CD as lines System.out.print( "The intersection of the given lines AB " + "and CD is: " ); Point.displayPoint(intersection); } } } |
C#
using System; // C# Implementation. To find the point of // intersection of two lines // Class used to used to store the X and Y // coordinates of a point respectively public class Point { public double x, y; public Point( double x, double y) { this .x = x; this .y = y; } // Method used to display X and Y coordinates // of a point public static void displayPoint(Point p) { Console.WriteLine( "(" + p.x + ", " + p.y + ")" ); } } public class Test { public static Point lineLineIntersection(Point A, Point B, Point C, Point D) { // Line AB represented as a1x + b1y = c1 double a1 = B.y - A.y; double b1 = A.x - B.x; double c1 = a1 * (A.x) + b1 * (A.y); // Line CD represented as a2x + b2y = c2 double a2 = D.y - C.y; double b2 = C.x - D.x; double c2 = a2 * (C.x) + b2 * (C.y); double determinant = a1 * b2 - a2 * b1; if (determinant == 0) { // The lines are parallel. This is simplified // by returning a pair of FLT_MAX return new Point( double .MaxValue, double .MaxValue); } else { double x = (b2 * c1 - b1 * c2) / determinant; double y = (a1 * c2 - a2 * c1) / determinant; return new Point(x, y); } } // Driver method public static void Main( string [] args) { Point A = new Point(1, 1); Point B = new Point(4, 4); Point C = new Point(1, 8); Point D = new Point(2, 4); Point intersection = lineLineIntersection(A, B, C, D); if (intersection.x == double .MaxValue && intersection.y == double .MaxValue) { Console.WriteLine( "The given lines AB and CD are parallel." ); } else { // NOTE: Further check can be applied in case // of line segments. Here, we have considered AB // and CD as lines Console.Write( "The intersection of the given lines AB " + "and CD is: " ); Point.displayPoint(intersection); } } } // This code is contributed by Shrikant13 |
输出:
The intersection of the given lines AB and CD is: (2.4, 2.4)
本文由 安雅·金达尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。