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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Point Clipping Algorithm in Computer Graphics
- Line Clipping | Set 2 (Cyrus Beck Algorithm)
- Methods for Text Clipping in Computer Graphics
- How to check if a given point lies inside or outside a polygon?
- Minimum Cost Polygon Triangulation
- Area of a polygon with given n ordered vertices
- Scan-line Polygon filling using OPENGL in C
- Regular polygon using only 1s in a binary numbered circle
- Find number of diagonals in n sided convex polygon
- Check if it is possible to create a polygon with a given angle
- Probability that the pieces of a broken stick form a n sided polygon
- Program to find the Circumcircle of any regular polygon
- Program to find the Perimeter of a Regular Polygon
- Program to find Area of Triangle inscribed in N-sided Regular Polygon
- Side of a regular n-sided polygon circumscribed in a circle
- Area of a n-sided regular polygon with given side length
- Area of a n-sided regular polygon with given Radius
- Length of Diagonal of a n-sided regular polygon
- Apothem of a n-sided regular polygon