# Calculate point of intersection line of two planes

I found some source code that I do not really understand. I will give some pseudo-code in my description to give you a better idea how the algorithm works. Basically, two planes with three vertices each are taken into account and the line of intersection for the two planes is calculated. The goal is to calculate the direction vector and the origin point of the intersection line.

The two planes are described as follows:

plane1 = {(0.0, 0.5, 0.8), (0.0, -0.5, 0.8), (0, 0, 0))
plane2 = {(0.5, -0.5, 0.5), (0.5, 0.5, 0.5), (-0.5, -0.5, 0.5)}


If you draw these two triangles, you will notice that they both intersect. Now I want to calculate the line of intersection.

First I calculate the two normal vectors for both planes, I call them n1 and n2. Then I calculate the direction of the intersection line by using the cross-product of the two normal vectors:

direction = n1 X n2


The direction vector is 0, -1, 0 since the intersection line goes along the negative y-axis.

So far so good. Next, I use the plane equation (ax + by +cz = -d) to calculate the distance from the two points of each plane to the coordinate origin:

d1 = -(n1.x * plane1.v1.x + n1.y * plane1.v1.y + n1.z * plane1.v1.z)
d2 = -(n2.x * plane2.v2.x + n2.y * plane2.v2.y + n2.z * plane2.v2.z)


So n1.x, n1.y and n1.z are the a, b and c components of the normal-vector n1. This is analog to n2.

And now comes the part that I do not understand. Given is the following source-code:

        if(Math.abs(direction.x)>0)
{
point.x = 0;
point.y = (d2*normalFace1.z - d1*normalFace2.z)/direction.x;
point.z = (d1*normalFace2.y - d2*normalFace1.y)/direction.x;
}
else if(Math.abs(direction.y)>0)
{
point.x = (d1*normalFace2.z - d2*normalFace1.z)/direction.y;
point.y = 0;
point.z = (d2*normalFace1.x - d1*normalFace2.x)/direction.y;
}
else
{
point.x = (d2*normalFace1.y - d1*normalFace2.y)/direction.z;
point.y = (d1*normalFace2.x - d2*normalFace1.x)/direction.z;
point.z = 0;
}


Here, the origin point of the intersection line is calculated, but I do not understand how it is done. Why is x, y or z set to zero if the corresponding x, y or z values of direction is greater 0? Where does the formulas inside of the if-statements come from?

Thanks for your help!

If the line is not parallel to $yz$ plane (abs(direction.x) not equal to $0$), it has to cross $yz$ plane at one point. At that point, $x=0$. Since the line is the intersection of the two planes, the $y,z$ values satisfy: $$b_1y+c_1z=-d_1\\ b_2y+c_2z=-d_2$$
We then find the $y,z$ value by solving this system of linear equations. You can apply Cramer's rule to get $$y=\frac{\det\begin{pmatrix}-d_1&c_1\\-d_2&c_2\end{pmatrix}}{\det\begin{pmatrix}b_1&c_1\\b_2&c_2\end{pmatrix}}$$