<p>I think it depends on what your goal is and what kind of initial data you are provided with. I don't think that dividing by the forth coordinate solves a clipping task. What it does is switching of the coordinates of your objects from 3D projective (four components) to 3D Euclidean (three components).</p>

<p>I would say, the basic component you need is a procedure of calculating the intersection point in projective coordinates of a line and a plane. </p>

<p>An arbitrary point in projective three space is written as a four-vector column:
<span class="math-container">$$X = \begin{bmatrix}X_1\\X_2\\X_3\\x_4\end{bmatrix}$$</span> and a plane in projective three space can be expressed as 
<span class="math-container">$$N^T X = 0$$</span> where
<span class="math-container">$$N^T X = \begin{bmatrix}N_1 \,\, N_2 \,\, N_3\,\, N_4\end{bmatrix} \begin{bmatrix}X_1\\X_2\\X_3\\x_4\end{bmatrix} = N_1X_1 + N_2X_2 + N_3X_3 + N_4X_4$$</span>
A line in projective three space is traditionally given by a pair of points 
<span class="math-container">$$A = \begin{bmatrix}A_1\\A_2\\A_3\\A_4\end{bmatrix} \,\, \text{ and }\,\, 
B = \begin{bmatrix}B_1\\B_2\\B_3\\B_4\end{bmatrix}$$</span> and any point <span class="math-container">$X$</span> on the line is expressed as
<span class="math-container">$$X = t_1 A + t_2 B$$</span> for two real numbers <span class="math-container">$t_1, \, t_2$</span> not equal to zero simultaneously. Tho find the intersection point of the line <span class="math-container">$X = t_1 A + t_2 B$</span> with the plane <span class="math-container">$N^TX = 0$</span> we simply plug 
<span class="math-container">$$0 = N^TX = N^T \big(t_1 A + t_2 B \big) = t_1(N^TA) + t_2 (N^TB)$$</span> and since <span class="math-container">$t_1, \, t_2$</span> are determined uniquely up to a common factor, a solution to the equation 
<span class="math-container">$$t_1(N^TA) + t_2 (N^TB) = 0$$</span> is <span class="math-container">$t_1 = -\, (N^TB)$</span> and <span class="math-container">$t_2 = (N^TA)$</span>. </p>

<p>Therefore, given a plane <span class="math-container">$N^TX = 0$</span> and a line determined by a pair of points <span class="math-container">$A$</span> and <span class="math-container">$B$</span>, the unique intersection point of the line with the plane is calculated as
<span class="math-container">$$P = - (N^TB)\, A + (N^TA)\, B$$</span> We us projective coordinates, because it may happen that one of the points on the line, say <span class="math-container">$B$</span> is "at infinity" with respect to our perferred choice of Cartesian coordinates. That means that 
<span class="math-container">$B = \begin{bmatrix}B_1\\B_2\\B_3\\0\end{bmatrix}$</span>. In this case, the line is interpreted in Cartesian coordinates as the line passing through the 3D Cartesian point <span class="math-container">$A = \begin{bmatrix}\frac{A_1}{A_4}\\\frac{A_2}{A_4}\\\frac{A_3}{A_4}\end{bmatrix}$</span> and aligned with the 3D vector <span class="math-container">$\vec{V}_B = \begin{bmatrix}B_1\\B_2\\B_3\end{bmatrix}$</span>.  </p>

<p>In order to clip a line, you can go about it in two different ways, both of which can use the formula for the intersection between a line and a plane. You can find the (projective) equations of the planes of the frustum, intersect the 3d line with them and then project the points onto the screen, which is again intersection of a plane with lines passing through the origin. Alternatively, you can project the two points that determine the line onto the screen (again you can use the formula for the intersection of the line connecing the origin with any of the two points with the plane of the screen), which allows you to know the projection of the 3D line on the screen, and then find the intersection points of the projected line with the borders of the screen. Maybe the second scenario is simpler?</p>

