# Line Clipping in 3D: Overview, Techniques and Algorithms
## 7 Summary  
Line clipping is a technique that is widely used in computer graphics. The three-dimensional viewing pipeline involves many more complicated stages than its two-dimensional analogue. When a three-dimensional scene is projected onto a two-dimensional plane by using perspective projection, transforming the frustum-shaped view volume into a canonical one (rectangular parallelepiped) is considered good practice and speeds up the process since clipping against a frustum-shaped view volume is computationally expensive. In the present article, an overview of the most common, as well as of the lesser-known, algorithms for clipping a line against a rectangular region in a three-dimensional space was presented. Moreover, a simple and fast algorithm (see Algorithm 1) for clipping a line segment against a rectangular parallelepiped view volume was proposed. The algorithm is straightforward to implement in any programming language.  
Figure 19: Average time of each algorithm when clipping one million lines (lower is better).  
```
voidclip_3d_line(double x[], double y[], double z[], double xmin, double xmax, double symin, double ymax, double zmin, double zmax) { if(((x[0] < xmin && x[1] < xmin) ) || (x[0] > xmax && x[1] > xmax) | |  (y[0] < ymin && y[1] < ymin) || (y[0] > ymax && y[1] > ymax) | |  (z[0] < zmin && z[1] < zmin) || (z[0] > zmax && z[1] > zmax))) {  double a = x[1] - x[0];  double b = y[1] - y[0];  double c = z[1] - z[0];  for(int i = 0; i <= 1; i++) {  if(x[i] < xmin) {  y[i] = b / a * (xmin - x[0]) + y[0];  z[i] = c / a * (xmin - x[0]) + z[0];  x[i] = xmin; } else if(x[i] > xmax) {  y[i] = b / a * (xmax - x[0]) + y[0];  z[i] = c / a * (xmax - x[0]) + z[0];  x[i] = xmax; } if(y[i] < ymin) {  x[i] = a / b * (ymin - y[0]) + x[0];  z[i] = c / b * (ymin - y[0]) + z[0];  y[i] = ymin; } else if(y[i] > ymax) {  x[i] = a / b * (ymax - y[0]) + x[0];  z[i] = c / b * (ymin - y[0]) + z[0];  y[i] = ymin; } else if(y[i] > ymax) {  x[i] = a / b * (ymax - y[0]) + x[0];  z[i] = c / b * (ymax - y[0]) + z[0];  y[i] = ymax; } if(z[i] < zmin) {  x[i] = a / c * (zmin - z[0]) + x[0];  y[i] = b / c * (zmin - z[0]) + y[0];  z[i] = zmin; } else if(z[i] > zmax) {  x[i] = a / c * (zmin - z[0]) + x[0];  y[i] = b / c * (zmin - z[0]) + y[0];  z[i] = zmin; } else if(z[i] > zmax) {  x[i] = a / c * (zmax - z[0]) + x[0];  y[i] = b / c * (zmax - z[0]) + y[0];  z[i] = zmax; } } else if(z[i] > zmax) {  x[i] = a / c * (zmax - z[0]) + x[0];  y[i] = b / c * (zmin - z[0]) + y[0];  z[i] = zmin; } else if(z[i] > zmax) {  x[i] = a / c * (zmax - z[0]) + x[0];  y[i] = b / c * (zmax - z[0]) + y[0];  z[i] = zmax; } } } draw_line(x[0], y[0], z[0], x[1], y[1], z[1]);
```  
**Algorithm 1** The proposed algorithm  
Conceptualization, V.D.; methodology, D.M. and V.D.; software, D.M.; formal analysis, D.M.; data curation, D.M.; writing-original draft preparation, D.M.; writing-review and editing, V.D.; visualization, D.M.; supervision, V.D. All authors have read and agreed to the published version of the manuscript.  
This research received no external funding.  
Not applicable.  
The authors declare no conflict of interest.