# Finding the intersection point of a line with a rectangle defined by 4 points

There exists some rectangle in 3d defined by 4 points $$p1,p2,p3,p4$$, and some line that's defined by two points $$q1,q2$$.

How to find out if the line intersects the rectangle?

So basically I have the answer, but I just have questions about it.

We find the intersection between the line spanned by the segment and the plane spanned by the rectangle and then verify that the intersection point is on both.

let

$$v_1=p_2-p_1$$

$$v_2=p_3-p_1$$

$$N=v_1 \times v_2$$

$$u=q_2-q_1$$

Then we find $$t$$ such that $$((q_1+tu)-p_1)\cdot N=0$$

Question 1: I don't 100% understand why we take $$v_2=p_3-p_1$$ - why we take the diagonal of the rectangle (from the drawing it's the diagonal)? I Would it still be correct if we defined $$v_2=p_4-p_1$$ ?

And obviously if $$t<0$$ or $$t>1$$ we reject since it's not on the actual line.

The intersection point on the plane is $$=q_1+tu$$

Now we want to check if $$p$$ resides in the given rectangle. In the answers they say:

if $$(p-p_1)\cdot v_1 <0$$ or $$(p-p_1)\cdot v_1 > v_1 \cdot v_1$$ reject, and the same thing for $$v_2$$.

Question 2: I'm having a hard time understanding the above. I think that by doing $$(p-p_1)\cdot v_1 <0$$ it's basically projecting $$p-p_1$$ on $$v_1$$, and if it's negative, then $$a>90$$ and it's not on the rectangle. But what happens with $$(p-p_1)\cdot v_1 > v_1 \cdot v_1$$ ? What is that checking? What's the geometrical meaning of this?

I assume that the naming of the points is such that $$p_1p_2,\;p_2p_3,\;p_3p_4,\;p_4p_1$$ are the four sides of the rectangle.

For finding $$t$$, it does not matter if you take $$p_3$$ or $$p_4$$ to get $$v_2.$$ You only need a set of two independent vectors to span the plane in which the rectangle resides.

However it matters for the question 2. You must use $$v_2=p_4-p_1$$

Example: $$p_1=\begin{pmatrix} 0 \\ 0\\ 0\end{pmatrix}\;,\;\; p_2=\begin{pmatrix} 1 \\ 0\\ 0\end{pmatrix}\;,\;\; p_3=\begin{pmatrix} 1 \\ 1\\ 0\end{pmatrix}\;,\;\; p_4=\begin{pmatrix} 0 \\ 1\\ 0\end{pmatrix}$$ and $$p=\begin{pmatrix} \frac{1}{4} \\ \frac{5}{4} \\ 0\end{pmatrix}$$ If you define $$v_2=p_3-p_1,$$ then $$0 < (p-p_1)\cdot v_1 = \frac{1}{4} < 1 = v_1\cdot v_1 \\ 0 < (p-p_1)\cdot v_2 = \frac{3}{2} < 2 = v_2\cdot v_2$$ although the point $$p$$ is clearly outside of the rectangle.

The reasoning behind the inequalities: Let us call the direction of $$v_1$$ "right" and the direction of $$v_2$$ "up" (which only makes sense if we define $$v_2=p_4-p_1$$ as you proposed). In the following, I use "left", "right", "above" and "below" accordingly. Then

$$(p-p_1)\cdot v_1 < 0$$ means that $$p$$ is more to the left than $$p_1.$$ Due to all the right angles this means "more to the left than the $$p_4p_1$$ edge."

$$(p-p_1)\cdot v_1 > v_1\cdot v_1$$ means that $$p$$ is more to the right than $$p_2.$$ This means "more to the right than the $$p_2p_3$$ edge."

$$(p-p_1)\cdot v_2 < 0$$ means that $$p$$ is below $$p_1.$$ This means "below the $$p_1p_2$$ edge."

$$(p-p_1)\cdot v_2 > v_2\cdot v_2$$ means that $$p$$ is above $$p_4.$$ This means "above the $$p_3p_4$$ edge."