Math gurus,
I apologize because in a previous post I asked how to determine if a line passes through a triangle; and an answer was given. Then, while testing, I came across the scenario where a vertical line “segment” was below the base of the triangle and the answer to my previous question was giving false positives. This was my own fault because clearly there is a difference between a line and a line segment.
Below is a diagram of what I would like to accomplish. All the green line segments are considered to be passing "through" the triangle. The red line segments (obviously) do not.
The data I am using has x, y and z values, but for this test, we can assume the triangle and line segment will be coplanar.
The triangle I am using for testing is defined by the points:
vo = (2,2,1) v1 = (7,2,1), v2 = (5,6,1)
and each of my tests cases are using these points:
- Line segment outside – no intersection: p0 = (1,4,1) p1 = (3,7,1)
- Intersection at vertex: p0 = (4,6,1) p1 = (8,6,1)
- Intersection through 2 edges: p0 = (4,1,1) p1 = (5,7,1)
- Intersection contains edge: p0 = (1,2,1) p1 = (5,8,1)
- Intersection through 1 vertex and edge: p0 = (5,1,1) p1 = (5,8,1)
Is there some specific formula that will provide a “true” / “false” indication as to whether a line segment passes over the triangle as described above?
I do NOT need to know the point of intersection in the case of a "true" result (but if it is part of determining the result, that would be a bonus).
Thank-you in advance for your help and patience.


