# Condition for line intersecting cuboid

Problem: This problem came up in a programming task of mine: I am given four points $A,B,C,D$. I need to find a condition for the connecting line of $A$ and $B$ to intersect the cuboid formed by $C$ and $D$, where $C$ and $D$ are the vertex closest to and farthest away from the origin respectively.

Now clearly if either $A$, $B$ or both are inside the cuboid, the line will intersect the cuboid and these conditions are easy to check for. But I can't seem to find a general condition when both $A$ and $B$ are outside the cuboid.

Let us assume that your cuboid is axis aligned.

Write the parameteric equation of the line, $P=(1-t)A+tB$.

Consider the $x$ component alone: $$x_C<(1-t)x_A+tx_B<x_D,$$ or, assuming $x_B>x_A$,

$$\frac{x_C-x_A}{x_B-x_A} < t < \frac{x_D-x_A}{x_B-x_A}.$$

The case of $x_B<x_A$ is treated similarly, reversing the inequalities. The case of $x_B=x_A$ amounts to $x_C<x_A=x_B<x_D$; if true, $t$ can take any value.

Repeating on $y$ and $z$, you get up to three bracketings of $t$. There is a feasible solution if the largest lower bound does not exceed the smallest upper bound.

If you are looking for intersection with the line segment instead of the whole line, add the bracketing $0<t<1$.

This is a picking problem. Try to intersect $AB$ with every face of your cuboid. But how can two points define a cuboid?

Never the less lets say $C$ $D$ and $E$ span one parallelogram of your polygone. Than try to solve the equation.

$$A+\lambda (B-A)=C+\alpha (D-C)+\beta (E-C)$$

Where $A$, $B$, $C$, $D$ and $E$ are vectors.

Afaik. If this equation has a solution and the greeks are between $0$ and $1$, then $AB$ intersects this face.