Line Clipping in 2D: Overview, Techniques and Algorithms
Abstract
:1. Introduction
2. Fundamental Line-Clipping Algorithms
2.1. Cohen–Sutherland
2.2. Cyrus–Beck
2.3. Liang–Barsky
- −
- If the line has then it is parallel to the corresponding clipping-window edge.
- −
- If the line has and then it is completely outside and is being rejected.
- −
- If , the line proceeds from the inside to the outside.
- −
- If , the line proceeds from the outside to the inside.
2.4. Nicholl–Lee–Nicholl
- 90° clockwise rotation about the origin.
- 180° clockwise rotation about the origin.
- 270° clockwise rotation about the origin.
- Reflection about the line .
- Reflection about the axis.
- is inside the clipping window and outside.The algorithm sets up four regions (L, T, R, B) as in Figure 12. Then, depending on which one of the four regions contains , it computes the line-intersection position with the corresponding window boundary.
- is on the edge region and is outside the clipping window.The algorithm sets up four regions labeled L, LT, LR, and LB as in Figure 13. These four regions again determine a unique clipping-window edge for the line segment, relative to the position of . For instance, if is in any one of the three regions labeled L, the algorithm clips the line at the left window border and draws the line segment from this intersection point to . If is in region LT, it draws the line segment from the left window boundary to the top boundary. Likewise, the same logic applies to regions LR and LB. However, if is not in any of these four regions, the line is clipped entirely.
- is on the corner region and is outside the clipping window.When is to the corner region, the algorithm uses one of the two sets as shown in Figure 14. The selection of (a) or (b) depends on the position of within the corner region. When is closer to the left clipping boundary of the window, the algorithm uses the regions in (a) of this figure but when is closer to the top clipping boundary of the window, it uses the regions in (b). If is in one of the regions T, L, TR, TB, LR, or LB, this determines a unique clipping-window border for the intersection calculations, otherwise, the entire line is rejected.
- is above the clipping window.
- is on the right of the clipping window.
- is below the clipping window.
- is on the left of the clipping window.
3. Common Line-Clipping Algorithms
3.1. Midpoint Subdivision
- Totally visible: If the outcode of both line segment endpoints is 0000 then the line segment is inside the clipping window and it is completely visible.
- Totally invisible: Bitwise AND between the two outcodes of the line segment endpoints. If the result is not 0000 then the line endpoints share the same region and the line segment does not cross the clipping window so it is rejected.
- Clipping candidate: If the line is in neither Category 1 nor Category 2 then it is partially visible and has to be subdivided into two equal parts. The visibility tests are then applied to each half. This subdivision process is repeated until we obtain completely visible and completely invisible line segments.
3.2. Skala 2005
Algorithm 1: Function to determine the outcode of the endpoints. |
function CODE(x); begin c := [0000]; if x < thenc := [1000] else if x > thenc := [0100]; if y < thenc := c lor [1001] else if y > thenc := c lor [0010]; CODE := c end [CODE]; |
3.3. S-Clip E2
4. Uncommon Line-Clipping Algorithms
4.1. Kodituwakku–Wijeweere–Chamikara
4.2. Matthes–Drakopoulos Line Clipping against a Rectangular Window
- Step 1
AND | (line is left to the clipping window) |
AND | (line is right to the clipping window) |
AND | (line is under the clipping window) |
AND | (line is over the clipping window) |
- Step 2
- If then
- If then
- If then
- If then
- Step 3
5. Evaluation of All Line-Clipping Algorithms against a Rectangular Window
6. Line Clipping vs. Polygon Clipping
7. Matthes–Drakopoulos Line Clipping against a Convex Polygon
7.1. Mathematical Background: The Cross Product
- Has zero length, when the vectors and are in the same or the opposite direction.
- It reaches the maximum length when the vectors and are at right angles.
- The direction changes depending on the angle of vectors and ; see Figure 32.
7.2. Further Analysis of the Cross Product
- The 2D cross product of point I with the line segment is zero.
- The 2D cross product of point I with the line segment is zero.
7.3. Description of the Algorithm
7.4. Analysis
- Simultaneously right to the edge.
- One of them is on the edge and the other is right of the edge.
7.5. The Steps of the Algorithm
- Check against an edge of the polygon which is defined by the vertices and where the points and of the line reside. Each point may reside to the left of the edge, to the right of the edge or on the edge.
- If both of the points are to the left of the edge then stop the algorithm and draw nothing. The line is completely outside the convex polygon.
- If only point A is to the left of the edge then calculate the intersection point between the edge and the line. Replace the coordinates of point A with those coordinates of the intersection point and repeat from Step 1 with the next edge.
- If only point B is to the left of the edge then calculate the intersection point between the edge and the line. Replace the coordinates of point B with those coordinates of the intersection point and repeat from Step 1 with the next edge.
- If both points A and B are to the right of the edge or one of them is on the edge and the other is right of the edge then repeat from Step 1 with the next edge or stop if you have checked all N edges.
- Draw the clipped line from point A to point B.
7.6. Pseudo-Code (C++ Based)
8. Evaluation of All Line-Clipping against a Convex Polygon Algorithms
9. Summary
Author Contributions
Funding
Institutional Review Board Statement
Informed Consent Statement
Data Availability Statement
Conflicts of Interest
References
- Hearn, D.; Baker, M.P. Computer Graphics C Version, 2nd ed.; Prentice Hall: Upper Saddle River, NJ, USA, 1997. [Google Scholar]
- Comninos, P. Mathematical and Computer Programming Techniques for Computer Graphics; Springer: London, UK, 2006. [Google Scholar] [CrossRef]
- Cyrus, M.; Beck, J. Generalized two- and three-dimensional clipping. Comput. Graph. 1978, 3, 23–28. [Google Scholar] [CrossRef]
- Liang, Y.D.; Barsky, B.A. A new concept and method for line clipping. ACM Trans. Graph. (TOG) 1984, 3, 1–22. [Google Scholar] [CrossRef]
- Nicholl, T.M.; Lee, D.; Nicholl, R.A. An efficient new algorithm for 2-D line clipping: Its development and analysis. Comput. Graph. 1987, 21, 253–262. [Google Scholar] [CrossRef]
- Sobkow, M.S.; Pospisil, P.; Yang, Y. A Fast Two-Dimensional Line Clipping Algorithm Via Line Encoding. Comput. Graph. 1987, 11, 459–467. [Google Scholar] [CrossRef]
- Skala, V. An efficient algorithm for line clipping by convex polygon. Comput. Graph. 1993, 17, 417–421. [Google Scholar] [CrossRef]
- Skala, V. O(lgN) Line clipping algorithm in E2. Comput. Graph. 1994, 18, 517–524. [Google Scholar] [CrossRef] [Green Version]
- Skala, V. A new approach to line and line segment clipping in homogeneous coordinates. Vis. Comput. 2005, 21, 905–914. [Google Scholar] [CrossRef]
- Skala, V. S-clip E2: A new concept of clipping algorithms. SIGGRAPH Asia 2012, 2012, 39. [Google Scholar] [CrossRef]
- Ray, B.K. A Line Segment Clipping Algorithm in 2D. Int. J. Comput. Graph. 2012, 3, 51–76. [Google Scholar]
- Andreev, R.; Sofianska, E. New algorithm for two-dimensional line clipping. Comput. Graph. 1991, 15, 519–526. [Google Scholar] [CrossRef]
- Day, J.D. An algorithm for clipping lines in object and image space. Comput. Graph. 1992, 16, 421–426. [Google Scholar] [CrossRef]
- Rappoport, A. An efficient algorithm for line and polygon clipping. Vis. Comput. 1991, 7, 19–28. [Google Scholar] [CrossRef]
- Dimri, S.C. A simple and efficient algorithm for line and polygon clipping in 2-D computer graphics. Int. J. Comput. Appl. 2015, 127, 31–34. [Google Scholar]
- Huang, W. A Novel Algorithm for Line Clipping. In Proceedings of the 2009 International Conference on Computational Intelligence and Software Engineering, Wuhan, China, 11–13 December 2009; pp. 1–5. [Google Scholar] [CrossRef]
- Huang, W. The Line Clipping Algorithm Basing on Affine Transformation. Intell. Inf. Manag. 2010, 2, 2029. [Google Scholar] [CrossRef] [Green Version]
- Elliriki, M.; Reddy, C.; Anand, K. An efficient line clipping algorithm in 2D space. Int. Arab J. Inf. Technol. 2019, 16, 798–807. [Google Scholar]
- Pandey, A.; Jain, S. Comparison of various line clipping algorithms for Improvement. Int. J. Mod. Eng. Res. 2013, 3, 69–74. [Google Scholar]
- Shilpa; Arora, P. Clipping in Computer Graphics-A review. In International Journal Of Advance Research And Innovative Ideas In Education; IJARIIE: Gujarat, India, 2016; Volume 2, pp. 1725–1730. [Google Scholar]
- Nisha. Comparison of various line clipping algorithms: Review. Int. J. Adv. Res. Comput. Sci. Softw. Eng. 2017, 7, 68–71. [Google Scholar] [CrossRef]
- Cohen, D. Incremental Methods for Computer Graphics. Ph.D. Thesis, Harvard University, Cambridge, MA, USA, 1969. [Google Scholar]
- Sproull, R.; Sutherland, I. A clipping divider. In Proceedings of the Fall Joint Computer Conference, Part I, AFIPS ’68 (Fall, Part I), New York, NY, USA, 9–11 December 1968; Association for Computing Machinery: New York, NY, USA, 1968; pp. 765–775. [Google Scholar] [CrossRef]
- Sutherland, I. Display Windowing by Clipping. U.S. Patent 3 639 736, 1 February 1972. [Google Scholar]
- Hearn, D.; Baker, M.P.; Carithers, W.R. Computer Graphics with Open GL, 4th ed.; Pearson Education Limited: Edinburgh Gate, UK; Harlow, UK; Essex, UK, 2014. [Google Scholar]
- Kodituwakku, S.R.; Wijeweera, K.R.; Chamikara, M.A.P. An efficient algorithm for line clipping in computer graphics programming. Ceylon J. Sci. (Phys. Sci.) 2013, 1, 1–7. [Google Scholar]
- Matthes, D.; Drakopoulos, V. A simple and fast line-clipping method as a Scratch extension for computer graphics education. Comput. Sci. Inf. Technol. 2019, 7, 40–47. [Google Scholar] [CrossRef] [Green Version]
- Matthes, D.; Drakopoulos, V. Another simple but faster method for 2D line clipping. Int. J. Comput. Graph. Animat. 2019, 9, 1–15. [Google Scholar] [CrossRef]
- Rogers, D.F. Procedural Elements for Computer Graphics, 2nd ed.; McGraw-Hill, Inc.: New York, NY, USA, 1997. [Google Scholar] [CrossRef]
- Weiler, K.; Atherton, P. Hidden Surface Removal Using Polygon Area Sorting. In Proceedings of the 4th Annual Conference on Computer Graphics and Interactive Techniques, SIGGRAPH ’77, San Jose, CA, USA, 20–22 July 1977; Association for Computing Machinery: New York, NY, USA, 1977; pp. 214–222. [Google Scholar] [CrossRef]
- Sutherland, I.; Hodgman, G. Reentrant Polygon Clipping. Commun. ACM 1974, 17, 32–42. [Google Scholar] [CrossRef]
- Greiner, G.; Hormann, K. Efficient clipping of arbitrary polygons. ACM Trans. Graph. 1998, 17, 71–83. [Google Scholar] [CrossRef]
- Vatti, B.R. A generic solution to polygon clipping. Commun. ACM 1992, 35, 56–63. [Google Scholar] [CrossRef]
c | TAB1 | TAB2 | MASK | ||||
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | None | None | None |
1 | 0 | 0 | 0 | 1 | 0 | 3 | 0100 |
2 | 0 | 0 | 1 | 0 | 0 | 1 | 0100 |
3 | 0 | 0 | 1 | 1 | 1 | 3 | 0010 |
4 | 0 | 1 | 0 | 0 | 1 | 2 | 0010 |
5 | 0 | 1 | 0 | 1 | N/A | N/A | N/A |
6 | 0 | 1 | 1 | 0 | 0 | 2 | 0100 |
7 | 0 | 1 | 1 | 1 | 2 | 3 | 1000 |
8 | 1 | 0 | 0 | 0 | 2 | 3 | 1000 |
9 | 1 | 0 | 0 | 1 | 0 | 2 | 0100 |
10 | 1 | 0 | 1 | 0 | N/A | N/A | N/A |
11 | 1 | 0 | 1 | 1 | 1 | 2 | 0010 |
12 | 1 | 1 | 0 | 0 | 1 | 3 | 0010 |
13 | 1 | 1 | 0 | 1 | 0 | 1 | 0100 |
14 | 1 | 1 | 1 | 0 | 0 | 3 | 0100 |
15 | 1 | 1 | 1 | 1 | None | None | None |
Exec. | CS | CB | LB | MS | NLN | SKA05 | SCE2 | KWC | MD |
---|---|---|---|---|---|---|---|---|---|
(sec) | (sec) | (sec) | (sec) | (sec) | (sec) | (sec) | (sec) | (sec) | |
1 | 1.883 | 2.096 | 1.840 | 2.176 | 1.853 | 1.985 | 2.100 | 1.868 | 1.758 |
2 | 1.905 | 2.075 | 1.870 | 2.206 | 1.989 | 1.959 | 2.173 | 1.774 | 1.733 |
3 | 1.914 | 2.069 | 1.871 | 2.177 | 1.873 | 1.986 | 2.160 | 1.870 | 1.765 |
4 | 1.934 | 2.114 | 1.903 | 2.144 | 1.847 | 1.984 | 2.162 | 1.803 | 1.764 |
5 | 1.857 | 2.136 | 1.851 | 2.145 | 1.892 | 1.965 | 2.100 | 1.859 | 1.776 |
6 | 1.817 | 2.085 | 1.869 | 2.174 | 1.838 | 1.994 | 2.132 | 1.825 | 1.814 |
7 | 1.918 | 2.082 | 1.847 | 2.190 | 1.869 | 1.987 | 2.170 | 1.768 | 1.787 |
8 | 1.836 | 2.093 | 1.832 | 2.211 | 1.814 | 2.005 | 2.149 | 1.810 | 1.769 |
9 | 1.820 | 2.136 | 1.921 | 2.175 | 1.805 | 1.971 | 2.144 | 1.828 | 1.757 |
10 | 1.944 | 2.082 | 1.859 | 2.210 | 1.816 | 1.941 | 2.167 | 1.743 | 1.806 |
Avg: | 1.883 | 2.097 | 1.866 | 2.181 | 1.860 | 1.978 | 2.146 | 1.815 | 1.773 |
The MD Algorithm Is % Faster Compared to | |||||||
---|---|---|---|---|---|---|---|
CS | CB | LB | MS | NLN | SKA05 | SCE2 | KWC |
6.20% | 18.27% | 5.27% | 23.01% | 4.89% | 11.55% | 21.03% | 2.36% |
Number of Edges | Cyrus–Beck (sec) | Skala 2005 (sec) | S-Clip E2 (sec) | Matthes–Drakopoulos (sec) |
---|---|---|---|---|
5 | 2.443 | 2.443 | 2.425 | 2.358 |
6 | 2.657 | 2.655 | 2.564 | 2.501 |
7 | 2.743 | 2.682 | 2.648 | 2.590 |
8 | 2.860 | 2.859 | 2.686 | 2.632 |
9 | 2.938 | 2.826 | 2.820 | 2.774 |
10 | 3.139 | 3.104 | 2.949 | 2.884 |
Edges | MD Algorithm Is Faster Compared to | ||
---|---|---|---|
Cyrus-Beck | Skala 2005 | S-Clip E2 | |
5 | 3.58% | 3.61% | 2.85% |
6 | 6.21% | 6.16% | 2.50% |
7 | 5.92% | 3.54% | 2.25% |
8 | 8.65% | 8.62% | 2.07% |
9 | 5.90% | 1.87% | 1.65% |
10 | 8.84% | 7.65% | 2.26% |
Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations. |
© 2022 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Matthes, D.; Drakopoulos, V. Line Clipping in 2D: Overview, Techniques and Algorithms. J. Imaging 2022, 8, 286. https://doi.org/10.3390/jimaging8100286
Matthes D, Drakopoulos V. Line Clipping in 2D: Overview, Techniques and Algorithms. Journal of Imaging. 2022; 8(10):286. https://doi.org/10.3390/jimaging8100286
Chicago/Turabian StyleMatthes, Dimitrios, and Vasileios Drakopoulos. 2022. "Line Clipping in 2D: Overview, Techniques and Algorithms" Journal of Imaging 8, no. 10: 286. https://doi.org/10.3390/jimaging8100286
APA StyleMatthes, D., & Drakopoulos, V. (2022). Line Clipping in 2D: Overview, Techniques and Algorithms. Journal of Imaging, 8(10), 286. https://doi.org/10.3390/jimaging8100286