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?