P T H I N K F A ST S
Competitive Programming
From Problem 2 Solution in O(1)
Computational Geometry
Lines Intersection
Mostafa Saad Ibrahim
PhD Student @ Simon Fraser University
Line Line Intersection
Src: https://sheridanmath.wikispaces.com/file/view/GEOMETRY_04.GIF/177066721/GEOMETRY_04.GIF
2D Lines Intersection
◼ In such intersection either no solution
(parallel) or one solution only
◼ Assume 2D explicit line format
◼ a1x+b1y=c1
◼ a2x+b2y=c2
◼ if a1b2−a2b1=0
◼ parallel (or identical) lines
◼ Otherwise, just solve system of equations
◼ You can do that by several ways
2D Lines Intersection
◼ One way is using Cramer's rule
◼ Cramer is inefficient, but ok for 2x2 matrix
◼ It is instable actually
◼ Cramer is based on determinant
Issues
◼ What about Segment/Segment Intersection?
◼ One can simply compute the intersection point and check
if it lies on the segment/ray
◼ What about 3D lines?
◼ 2D lines are either parallel or intersecting
◼ But 3D and more are rarely intersecting
◼ Also, linear projections onto a 2D plane will also intersect
◼ Overall, we can use this method
◼ However, let’s move to the parametric style
Parametric equations
◼ Remember each line/segment has t parameter
◼ E.g. from [0-1] express on segment
◼ Let first line has t1 and 2nd has t2
◼ Then
◼ X value for first line1 at t1 = X value for first line2 at t2
◼ Y value for first line1 at t1 = Y value for first line2 at t2
◼ using these information, we can create 2
equations and solve them to get t1 and t2
◼ Using them we can compute the point or
validtate segment/ray things
Parametric equations
◼ Specifically, Assume points a, b, c, d
◼ Assume line 1 is ab and 2nd is cd
◼ a.x + (b.x - a.x)*t1 = c.x + (d.x - c.x)*t2 (1)
◼ a.y + (b.y - a.y)*t1 = c.y + (d.y - c.y)*t2 (2)
◼ One can use Cramer's to solve them
◼ Or even by hand computations
◼ E.g. multiply first by (d.y - c.y) and
◼ second by (d.x - c.x)
◼ then subtract equations and rearrange
Parametric equations (2)
◼ We already showed how to do it
◼ Let’s do it using other (harder) way
◼ Assume lines P0P1 and Q0Q1
◼ Their vectors u and v
Parametric equations (2)
◼ S1 is the intersecting point at line P0P1
◼ Can we make a solvable equation based on P(s1)?
◼ Notice v.vt = 0 (dot product between perpendicular vecs)
◼ From vector addition: v = w + s1u
◼ Then vt.(w + s1u) = 0
Parametric equations (2)
◼ Solving this equation for line 1 and (similar way to line 2)
Parametric equations (2)
Is segment segment intersect?
◼ What if all what we need to know is this
boolean answer?
◼ Let’s first know the counter (anti) clockwise
test
◼ CCW is a nice utility for some programs (e.g. test point
inside a triangle)
Counterclockwise test
◼ Given three points a, b, c
◼ Think about the path from a to b to c
◼ Either it goes counterclockwise
◼ Or goes clockwise
◼ Or it doesn’t in some collinear cases
◼ So we have 3 different responses
◼ CCW, CW, Undefined
◼ Let answer for any point between a-b inclusive
to be undefined
Counterclockwise test
Counterclockwise test
Is segment segment intersect?
Is segment segment intersect?
Other intersections types
◼ There are other types of intersections, but they
are rare (if any) in competitive programming
◼ Intersection of a Ray/Segment with a Plane
◼ Intersection of a Ray/Segment with a Triangle
◼ Intersection of a Triangle with a Plane
◼ Intersection of a Triangle with a Triangle
◼ Line-Plane Intersection
◼ Intersection of 2 Planes
◼ Intersection of 3 Planes
◼ For reading in these types: See1, See2
ﺗﻢ ﺑﺤﻤﺪ ﷲ
ﻋﻠﻤﻜﻢ ﷲ ﻣﺎ ﯾﻨﻔﻌﻜﻢ
وﻧﻔﻌﻜﻢ ﺑﻤﺎ ﺗﻌﻠﻤﺘﻢ
وزادﻛﻢ ﻋﻠﻤﺎ ً