# Ray VS Line intersection

Continue vector vs plane intersection

Image

How to determine Ray (one point R(rx;ry) and alpha with OX) with line (two points A(ax;ay) and B(bx;by)) intersection?

-

The line segment $AB = \{A + t(B-A) \in \mathbb R^2|t \in [0,1]\}$.

The ray $L = \{R+s(\cos(\alpha),\sin(\alpha))\in \mathbb R^2|s \in \mathbb R^+\}$.

Therefore the intersection $AB \cap L$ is the set of points $(x,y)$ that satisfy $$A + t(B-A) = R+s(\cos(\alpha),\sin(\alpha))$$ for some $t \in [0,1]$ and $s \in \mathbb R^+$. To solve this split it into components:

• $A_x + t(B_x-A_x) = R_x+s\cos(\alpha)$
• $A_y + t(B_y-A_y) = R_y+s\sin(\alpha)$

and solve both sides for $t$:

• $t = \frac{R_x-A_x}{B_x-A_x}+s\frac{\cos(\alpha)}{B_x-A_x}$
• $t = \frac{R_y-A_y}{B_y-A_y}+s\frac{\sin(\alpha)}{B_y-A_y}$

now we equate these to solve for $s$:

$$s = \frac{\frac{R_x-A_x}{B_x-A_x}-\frac{R_y-A_y}{B_y-A_y}}{\frac{\sin(\alpha)}{B_y-A_y}-\frac{\cos(\alpha)}{B_x-A_x}}$$

You can back substitute this to get $t$ and check that $t \in [0,1]$ and $s \in \mathbb R^+$ before back substituting to find the intersection point.

-