# Line Clipping in 3D: Overview, Techniques and Algorithms
## 5 The Proposed Method
### Description  
The first step of the algorithm checks, if both of the endpoints are outside the clipping region and at the same time in the same region (left, right, bottom, top, near, far). If one of the following occurs, then the line is being rejected and the algorithm draws nothing; see examples of Figure 15:  
\(x_{0}<x_{min}\) AND \(x_{1}<x_{min}\) (the line is left of the clipping region);  
\(x_{0}>x_{max}\) AND \(x_{1}>x_{max}\) (the line is right of the clipping region);  
\(y_{0}<y_{min}\) AND \(y_{1}<y_{min}\) (the line is under the clipping region);  
\(y_{0}>y_{max}\) AND \(y_{1}>y_{max}\) (the line is above the clipping region);  
\(z_{0}<z_{min}\) AND \(z_{1}<z_{min}\) (the line is behind the clipping region);  
\(z_{0}>z_{max}\) AND \(z_{1}>z_{max}\) (the line is in front of the clipping region).  
In the second step, the algorithm compares the coordinates of the two endpoints of the line along with the boundaries of the clipping region. It compares the \(x_{0}\) and \(x_{1}\) coordinates with the \(x_{min}\) and \(x_{max}\) boundaries, respectively, then it compares the \(y_{0}\) and \(y_{1}\) coordinates with the \(y_{min}\) and \(y_{max}\) boundaries and, finally, it compares the \(z_{0}\) and \(z_{1}\) coordinates with the \(z_{min}\) and \(z_{max}\) boundaries. If any of these coordinates are outside the clipping boundary, then the boundary coordinates are used instead for limiting the line inside the volume and achieving clipping (see Figure 16).  
Figure 14: Line \(L\) in three-dimensional space.  
Figure 15: Rejected lines: line A is left of the clipping region, line B is right of the clipping region, line C is in front of the clipping region, and line D is on the back of the clipping region.  
For each of the coordinates of the two endpoints, and according to Equations (2)-(5), the comparisons and the new coordinates are as follows:  
* If \(x_{i}<x_{min}\), then \[\frac{x_{min}-x_{0}}{a}=\frac{y-y_{0}}{b}\Rightarrow y=\frac{b}{a}\cdot(x_{min} -x_{0})+y_{0};\] \[\frac{x_{min}-x_{0}}{a}=\frac{z-z_{0}}{c}\Rightarrow z=\frac{c}{a}\cdot(x_{ min}-x_{0})+z_{0}.\]
* If \(x_{i}>x_{max}\), then \[\frac{x_{max}-x_{0}}{a}=\frac{y-y_{0}}{b}\Rightarrow y=\frac{b}{a}\cdot(x_{ max}-x_{0})+y_{0};\] \[\frac{x_{max}-x_{0}}{a}=\frac{z-z_{0}}{c}\Rightarrow z=\frac{c}{a}\cdot(x_{ max}-x_{0})+z_{0}.\]
* If \(y_{i}<y_{min}\), then \[\frac{y_{min}-y_{0}}{b}=\frac{x-x_{0}}{a}\Rightarrow x=\frac{a}{b}\cdot(y_{ min}-y_{0})+x_{0};\] \[\frac{y_{min}-y_{0}}{b}=\frac{z-z_{0}}{c}\Rightarrow z=\frac{c}{b}\cdot(y_{ min}-y_{0})+z_{0}.\]
* If \(y_{i}>y_{max}\), then \[\frac{y_{max}-y_{0}}{b}=\frac{x-x_{0}}{a}\Rightarrow x=\frac{a}{b}\cdot(y_{ max}-y_{0})+x_{0};\] \[\frac{y_{max}-y_{0}}{b}=\frac{z-z_{0}}{c}\Rightarrow z=\frac{c}{b}\cdot(y_{ max}-y_{0})+z_{0}.\]
* If \(z_{i}<z_{min}\), then \[\frac{z_{min}-z_{0}}{c}=\frac{x-x_{0}}{a}\Rightarrow x=\frac{a}{c}\cdot(z_{ min}-z_{0})+x_{0};\] \[\frac{z_{min}-z_{0}}{c}=\frac{y-y_{0}}{b}\Rightarrow y=\frac{b}{c}\cdot(z_{ min}-z_{0})+y_{0}.\]
* If \(z_{i}>z_{max}\), then \[\frac{z_{max}-z_{0}}{c}=\frac{x-x_{0}}{a}\Rightarrow x=\frac{a}{c}\cdot(z_{ max}-z_{0})+x_{0};\] \[\frac{z_{max}-z_{0}}{c}=\frac{y-y_{0}}{b}\Rightarrow y=\frac{b}{c}\cdot(z_{ max}-z_{0})+y_{0}.\]  
In the above we have that \(i\) vary from 0 to 1. In the third and final step, the algorithm draws the line between the two endpoints.  
Figure 16: Using the boundary coordinates for the endpoints of the line.