So we have a polygon which we have to clip against the given window. Cohen Sutherland Algorithm works from Left to Top to Right to Bottom. So, let's start. But before that, let us draw a rough diagram of the polygon and the clipping window.
(Sorry for my drawing)
Step 1: Clip the polygon from the left
Since the polygon does not intersect the left part of the clipping window, no clipping is performed.
Output Vertex Array = Empty
Step 2: Clip the polygon from the top
From the left we have the edge BD. Here, B is outside but D is inside. In such a case, we only add the point of intersection and the second point to our output list. So, we have to calculate the point of intersection first.
The slope of BD is given by
Now let the intersection point be B'. We know that the y coordinate of that point is same as S and R, which is 10. We only have to find the x coordinate. We can do so by using slope. Let the x coordinate be "w".
Using this formula, we can find out the value of d, which will come out to be 5.2.
Output Vertex Array = {B' , D}
Now, there is another edge that can clipped which is BC. It will be clipped in a similar manner. First we find its slope which comes out to be -5/2. Next, we find the coordinate of the intersecting part, C'. The coordinates for this will come out to be 5.8.
Output Vertex Array = {B', D, C', C}
Step 3: Clip from Right
Since the polygon is not intersecting the right edge, we make no change to the output vertex array.
Output Vertex Array = {B', D, C', C}
Step 4: Clip from Bottom
Finally we clip from the bottom. Since all edges are present inside, all of them are added to the array.
Output Vertex Array = {B', D, C', C, A}
Now we can finally draw the final polygon.
This is how our clipped polygon looks like.
Comments