A convex polygon and a convex clipping area are given. The task is to clip polygon edges using the Sutherland–Hodgman Algorithm. Input is in the form of vertices of the polygon in clockwise order.
Input : Polygon : (100,150), (200,250), (300,200) Clipping Area : (150,150), (150,200), (200,200), (200,150) i.e. a Square Output : (150, 162) (150, 200) (200, 200) (200, 174) Example 2 Input : Polygon : (100,150), (200,250), (300,200) Clipping Area : (100,300), (300,300), (200,100) Output : (242, 185) (166, 166) (150, 200) (200, 250) (260, 220)
Overview of the algorithm:
Consider each edge e of clipping Area and do following: a) Clip given polygon against e.
How to clip against an edge of clipping area?
The edge (of clipping area) is extended infinitely to create a boundary and all the vertices are clipped using this boundary. The new list of vertices generated is passed to the next edge of the clip polygon in clockwise fashion until all the edges have been used.
There are four possible cases for any given edge of given polygon against current clipping edge e.
- Both vertices are inside : Only the second vertex is added to the output list
- First vertex is outside while second one is inside : Both the point of intersection of the edge with the clip boundary and the second vertex are added to the output list
- First vertex is inside while second one is outside : Only the point of intersection of the edge with the clip boundary is added to the output list
- Both vertices are outside : No vertices are added to the output list
There are two sub-problems that need to be discussed before implementing the algorithm:-
To decide if a point is inside or outside the clipper polygon
If the vertices of the clipper polygon are given in clockwise order then all the points lying on the right side of the clipper edges are inside that polygon. This can be calculated using :
(150, 162) (150, 200) (200, 200) (200, 174)
This article is contributed by Nabaneet Roy. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Line Clipping | Set 2 (Cyrus Beck Algorithm)
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Point Clipping Algorithm in Computer Graphics
- Check if it is possible to create a polygon with given n sides
- Check if it is possible to create a polygon with a given angle
- Minimum area of a Polygon with three points given
- Area of a polygon with given n ordered vertices
- Minimum Cost Polygon Triangulation
- Apothem of a n-sided regular polygon
- Program to find the Perimeter of a Regular Polygon
- How to check if a given point lies inside or outside a polygon?
- Scan-line Polygon filling using OPENGL in C
- Program to find the Circumcircle of any regular polygon
- Length of Diagonal of a n-sided regular polygon
- Regular polygon using only 1s in a binary numbered circle