<p>The screen is parallel to the <span class="math-container">$x,\,y$</span> plane of the 3D Cartesian frame, while the <span class="math-container">$z$</span> axis is perpendicular to the screen and the screen is at a distance <span class="math-container">$l$</span> from the origin of the coordinate system. Therefore, the Cartesian equation of the plane of the screen is <span class="math-container">$z = l$</span>. In projective coordinates, <span class="math-container">$z = \frac{X_3}{X_4}$</span>, which means that <span class="math-container">$\frac{X_3}{X_4} = l$</span>  so the projective equation is <span class="math-container">$X_3 = l\, X_4$</span> and can be also expressed as
<span class="math-container">$$0 = X_3 - l\, X_4 = S^TX = \begin{bmatrix} 0 \,\,\, 0 \,\,\, 1 \,\, - l\end{bmatrix} \begin{bmatrix}X_1\\X_2\\X_3\\x_4\end{bmatrix} $$</span> where 
<span class="math-container">$S = \begin{bmatrix}0 \\ 0 \\ 1 \\ -l\end{bmatrix}$</span>
so if the 3D line is determined by the pair of points <span class="math-container">$A = \begin{bmatrix}A_1\\A_2\\A_3\\A_4\end{bmatrix} $</span> and <span class="math-container">$B = \begin{bmatrix}B_1\\B_2\\B_3\\B_4\end{bmatrix}$</span> Hence, with <span class="math-container">$O = \begin{bmatrix}0\\0\\0\\1\end{bmatrix}$</span>, the projection of <span class="math-container">$A$</span> on the screen is
<span class="math-container">$$\hat{A} =  - (S^TO)\, A + (S^TA)\, O = l\, A + (A_3-l\, A_4)\, O = \begin{bmatrix}l\,A_1\\l\,A_2\\l\,A_3\\l\,A_4\end{bmatrix} + \begin{bmatrix}0\\0\\0\\A_3-l\, A_4\end{bmatrix} = \begin{bmatrix}l\,A_1\\l\,A_2\\l\,A_3\\A_3\end{bmatrix}$$</span>  and the projection of <span class="math-container">$B$</span> on the screen is <span class="math-container">$$\hat{B}= \begin{bmatrix}l\,B_1\\l\,B_2\\l\,B_3\\B_3\end{bmatrix}$$</span> So one can write this projection in  matrix form as follows:</p>

<p><span class="math-container">$$P_{scr} = - (S^TO)\, \text{Id} + O\,S^T$$</span>
because <span class="math-container">$P_{scr}A = \Big(- (S^TO)\, \text{Id} + O\,S^T\Big)\,A = - (S^TO)\,A + O\,(S^TA) = - (S^TO)\,A + (S^TA)\,O = \hat{A}$</span></p>

<p><span class="math-container">\begin{align}
P_{scr} &amp;= - (S^TO)\, \text{Id} + O\,S^T\\
&amp;\\
&amp;= - (-l)\begin{bmatrix}1 &amp; 0 &amp; 0 &amp; 0 \\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\end{bmatrix} + \begin{bmatrix}0\\0\\0\\1\end{bmatrix} 
\begin{bmatrix}0\,\,\, 0\,\,\, 1 \,\,\, - l\end{bmatrix}\\ 
&amp;\\
&amp;= \begin{bmatrix}l &amp; 0 &amp; 0 &amp; 0 \\0 &amp; l &amp; 0 &amp; 0\\0 &amp; 0 &amp; l &amp; 0\\0 &amp; 0 &amp; 0 &amp; l\end{bmatrix} + \begin{bmatrix}0 &amp; 0 &amp; 0 &amp; 0 \\0 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; -l \end{bmatrix}\\
&amp;\\
&amp; = \begin{bmatrix}l &amp; 0 &amp; 0 &amp; 0\\0 &amp; l &amp; 0 &amp; 0\\0 &amp; 0 &amp; l &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\end{bmatrix}
\end{align}</span>
And thus
<span class="math-container">$$\hat{A} = P_{scr}\, A \,\, \text{ and } \,\, 
\hat{B} = P_{scr}\, B$$</span> 
When you have your points projected on the screen plane, you would like to get rid of the <span class="math-container">$z-$</span>coordiante, so you need to remove the third (out of our) component of your projected points, i.e.:
<span class="math-container">$\hat{A} = \begin{bmatrix}l\,A_1\\l\,A_2\\l\,A_3\\A_3\end{bmatrix} \mapsto \begin{bmatrix}l\,A_1\\l\,A_2\\A_3\end{bmatrix}$</span> so if you take the projection from four vectors to three vectors
<span class="math-container">$$P_0 = \begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0\\0 &amp; 1 &amp; 0 &amp; 0\\0 &amp; 0 &amp; 0 &amp; 1\end{bmatrix}$$</span> and muptiply it by  <span class="math-container">$P_{scr}$</span> from the right as <span class="math-container">$P_0P_{scr}$</span>, we can take the projective map from the projective three space to the projective plane of the screen
<span class="math-container">$$P = P_0P_{scr} = \begin{bmatrix} l &amp; 0 &amp; 0 &amp; 0\\0 &amp; l &amp; 0 &amp; 0\\0 &amp; 0 &amp; 1 &amp; 0\end{bmatrix}$$</span> 
So, to recap, given  a line in projective three space, passing through the pair of points <span class="math-container">$A$</span> and <span class="math-container">$B$</span>, their screen images, in projective coordinates of the screen, are
<span class="math-container">$\hat{A} = P\, A$</span> and <span class="math-container">$\hat{B} =P \, B$</span>. The formula for the intersection of a line with a plane in projective three space translates verbatim to the intersection of a line given by a pair of points <span class="math-container">$\hat{A}$</span> and <span class="math-container">$\hat{B}$</span> with another line given as an equation <span class="math-container">$N^T\hat{X} = 0$</span> in the projective plane, where <span class="math-container">$N = \begin{bmatrix} n_1\\n_2\\n_3\end{bmatrix}$</span>, 
<span class="math-container">$$\hat{X}_N = (- N^T\hat{B})\hat{A} + (N^T\hat{A})\hat{B}$$</span> so 
alltogether 
<span class="math-container">$$\hat{X}_N = (- N^TP{B})\,P{A} + (N^TP{A})\,P{B}$$</span> So this is the formula for the projected points of intersection of the three dimensional line with the frustum's boundary. For example,  the right vertical boundary of the screen is given by an <span class="math-container">$N_{right\, vert} = \begin{bmatrix} w\\0\\0\end{bmatrix}$</span> and the bottom horizontal boundary of the screen is given by an <span class="math-container">$N_{bottom\, hor} = \begin{bmatrix} 0\\- h\\0\end{bmatrix}$</span></p>

