给定对应于线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 )。现在,我们找到由这些点形成的直线方程。
让给定的行为:
- a 1 x + b 1 y = c 1
- 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 )
- 最小值(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
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)