[Math] Plane intersecting line segment

3dgeometry

I have a plane which is represented as a 3d point $\vec{p}$ with a normal $\hat{n}$. I also have a line segment specified by two points $\vec{v_1},\vec{v_2}$ . I want to get the intersection point (if any). Here's what I have:

$${\rm dist}_{v_1} = \hat{n} \cdot (\vec{v_1} – \vec{p})$$
$${\rm dist}_{v_2} = \hat{n} \cdot (\vec{v_2} – \vec{p})$$

If ${\rm dist}_{v_1}\cdot {\rm dist}_{v_2} \leq 0$ then I know there is an intersection because the distances are signed and the product of numbers with opposite signs is negative. Zero is included to cover the case when an endpoint is exactly on the plane. Then:

$$\hat{x} = \frac{\vec{v_2} – \vec{v_1}}{\left|\vec{v_2} – \vec{v_1}\right|}$$
$$\cos\theta = \hat{n}\cdot\hat{x}$$

Now I test $\cos\theta$. If zero then I choose one (of both) the endpoints as the intersection point. If non-zero I proceed to find the intersection point $\vec{v}$:
$$\vec{{v}} = \vec{v_2} – \hat{x}({\rm dist}_{v_2} / \cos\theta)$$

I'd like to know if my solution can be reworked to be more "elegant" without the last check of $\cos\theta$ being non-zero to avoid a divide-by-zero?

Best Answer

The formula is correct (your additional comments what to do when $\cos\theta=0$ are not) and you can't avoid a possible division by zero because the division by zero is the right result.

If $\cos\theta$ vanishes, it means that $\hat n$ - the normal direction of the plane - is perpendicular to $\vec v_2-\vec v_1$, the direction of the line. In other words, the direction of the line $\vec v_2-\vec v_1$ is parallel to the plane.

If it is parallel, the line either belongs to the plane, in which case there is a whole line of intersections and the division appropriately yields a $0/0$ indeterminate form; or the line is outside the plane in which case the division by zero is of the form $1/0$ and there is no intersection (or the intersections is infinitely far, if you wish).

You can't get finite unique coordinates of the intersection if $\cos\theta=0$ because there aren't any.

Related Question