CC BY-SA 4.0
2019-04-19T22:43:16.630Z
2019-04-24T14:48:28.203Z
2019-04-24T14:48:28.203Z
Futurologist
<p>When doing clipping in homogeneous coordinates, we need to find the coordinates of the intersection of the viewing frustum with the line to be clipped. This can be done easily if we want those coordinates to be in screen space(simply divide by the fourth coordinate). But how can we find those coordinates in 3D space? And do we need to do so in the first place?</p>

<p>Thanks in advance.</p>

CC BY-SA 4.0
2019-04-14T09:20:52.760Z
Dividing by the 4th coordinate would lead to conversion from [homogeneous projective coordinates](https://en.wikipedia.org/wiki/Homogeneous_coordinates) to [Carthesian coordinates](https://en.wikipedia.org/wiki/Cartesian_coordinate_system) in Euclidean space. The clipping test then computes intersection with a plane in Euclidean space, with that plane just happening to be axis-aligned.

If you need to do the same thing in world-space, the only difference is that the plane is no longer axis-aligned, but ends up being the (not-axis-aligned) planes bounding your viewing frustum.
Martin Prazak
<clipping>
3D coordinates of intersection point in clipping for a rasterizer
Andreas
# 3D coordinates of intersection point in clipping for a rasterizer

(Asked by: username_0 on 2019-04-13)

When doing clipping in homogeneous coordinates, we need to find the coordinates of the intersection of the viewing frustum with the line to be clipped. This can be done easily if we want those coordinates to be in screen space(simply divide by the fourth coordinate). But how can we find those coordinates in 3D space? And do we need to do so in the first place?

Thanks in advance.

**Tags:** clipping

> **Comments:**
> **username_1**: Dividing by the 4th coordinate would lead to conversion from [homogeneous projective coordinates](https://en.wikipedia.org/wiki/Homogeneous_coordinates) to [Carthesian coordinates](https://en.wikipedia.org/wiki/Cartesian_coordinate_system) in Euclidean space. The clipping test then computes intersection with a plane in Euclidean space, with that plane just happening to be axis-aligned.  If you need to do the same thing in world-space, the only difference is that the plane is no longer axis-aligned, but ends up being the (not-axis-aligned) planes bounding your viewing frustum.  

---

## 1 answers

### Answer by username_2 on 2019-04-19. Score: 2

I think it depends on what your goal is and what kind of initial data you are provided with. I don't think that dividing by the forth coordinate solves a clipping task. What it does is switching of the coordinates of your objects from 3D projective (four components) to 3D Euclidean (three components).

I would say, the basic component you need is a procedure of calculating the intersection point in projective coordinates of a line and a plane.

An arbitrary point in projective three space is written as a four-vector column:
$$X = \begin{bmatrix}X\_1\\X\_2\\X\_3\\x\_4\end{bmatrix}$$ and a plane in projective three space can be expressed as
$$N^T X = 0$$ where
$$N^T X = \begin{bmatrix}N\_1 \,\, N\_2 \,\, N\_3\,\, N\_4\end{bmatrix} \begin{bmatrix}X\_1\\X\_2\\X\_3\\x\_4\end{bmatrix} = N\_1X\_1 + N\_2X\_2 + N\_3X\_3 + N\_4X\_4$$
A line in projective three space is traditionally given by a pair of points
$$A = \begin{bmatrix}A\_1\\A\_2\\A\_3\\A\_4\end{bmatrix} \,\, \text{ and }\,\,
B = \begin{bmatrix}B\_1\\B\_2\\B\_3\\B\_4\end{bmatrix}$$ and any point $X$ on the line is expressed as
$$X = t\_1 A + t\_2 B$$ for two real numbers $t\_1, \, t\_2$ not equal to zero simultaneously. Tho find the intersection point of the line $X = t\_1 A + t\_2 B$ with the plane $N^TX = 0$ we simply plug
$$0 = N^TX = N^T \big(t\_1 A + t\_2 B \big) = t\_1(N^TA) + t\_2 (N^TB)$$ and since $t\_1, \, t\_2$ are determined uniquely up to a common factor, a solution to the equation
$$t\_1(N^TA) + t\_2 (N^TB) = 0$$ is $t\_1 = -\, (N^TB)$ and $t\_2 = (N^TA)$.

Therefore, given a plane $N^TX = 0$ and a line determined by a pair of points $A$ and $B$, the unique intersection point of the line with the plane is calculated as
$$P = - (N^TB)\, A + (N^TA)\, B$$ We us projective coordinates, because it may happen that one of the points on the line, say $B$ is "at infinity" with respect to our perferred choice of Cartesian coordinates. That means that
$B = \begin{bmatrix}B\_1\\B\_2\\B\_3\\0\end{bmatrix}$. In this case, the line is interpreted in Cartesian coordinates as the line passing through the 3D Cartesian point $A = \begin{bmatrix}\frac{A\_1}{A\_4}\\\frac{A\_2}{A\_4}\\\frac{A\_3}{A\_4}\end{bmatrix}$ and aligned with the 3D vector $\vec{V}\_B = \begin{bmatrix}B\_1\\B\_2\\B\_3\end{bmatrix}$.

In order to clip a line, you can go about it in two different ways, both of which can use the formula for the intersection between a line and a plane. You can find the (projective) equations of the planes of the frustum, intersect the 3d line with them and then project the points onto the screen, which is again intersection of a plane with lines passing through the origin. Alternatively, you can project the two points that determine the line onto the screen (again you can use the formula for the intersection of the line connecing the origin with any of the two points with the plane of the screen), which allows you to know the projection of the 3D line on the screen, and then find the intersection points of the projected line with the borders of the screen. Maybe the second scenario is simpler?

The screen is parallel to the $x,\,y$ plane of the 3D Cartesian frame, while the $z$ axis is perpendicular to the screen and the screen is at a distance $l$ from the origin of the coordinate system. Therefore, the Cartesian equation of the plane of the screen is $z = l$. In projective coordinates, $z = \frac{X\_3}{X\_4}$, which means that $\frac{X\_3}{X\_4} = l$ so the projective equation is $X\_3 = l\, X\_4$ and can be also expressed as
$$0 = X\_3 - l\, X\_4 = S^TX = \begin{bmatrix} 0 \,\,\, 0 \,\,\, 1 \,\, - l\end{bmatrix} \begin{bmatrix}X\_1\\X\_2\\X\_3\\x\_4\end{bmatrix} $$ where
$S = \begin{bmatrix}0 \\ 0 \\ 1 \\ -l\end{bmatrix}$
so if the 3D line is determined by the pair of points $A = \begin{bmatrix}A\_1\\A\_2\\A\_3\\A\_4\end{bmatrix} $ and $B = \begin{bmatrix}B\_1\\B\_2\\B\_3\\B\_4\end{bmatrix}$ Hence, with $O = \begin{bmatrix}0\\0\\0\\1\end{bmatrix}$, the projection of $A$ on the screen is
$$\hat{A} = - (S^TO)\, A + (S^TA)\, O = l\, A + (A\_3-l\, A\_4)\, O = \begin{bmatrix}l\,A\_1\\l\,A\_2\\l\,A\_3\\l\,A\_4\end{bmatrix} + \begin{bmatrix}0\\0\\0\\A\_3-l\, A\_4\end{bmatrix} = \begin{bmatrix}l\,A\_1\\l\,A\_2\\l\,A\_3\\A\_3\end{bmatrix}$$ and the projection of $B$ on the screen is $$\hat{B}= \begin{bmatrix}l\,B\_1\\l\,B\_2\\l\,B\_3\\B\_3\end{bmatrix}$$ So one can write this projection in matrix form as follows:

$$P\_{scr} = - (S^TO)\, \text{Id} + O\,S^T$$
because $P\_{scr}A = \Big(- (S^TO)\, \text{Id} + O\,S^T\Big)\,A = - (S^TO)\,A + O\,(S^TA) = - (S^TO)\,A + (S^TA)\,O = \hat{A}$

\begin{align}
P\_{scr} &= - (S^TO)\, \text{Id} + O\,S^T\\
&\\
&= - (-l)\begin{bmatrix}1 & 0 & 0 & 0 \\0 & 1 & 0 & 0\\0 & 0 & 1 & 0\\0 & 0 & 0 & 1\end{bmatrix} + \begin{bmatrix}0\\0\\0\\1\end{bmatrix}
\begin{bmatrix}0\,\,\, 0\,\,\, 1 \,\,\, - l\end{bmatrix}\\
&\\
&= \begin{bmatrix}l & 0 & 0 & 0 \\0 & l & 0 & 0\\0 & 0 & l & 0\\0 & 0 & 0 & l\end{bmatrix} + \begin{bmatrix}0 & 0 & 0 & 0 \\0 & 0 & 0 & 0\\0 & 0 & 0 & 0\\0 & 0 & 1 & -l \end{bmatrix}\\
&\\
& = \begin{bmatrix}l & 0 & 0 & 0\\0 & l & 0 & 0\\0 & 0 & l & 0\\0 & 0 & 1 & 0\end{bmatrix}
\end{align}
And thus
$$\hat{A} = P\_{scr}\, A \,\, \text{ and } \,\,
\hat{B} = P\_{scr}\, B$$
When you have your points projected on the screen plane, you would like to get rid of the $z-$coordiante, so you need to remove the third (out of our) component of your projected points, i.e.:
$\hat{A} = \begin{bmatrix}l\,A\_1\\l\,A\_2\\l\,A\_3\\A\_3\end{bmatrix} \mapsto \begin{bmatrix}l\,A\_1\\l\,A\_2\\A\_3\end{bmatrix}$ so if you take the projection from four vectors to three vectors
$$P\_0 = \begin{bmatrix} 1 & 0 & 0 & 0\\0 & 1 & 0 & 0\\0 & 0 & 0 & 1\end{bmatrix}$$ and muptiply it by $P\_{scr}$ from the right as $P\_0P\_{scr}$, we can take the projective map from the projective three space to the projective plane of the screen
$$P = P\_0P\_{scr} = \begin{bmatrix} l & 0 & 0 & 0\\0 & l & 0 & 0\\0 & 0 & 1 & 0\end{bmatrix}$$
So, to recap, given a line in projective three space, passing through the pair of points $A$ and $B$, their screen images, in projective coordinates of the screen, are
$\hat{A} = P\, A$ and $\hat{B} =P \, B$. The formula for the intersection of a line with a plane in projective three space translates verbatim to the intersection of a line given by a pair of points $\hat{A}$ and $\hat{B}$ with another line given as an equation $N^T\hat{X} = 0$ in the projective plane, where $N = \begin{bmatrix} n\_1\\n\_2\\n\_3\end{bmatrix}$,
$$\hat{X}\_N = (- N^T\hat{B})\hat{A} + (N^T\hat{A})\hat{B}$$ so
alltogether
$$\hat{X}\_N = (- N^TP{B})\,P{A} + (N^TP{A})\,P{B}$$ So this is the formula for the projected points of intersection of the three dimensional line with the frustum's boundary. For example, the right vertical boundary of the screen is given by an $N\_{right\, vert} = \begin{bmatrix} w\\0\\0\end{bmatrix}$ and the bottom horizontal boundary of the screen is given by an $N\_{bottom\, hor} = \begin{bmatrix} 0\\- h\\0\end{bmatrix}$


---