I would suggest transforming to planar coordinates, so that the triangle vertices are mapped to $(0,0)$, $(1,0)$, and $(1,1)$.
Let's say your triangle vertices are $( x_0 , y_0 , z_0 )$, $( x_1 , y_1 , z_1 )$, and $( x_2 , y_2 , z_2 )$. First, calculate $$\begin{array}{l} d_{XY} = x_0 ( y_1 - y_2 ) + x_1 ( y_2 - y_0 ) - x_2 ( y_1 - y_0 ) \\ d_{XZ} = x_0 ( z_1 - z_2 ) + x_1 ( z_2 - z_0 ) - x_2 ( z_1 - z_0 ) \\ d_{YZ} = y_0 ( z_1 - z_2 ) + y_1 ( z_2 - z_0 ) - y_2 ( z_1 - z_0 ) \end{array} \tag{1}\label{1}$$ Depending on which one of $\eqref{1}$ is largest in magnitude, we calculate eight constants.
If $\lvert d_{XY} \rvert \ge \lvert d_{XZ} \rvert$ and $\lvert d_{XY} \rvert \ge \lvert d_{XZ} \rvert$, then $$\begin{array}{ll} U_u = (x_2 y_0 - x_0 y_2) / d_{XY} & V_v = (x_0 y_1 - x_1 y_0) / d_{XY} \\ U_x = (y_2 - y_0) / d_{XY} & V_x = (y_0 - y_1) / d_{XY} \\ U_y = (x_0 - x_2) / d_{XY} & V_y = (x_1 - x_0) / d_{XY} \\ U_z = 0 & V_z = 0 \end{array}$$
Else, if $\lvert d_{XZ} \rvert \ge \lvert d_{XY} \rvert$ and $\lvert d_{XZ} \rvert \ge \lvert d_{YZ} \rvert$, then $$\begin{array}{ll} U_u = (x_2 z_0 - x_0 z_2) / d_{XZ} & V_v = (x_0 z_1 - x_1 z_0) / d_{XZ} \\ U_x = (z_2 - z_0) / d_{XZ} & V_x = (z_0 - z_1) / d_{XZ} \\ U_y = 0 & V_y = 0 \\ U_z = (x_0 - x_2) / d_{XZ} & V_z = (x_1 - x_0) / d_{XZ} \end{array}$$
Otherwise, $\lvert d_{YZ} \rvert \ge \lvert d_{XY} \rvert$ and $\lvert d_{YZ} \rvert \ge \lvert d_{XZ} \rvert$, and $$\begin{array}{ll} U_u = (y_2 z_0 - z_2 y_0) / d_{YZ} & V_v = (y_0 z_1 - y_1 z_0) / d_{YZ} \\ U_x = 0 & V_x = 0 \\ U_y = (z_2 - z_0) / d_{YZ} & V_y = (z_0 - z_1) / d_{YZ} \\ U_z = (y_0 - y_2) / d_{YZ} & V_z = (y_1 - y_0) / d_{YZ} \end{array}$$
Using the above eight constants, we have a function that transforms any $(x, y, z)$ coordinates to out planar coordinates $(u, v)$: $$\begin{array}{ll} u = U_u + x U_x + y U_y + z U_z \\ v = V_v + x V_x + y V_y + z V_z \end{array}\tag{2}\label{2}$$
Use $\eqref{2}$ to transform the line segment endpoint coordinates, so that $( u_0 , v_0 )$ is the starting point, and $( u_1 , v_1 )$ is the end point: $$\begin{array}{l} u_0 = U_u + x_{start} U_x + y_{start} U_y + z_{start} U_z \\ v_0 = V_v + x_{start} V_x + y_{start} V_y + z_{start} V_z \\ u_1 = U_u + x_{end} U_x + y_{end} U_y + z_{end} U_z \\ v_1 = V_v + x_{end} V_x + y_{end} V_y + z_{end} V_z \end{array} \tag{3}\label{3}$$ Note that these are only valid if the line segment is coplanar with the triangle!
To find the line segment and edge intersections, we parametrise the line using $t \in \mathbb{R}$, $0 \le t \le 1$, so that $t = 0$ at one endpoint, and $t = 1$ at the other. This is straightforward linear interpolation; i.e. $$\begin{array}{l} u = (1 - t) u_0 + t u_1 \\ v = (1 - t) v_0 + t v_1 \end{array}$$This is useful, because we only need to find (and then verify) each solution in only one variable, the line parameter $t$.
The triangle has three edges the line segment can intersect. To verify the intersection point, we must find the $t$ corresponding to the intersection point. When $t$ is known, you can trivially calculate the 3D coordinates corresponding to the intersection: $$\begin{array}{ll} x = (1 - t) x_{start} + t x_{end} \\ y = (1 - t) y_{start} + t y_{end} \\ z = (1 - t) z_{start} + t z_{end} \end{array}\tag{4}\label{4}$$
Next, we check the possible cases, one at a time (but in whatever order you want):
If $u_0 = u_1 = 0$, the line segment is parallel to edge from $( x_0 , y_0 , z_0 )$ to $( x_2 , y_2 , z_2 )$.
If $v_0 \le 0$ and $v_1 \gt 0$, or if $v_0 \gt 0$ and $v_1 \le 0$, the line segment intersects the triangle at vertex $( x_0 , y_0 , z_0 )$.
If $v_0 \le 1$ and $v_1 \gt 1$, or if $v_0 \gt 1$ and $v_1 \le 1$, the line segment intersects the triangle at vertex $( x_2 , y_2 , z_2 )$.
If both $0 \le v_0 \le 1$ and $0 \le v_1 \le 1$, then the entire line segment is contained within this edge.
If $v_0 = v_1 = 0$, the line segment is parallel to edge from $( x_0 , y_0 , z_0 )$ to $( x_1 , y_1 , z_1 )$.
If $u_0 \le 0$ and $u_1 \gt 0$, or if $u_0 \gt 0$ and $u_1 \le 0$, the line segment intersects the triangle at vertex $( x_0 , y_0 , z_0 )$.
If $u_0 \le 1$ and $u_1 \gt 1$, or if $u_0 \gt 1$ and $u_1 \le 1$, the line segment intersects the triangle at vertex $( x_1 , y_1 , z_1 )$.
If both $0 \le u_0 \le 1$ and $0 \le u_1 \le 1$, then the entire line segment is contained within this edge.
If $u_0 + v_0 = 1$ and $u_1 + v_1 = 1$, the line segment is parallel to the edge from $( x_1 , y_1 , z_1 )$ to $( x_2 , y_2 , z_2 )$ (i.e., $u + v = 1$).
If $u_0 \lt 0$ and $u_1 \ge 0$, or if $u_0 \ge 0$ and $u_1 \lt 0$, the line intersects the edge at vertex $( x_2 , y_2 , z_2 )$.
If $u_0 \gt 1$ and $u_1 \le 1$, or if $u_0 \le 1$ and $u_1 \gt 1$, the line intersects the edge at vertex $( x_1 , y_1 , z_1 )$.
If $0 \le u_0 \le 1$, $0 \le v_0 \le 1$, $0 \le u_1 \le 1$, and $0 \le v_1 \le 1$, the entire line segment is contained within this edge.
If $u_0 \le 0$ and $u_1 \gt 0$, or if $u_0 \ge 0$ and $u_1 \lt 0$, the line segment may intersect the edge from $( x_0 , y_0 , z_0 )$ to $( x_1 , y_1, z_1 )$ (i.e., $u = 0$).
Calculate $$t_u = \frac{ u_0 }{ u_0 - u_1 }$$ If $$0 \le t_u \le 1$$ and $$0 \le ( 1 - t_u ) v_0 + t_u v_1 \le 1$$ there is an intersection between the line segment and this edge, at $t_u$.
If $v_0 \le 0$ and $v_1 \gt 0$, or if $v_0 \ge 0$ and $v_1 \lt 0$, the line segment may intersect the edge from $( x_0 , y_0 , z_0 )$ to $( x_2 , y_2, z_2 )$ (i.e., $v = 0$).
Calculate $$t_v = \frac{ v_0 }{ v_0 - v_1 }$$ If $$0 \le t_v \le 1$$ and $$0 \le ( 1 - t_v ) u_0 + t_v u_1 \le 1$$ there is an intersection between the line segment and this edge, at $t_v$.
If $1 - u_0 - v_0 \ge u_1 - u_0 + v_1 - v_0 \gt 0$, or if $u_0 + v_0 - 1 \ge u_0 - u_1 + v_0 - v_1 \gt 0$, the line segment may intersect the edge from $( x_1 , y_1 , z_1 )$ to $( x_2 , y_2 , z_2 )$ (i.e., $u + v = 1$).
Calculate $$t_{uv} = \frac{1 - u_0 - v_0}{u_1 - u_0 + v_1 - v_0}$$ If $$0 \le t_{uv} \le 1$$ and $$0 \le ( 1 - t_{uv} ) u_0 + t_{uv} u_1 \le 1$$ and $$0 \le ( 1 - t_{uv} ) v_0 + t_{uv} v_1 \le 1$$ there is an intersection between the line segment and this edge, at $t_{uv}$.
Each part and step above are written in a form that should ensure numerical stability. For example, $(1 - t) u_0 + t u_1 = u_0 + t (u_1 - u_0)$, but the left side yields exactly $u_1$ at $t = 1$, whereas the right side may differ due to domain cancellation, if $u_1$ and $u_0$ differ a lot in magnitude.