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 firstname.lastname@example.org. 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 1 (Cohen–Sutherland Algorithm)
- Point Clipping Algorithm in Computer Graphics
- Apothem of a n-sided regular polygon
- Area of a polygon with given n ordered vertices
- Check if it is possible to create a polygon with a given angle
- Minimum Cost Polygon Triangulation
- Minimum area of a Polygon with three points given
- Check if it is possible to create a polygon with given n sides
- Regular polygon using only 1s in a binary numbered circle
- Program to find the Perimeter of a Regular Polygon
- Scan-line Polygon filling using OPENGL in C
- Length of Diagonal of a n-sided regular polygon
- Area of a n-sided regular polygon with given Radius
- How to check if a given point lies inside or outside a polygon?
- Program to find the Circumcircle of any regular polygon