   SEARCH HOME Math Central Quandaries & Queries  Question from RJ: Well hello, Let's say I have a triangle, Not any ordinary triangle, but a triangle that is represented in 3 dimensions. I also have a line segment. Lets say that this triangle has points A(0,0,0), B(1,0,0), C(0,1,1) And the line segment has points D(0,1/2,0), E(1,1/2,1) In what way can I find if the line segment and triangle intersect? Their intersection point? I don't prefer matrices, because they don't make any sense, but ... if that's the only way to solve this, then do what's necessary. Thank you in advance. Hi,

I am going to change your points $A, B, C, D$ and $E$ to $A(0,1,2), B(1,0,1), C(2,1,1), D(0,1/2,0)$ and $E(1,1,2)$ to make the problem slightly more interesting. With these coordinated a vector in the direction from $D$ to $E$ is $(1, 1/2, 2)$. Thus the equation of the line containing $D$ and $E$ is

$x = t, y = \frac12 + \frac12 t, z = 2t$

Notice that a point on the line is on the line segment from $D$ to $E$ if and only if $0 \leq t \leq 1.$

Now I need the equation of the plane containing the triangle $ABC.$ The vector from $A$ to $B$ is $\vec{u} = (1,-1,-1)$ and the vector from $A$ to $C$ is $\vec{v} = (2,1,0).$ The cross product of $\vec{u}$ and $\vec{v}$ is a vector $\vec{n} = (1,1,2)$ which is normal to the plane containing $A, B,$ and $C.$ Hence the equation of the plane containing $A,B$ and $C$ is

$1(x-0) +1(y-1) + 2(z-2) = 0 \mbox{ or } x +y +2z = 5.$

To determine the point $P$ where the line meets the plane substitute the equation of the line into the equation of the plane and solve for $t.$ I got $t = \large \frac{9}{10}$ and the point $P$ has coordinates $(\large \frac{18}{20}, \frac{19}{20}, \frac{36}{20}\normalsize ).$ Since $t$ satisfies $0 \leq t \leq 1$ the line segment intersect the plane containing the triangle. Finally let's determine if $P$ is in the triangle.

To accomplish this I am going to use the vectors $\vec{u}, \vec{v}$ and $\vec{n}$ again. Use these three vectors for a base and write the point $P$ in this basis. That is find $p, q$ and $r$ so that

$p \vec{u} + q \vec{v} + r \vec{n} = P.$

The point $P$ is inside or on the boundary of the triangle if $p,q \geq 0$ and $p+q \leq1.$ (See Wolfram Math World.)

The linear system

$p \vec{u} + q \vec{v} + r \vec{n} = P.$

is

\begin{eqnarray*}
p &+& 2q &+& r &= & \frac{18}{20} \\
-p & & &+& r & = & \frac{19}{20} \\
-p &-& q &+& 2r & =& \frac{36}{20}.
\end{eqnarray*}

Solving this system yields

$p = \frac{1}{10} \mbox{ and } q = \frac{-7}{40}.$

Since $q < 0$ the point $P$ is not inside or on the perimeter of the triangle.

Write back if you need more help,
Harley

RJ wrote back

I realized that in my post I said that I'm not too familiar with matrices
although I have operated slightly with them before, but I very barely
understand anything in the answer given since I am not familiar with 3d
vector stuff.

I asked a similar question to this on Stack Overflow, and here's some code
context of an answer which was given:

abcd = np.array([[0, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 1],
[0, 0.5, 0, 1]])

abce = np.array([[0, 0, 0, 1],
[1, 0, 0, 1],
[0, 1, 1, 1],
[1, 0.5, 1, 1]])
print(np.sign(np.linalg.det(abcd)))print(np.sign(np.linalg.det(abce)))

If whoever is reading this doesn't understand code, *abcd* is a matrix made
up triangle A(0,0,0), B(1,0,0), C(0,1,1), and a point E(1,.5,1) which is an
end of a line segment. *abce* is also a matrix, but the last point is the
other point on the line segment, E(1,.5,1)

The issue with this is that when the code runs, it only works correctly in
assumption that the triangular plane is boundless / extends infinity. The
triangle must have bounds in order to test whether there is a real
intersection.

I've attached the official code i'm testing this answer with. If you decide
to run this, you will need Python 3, and have installed the Numpy library.
Numpy can be installed on windows on command line with pip3 install numpy.

I have tried testing differences between the determinants of abcd and abcd,
but that doesn't make any difference.

And thank you for reading this.

~rj

The code you sent didn't arrive in a readable form and it doesn't matter since I am not a coder. I do have a suggestion however.

On the stackoverflow page you use a technique in two dimensions involving the areas of four triangles to decide if $P$ is inside the triangle $ABC.$ You can use a similar technique in three dimensions if you know the coordinates of $A, B, C$ and $P,$ and $P$ is in the plane containing triangle $ABC.$ First use the formula for the distance $d$ between two points $(x_0, y_0, z_0)$ and $(x_1, y_1, z_1)$ in 3-space

$d = \sqrt{(x_1 - x_0)^2 + (y_1 - y_0)^2 + (z_1 - z_0)^2)}$

to find the lengths of $AB, BC, CA, AP, BP$ and $CP.$ Then use Heron's Formula to calculate the areas triangles $ABC, APB, APC,$ and $BPC$ and check to see if the area of triangle $ABC$ is the sum of the areas of the other three.

The challenge here is to determine if $P$ is on the plane containing the triangle and if so to determine the coordinates of $P.$ The technique I described earlier does work. The question you originally asked is a geometric problem in three space and you are going to have to use some three dimensional geometry to solve it.

Harley     Math Central is supported by the University of Regina and The Pacific Institute for the Mathematical Sciences.