Line–line intersection
{{Short description|Common point(s) shared by two lines in Euclidean geometry}}
Image:Line-Line Intersection.png
In Euclidean geometry, the intersection of a line and a line can be the empty set, a point, or another line. Distinguishing these cases and finding the intersection have uses, for example, in computer graphics, motion planning, and collision detection.
In three-dimensional Euclidean geometry, if two lines are not in the same plane, they have no point of intersection {{Cite web |first=Jeremy |last=Cook |date=November 21, 2023 |title=Coplanar Lines in Geometry {{!}} Definition, Diagrams & Examples |url=https://study.com/academy/lesson/coplanar-lines-in-geometry-definition-lesson-quiz.html#:~:text=Non%2Dcoplanar%20lines%20are%20lines%20that%20are%20not%20on%20the,not%20in%20the%20same%20plane. |access-date=November 1, 2024}} and are called skew lines. If they are in the same plane, however, there are three possibilities: if they coincide (are not distinct lines), they have an infinitude of points in common (namely all of the points on either of them); if they are distinct but have the same slope, they are said to be parallel and have no points in common; otherwise, they have a single point of intersection.
The distinguishing features of non-Euclidean geometry are the number and locations of possible intersections between two lines and the number of possible lines with no intersections (parallel lines) with a given line.{{Further explanation needed|reason=This is an encyclopedic topic, go write about it|date=June 2022}}
Formulas
{{see also|Skew lines#Formulas}}
A necessary condition for two lines to intersect is that they are in the same plane—that is, are not skew lines. Satisfaction of this condition is equivalent to the tetrahedron with vertices at two of the points on one line and two of the points on the other line being degenerate in the sense of having zero volume. For the algebraic form of this condition, see {{slink|Skew lines|Testing for skewness}}.
=Given two points on each line=
First we consider the intersection of two lines {{math|L1}} and {{math|L2}} in two-dimensional space, with line {{math|L1}} being defined by two distinct points {{math|(x1, y1)}} and {{math|(x2, y2)}}, and line {{math|L2}} being defined by two distinct points {{math|(x3, y3)}} and {{math|(x4, y4)}}.{{MathWorld|title=Line-Line Intersection|id=Line-LineIntersection|access-date=2008-01-10}}
The intersection {{mvar|P}} of line {{math|L1}} and {{math|L2}} can be defined using determinants.
:
P_x = \frac{\begin{vmatrix} \begin{vmatrix} x_1 & y_1\\x_2 & y_2\end{vmatrix} & \begin{vmatrix} x_1 & 1\\x_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & y_3\\x_4 & y_4\end{vmatrix} & \begin{vmatrix} x_3 & 1\\x_4 & 1\end{vmatrix} \end{vmatrix} }
{\begin{vmatrix} \begin{vmatrix} x_1 & 1\\x_2 & 1\end{vmatrix} & \begin{vmatrix} y_1 & 1\\y_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & 1\\x_4 & 1\end{vmatrix} & \begin{vmatrix} y_3 & 1\\y_4 & 1\end{vmatrix} \end{vmatrix}}\,\!
\qquad
P_y = \frac{\begin{vmatrix} \begin{vmatrix} x_1 & y_1\\x_2 & y_2\end{vmatrix} & \begin{vmatrix} y_1 & 1\\y_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & y_3\\x_4 & y_4\end{vmatrix} & \begin{vmatrix} y_3 & 1\\y_4 & 1\end{vmatrix} \end{vmatrix} }
{\begin{vmatrix} \begin{vmatrix} x_1 & 1\\x_2 & 1\end{vmatrix} & \begin{vmatrix} y_1 & 1\\y_2 & 1\end{vmatrix} \\\\ \begin{vmatrix} x_3 & 1\\x_4 & 1\end{vmatrix} & \begin{vmatrix} y_3 & 1\\y_4 & 1\end{vmatrix} \end{vmatrix}}\,\!
The determinants can be written out as:
:
\begin{align}
P_x&= \frac{(x_1 y_2-y_1 x_2)(x_3-x_4)-(x_1-x_2)(x_3 y_4-y_3 x_4)}{(x_1-x_2) (y_3-y_4) - (y_1-y_2) (x_3-x_4)}
\\[4px]
P_y&= \frac{(x_1 y_2-y_1 x_2)(y_3-y_4)-(y_1-y_2)(x_3 y_4-y_3 x_4)}{(x_1-x_2) (y_3-y_4) - (y_1-y_2) (x_3-x_4)}
\end{align}
When the two lines are parallel or coincident, the denominator is zero.
=Given two points on each line segment=
{{see also|Intersection_(geometry)#Two_line_segments}}
The intersection point above is for the infinitely long lines defined by the points, rather than the line segments between the points, and can produce an intersection point not contained in either of the two line segments. In order to find the position of the intersection in respect to the line segments, we can define lines {{math|L1}} and {{math|L2}} in terms of first degree Bézier parameters:
:
L_1 = \begin{bmatrix}x_1 \\ y_1\end{bmatrix}
+ t \begin{bmatrix}x_2-x_1 \\ y_2-y_1\end{bmatrix},
\qquad
L_2 = \begin{bmatrix}x_3 \\ y_3\end{bmatrix}
+ u \begin{bmatrix}x_4-x_3 \\ y_4-y_3\end{bmatrix}
(where {{mvar|t}} and {{mvar|u}} are real numbers). The intersection point of the lines is found with one of the following values of {{mvar|t}} or {{mvar|u}}, where
:
t = \frac{\begin{vmatrix} x_1-x_3 & x_3-x_4\\y_1-y_3 & y_3-y_4\end{vmatrix}}{\begin{vmatrix} x_1-x_2 & x_3-x_4\\y_1-y_2 & y_3-y_4\end{vmatrix}} = \frac{(x_1 - x_3)(y_3-y_4)-(y_1-y_3)(x_3-x_4)}{(x_1-x_2)(y_3-y_4)-(y_1-y_2)(x_3-x_4)}
and
:
u = - \frac{\begin{vmatrix} x_1-x_2 & x_1-x_3\\y_1-y_2 & y_1-y_3\end{vmatrix}}{\begin{vmatrix} x_1-x_2 & x_3-x_4\\y_1-y_2 & y_3-y_4\end{vmatrix}} = -\frac{(x_1 - x_2)(y_1-y_3)-(y_1-y_2)(x_1-x_3)}{(x_1-x_2)(y_3-y_4)-(y_1-y_2)(x_3-x_4)},
with
:
(P_x, P_y)= \bigl(x_1 + t (x_2-x_1),\; y_1 + t (y_2-y_1)\bigr) \quad \text{or} \quad (P_x, P_y) = \bigl(x_3 + u (x_4-x_3),\; y_3 + u (y_4-y_3)\bigr)
There will be an intersection if {{math|0 ≤ t ≤ 1}} and {{math|0 ≤ u ≤ 1}}. The intersection point falls within the first line segment if {{math|0 ≤ t ≤ 1}}, and it falls within the second line segment if {{math|0 ≤ u ≤ 1}}. These inequalities can be tested without the need for division, allowing rapid determination of the existence of any line segment intersection before calculating its exact point.{{cite book |last1=Antonio |first1=Franklin |editor-last=Kirk |editor-first=David |title=Graphics Gems III |publisher=Academic Press, Inc. |date=1992 |pages=199–202 |chapter=Chapter IV.6: Faster Line Segment Intersection |isbn=0-12-059756-X}}
In the case where the two line segments share an x axis and , and simplify to
t = u = \frac{y_1 - y_3}{y_1 - y_2 - y_3 + y_4},
with
(P_x, P_y)= \bigl(x_1 + t,\; y_1 + t (y_2-y_1)\bigr) \quad \text{or} \quad (P_x, P_y) = \bigl(x_1 + t,\; y_3 + t (y_4-y_3)\bigr).
=Given two line equations=
{{see also|Intersection_(geometry)#Two_lines}}
The {{mvar|x}} and {{mvar|y}} coordinates of the point of intersection of two non-vertical lines can easily be found using the following substitutions and rearrangements.
Suppose that two lines have the equations {{math|y {{=}} ax + c}} and {{math|y {{=}} bx + d}} where {{mvar|a}} and {{mvar|b}} are the slopes (gradients) of the lines and where {{mvar|c}} and {{mvar|d}} are the {{mvar|y}}-intercepts of the lines. At the point where the two lines intersect (if they do), both {{mvar|y}} coordinates will be the same, hence the following equality:
:
We can rearrange this expression in order to extract the value of {{mvar|x}},
:
and so,
:
To find the {{mvar|y}} coordinate, all we need to do is substitute the value of {{mvar|x}} into either one of the two line equations, for example, into the first:
:
Hence, the point of intersection is
:
Note that if {{math|a {{=}} b}} then the two lines are parallel and they do not intersect, unless {{math|c {{=}} d}} as well, in which case the lines are coincident and they intersect at every point.
= Using homogeneous coordinates =
By using homogeneous coordinates, the intersection point of two implicitly defined lines can be determined quite easily. In 2D, every point can be defined as a projection of a 3D point, given as the ordered triple {{math|(x, y, w)}}. The mapping from 3D to 2D coordinates is {{math|(x′, y′) {{=}} ({{sfrac|x|w}}, {{sfrac|y|w}})}}. We can convert 2D points to homogeneous coordinates by defining them as {{math|(x, y, 1)}}.
Assume that we want to find intersection of two infinite lines in 2-dimensional space, defined as {{math|a1x + b1y + c1 {{=}} 0}} and {{math|a2x + b2y + c2 {{=}} 0}}. We can represent these two lines in line coordinates as {{math|U1 {{=}} (a1, b1, c1)}} and {{math|U2 {{=}} (a2, b2, c2)}}. The intersection {{math|P′}} of two lines is then simply given by{{Cite web |last=Birchfield |first=Stanley |date=1998-04-23 |title=Homogeneous coordinates |url=http://robotics.stanford.edu/~birch/projective/node4.html |url-status=live |archive-url=https://web.archive.org/web/20000929025956/http://robotics.stanford.edu/~birch/projective/node4.html |archive-date=2000-09-29 |website=robotics.stanford.edu |accessdate=2015-08-18}}
:
If {{math|cp {{=}} 0}}, the lines do not intersect.
More than two lines
{{see also|Skew lines#More than two lines}}
The intersection of two lines can be generalized to involve additional lines. The existence of and expression for the {{mvar|n}}-line intersection problem are as follows.
= In two dimensions =
In two dimensions, more than two lines almost certainly do not intersect at a single point. To determine if they do and, if so, to find the intersection point, write the {{mvar|i}}th equation ({{math|i {{=}} 1, …, n}}) as
:
and stack these equations into matrix form as
:
where the {{mvar|i}}th row of the {{math|n × 2}} matrix {{math|A}} is {{math|[ai1, ai2]}}, {{math|w}} is the 2 × 1 vector {{math|[{{su|p=x|b=y}}]}}, and the {{mvar|i}}th element of the column vector {{math|b}} is {{math|bi}}. If {{math|A}} has independent columns, its rank is 2. Then if and only if the rank of the augmented matrix {{math|[A {{!}} b]}} is also 2, there exists a solution of the matrix equation and thus an intersection point of the {{mvar|n}} lines. The intersection point, if it exists, is given by
:
where {{math|Ag}} is the Moore–Penrose generalized inverse of {{math|A}} (which has the form shown because {{math|A}} has full column rank). Alternatively, the solution can be found by jointly solving any two independent equations. But if the rank of {{math|A}} is only 1, then if the rank of the augmented matrix is 2 there is no solution but if its rank is 1 then all of the lines coincide with each other.
= In three dimensions =
The above approach can be readily extended to three dimensions. In three or more dimensions, even two lines almost certainly do not intersect; pairs of non-parallel lines that do not intersect are called skew lines. But if an intersection does exist it can be found, as follows.
In three dimensions a line is represented by the intersection of two planes, each of which has an equation of the form
:
Thus a set of {{mvar|n}} lines can be represented by {{math|2n}} equations in the 3-dimensional coordinate vector {{math|w}}:
:
where now {{math|A}} is {{math|2n × 3}} and {{math|b}} is {{math|2n × 1}}. As before there is a unique intersection point if and only if {{math|A}} has full column rank and the augmented matrix {{math|[A {{!}} b]}} does not, and the unique intersection if it exists is given by
:
Nearest points to skew lines
File:skew_lines_shortest_distance.svg
{{main|Skew lines#Nearest points}}
In two or more dimensions, we can usually find a point that is mutually closest to two or more lines in a least-squares sense.
=In two dimensions=
In the two-dimensional case, first, represent line {{mvar|i}} as a point {{math|pi}} on the line and a unit normal vector {{math|n̂i}}, perpendicular to that line. That is, if {{math|x1}} and {{math|x2}} are points on line 1, then let {{math|p1 {{=}} x1}} and let
:
which is the unit vector along the line, rotated by a right angle.
The distance from a point {{math|x}} to the line {{math|(p, n̂)}} is given by
:
And so the squared distance from a point {{math|x}} to a line is
:
The sum of squared distances to many lines is the cost function:
:
This can be rearranged:
:
\begin{align}
E(\mathbf{x}) & = \sum_i \mathbf{x}^\mathsf{T} \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \mathbf{x} - \mathbf{x}^\mathsf{T} \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \mathbf{p}_i - \mathbf{p}_i^\mathsf{T} \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \mathbf{x} + \mathbf{p}_i^\mathsf{T} \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \mathbf{p}_i \\
& = \mathbf{x}^\mathsf{T} \left(\sum_i \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T}\right) \mathbf{x} - 2 \mathbf{x}^\mathsf{T} \left(\sum_i \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \mathbf{p}_i\right) + \sum_i \mathbf{p}_i^\mathsf{T} \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \mathbf{p}_i.
\end{align}
To find the minimum, we differentiate with respect to {{math|x}} and set the result equal to the zero vector:
:
so
:
and so
:
=In more than two dimensions=
While {{math|n̂i}} is not well-defined in more than two dimensions, this can be generalized to any number of dimensions by noting that {{math|n̂i n̂iT}} is simply the symmetric matrix with all eigenvalues unity except for a zero eigenvalue in the direction along the line providing a seminorm on the distance between {{math|pi}} and another point giving the distance to the line. In any number of dimensions, if {{math|v̂i}} is a unit vector along the {{mvar|i}}th line, then
: becomes
where {{math|I}} is the identity matrix, and so{{cite web |last1=Traa |first1=Johannes |date=2013 |title=Least-Squares Intersection of Lines |url=http://cal.cs.illinois.edu/~johannes/research/LS_line_intersect.pdf |url-status=dead |archive-url=https://web.archive.org/web/20170912055605/http://cal.cs.illinois.edu/~johannes/research/LS_line_intersect.pdf |archive-date=2017-09-12 |website=cal.cs.illinois.edu |accessdate=2018-08-30}}
:
=General derivation=
In order to find the intersection point of a set of lines, we calculate the point with minimum distance to them. Each line is defined by an origin {{math|ai}} and a unit direction vector {{math|n̂i}}. The square of the distance from a point {{math|p}} to one of the lines is given from Pythagoras:
:
= \left( \mathbf{p} - \mathbf{a}_i \right)^\mathsf{T} \left( \mathbf{p} - \mathbf{a}_i \right) - \left( \left( \mathbf{p} - \mathbf{a}_i \right)^\mathsf{T} \mathbf{\hat n}_i \right)^2
where {{math|(p − ai)T n̂i}} is the projection of {{math|p − ai}} on line {{mvar|i}}. The sum of distances to the square to all lines is
:
To minimize this expression, we differentiate it with respect to {{math|p}}.
:
:
which results in
:
= \sum_i \left(\mathbf{I} - \mathbf{\hat n}_i \mathbf{\hat n}_i^\mathsf{T} \right) \mathbf{a}_i
where {{math|I}} is the identity matrix. This is a matrix {{math|Sp {{=}} C}}, with solution {{math|p {{=}} S+C}}, where {{math|S+}} is the pseudo-inverse of {{math|S}}.
Non-Euclidean geometry
{{See also|Parallel postulate}}{{Expand section|date=June 2022}}
File:Euclidian and non euclidian geometry.png
In spherical geometry, any two great circles intersect.{{Cite web |title=Exploring Hyperbolic Space |url=https://math.berkeley.edu/~kpmann/Hyperbolic%20and%20Spherical%20geometries.pdf |access-date=2022-06-03 |website=math.berkeley.edu}}
In hyperbolic geometry, given any line and any point, there are infinitely many lines through that point that do not intersect the given line.
See also
- Line segment intersection
- Line intersection in projective space
- Distance between two parallel lines
- Distance from a point to a line
- Line–plane intersection
- Parallel postulate
- Triangulation (computer vision)
- {{slink|Intersection (Euclidean geometry)|Two line segments}}
References
External links
- [http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm Distance between Lines and Segments with their Closest Point of Approach], applicable to two, three, or more dimensions.
{{DEFAULTSORT:Line-line intersection}}