Title: Plane intersecting line segment
Tags: geometry;3d
Question: 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?

Here is the accepted 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.
Comment for this answer: Dear @PUK, if a line belongs to a plane, then it surely *is* parallel with it. At any rate, when it is so, the intersection - as in set theory - of the plane and the line is the whole line (the very same one). I don't understand in what sense it would make sense to pick two particular points on the line as "better intersections" than all the other points. Also, I am confused by your separate discussion of negative and positive values of $dist.dist$. A fun about maths and algebra is that one may calculate with negative numbers just like with the positive ones w/o splitting derivations.
Comment for this answer: The objects you called $dist$ are not distances in the exact sense. They're inner products which can be both positive and negative. There's no reason to divide the debate to the positive and negative case because all the geometrically natural formulae here are linear, rational, or otherwise analytic and they work uniformly both for positive and negative values of the inner products.
Comment for this answer: @Luboš: First, thanks for the answer. My algorithm is sectioning a triangulated model where the line segments are the edges of the triangles. For this particular application, it makes sense to me to pick an endpoint (or both) when $\cos\theta = 0$ otherwise I'm losing data. When $\cos\theta = 0$ in this algorithm, it means the line belongs to the plane (and can't be parallel) because otherwise the product of the distances would be $\geq 0$.
Comment for this answer: I meant to say $>0$
Comment for this answer: @Luboš: Appreciate the correction about a line belonging to the plane being parallel to it. WHY PICK ENDPOINTS: I'm writing a function in a computer the inputs of which are a plane, and a set of line segments (edges of triangles as it's a triangulated mesh). The output of the function is a set of points which represent the cross-section. I cannot output an infinite set of points when the line segment is in the plane, hence the selection of the endpoints. WHY DIVIDE +VE AND -VE: I'm not interested in the positive product as there's no intersection with the plane and the line segment (cont'd).
Comment for this answer: @Luboš: I didn't quite understand your comment about not treating the positive and negative cases distinctly. Given a plane and a line segment, that's the only way I could come up with to determine intersection.


{'source': 'StackExchangeDataset', 'subset': 'math.stackexchange.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:06/25/11', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<geometry><3d>', 'Title': 'Plane intersecting line segment', 'ViewCount': 2759, 'id': 47594, 'user_AboutMe': "<p>I'm a CAD developer teaching myself mathematics so I can do my job more effectively while enjoying the mental stimulation.</p>\n", 'user_AccountId': 67220.0, 'user_DisplayName': 'PeteUK', 'user_Reputation': 1430.0, 'user_Views': 265.0, 'user_id': 7879.0}}