📜  两条线的交点程序

📅  最后修改于: 2021-10-23 08:32:08             🧑  作者: Mango

给定与线AB对应的点A和B以及与线PQ对应的点P和Q,找到这些线的交点。这些点在 2D 平面中给出,并带有它们的 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 )。现在,我们找到由这些点组成的直线方程。

让给定的行是:

  1. a 1 x + b 1 y = c 1
  2. a 2 x + b 2 y = c 2

我们现在必须求解这两个方程才能找到交点。为了求解,我们将 1. 乘以 b 2和 2 乘以 b 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 , x 2 ) <= x <= 最大值 (x 1 , x 2 )
  • min (y 1 , y 2 ) <= y <= max (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 
using namespace std;
  
// This pair is used to store the X and Y
// coordinates of a point respectively
#define pdd pair
  
// 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.\n";
    }
  
    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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程