How do you check for intersection between a line segment and a line ray emanating from a point at an angle from horizontal?

Let’s label the points q = (x1, y1) and q + s = (x2, y2). Hence s = (x2x1, y2y1). Then the problem looks like this:

Let r = (cos θ, sin θ). Then any point on the ray through p is representable as p + t r (for a scalar parameter 0 ≤ t) and any point on the line segment is representable as q + u s (for a scalar parameter 0 ≤ u ≤ 1).

The two lines intersect if we can find t and u such that p + t r = q + u s:

See this answer for how to find this point (or determine that there is no such point).

Then your line segment intersects the ray if 0 ≤ t and 0 ≤ u ≤ 1.

Leave a Comment