2. Introduction
When drawing a 2D line on screen, it might happen that
one or both of the endpoints are outside the screen while
a part of the line should still be visible.
In that case, an efficient algorithm is needed to find two
new endpoints that are on the edges on the screen, so
that the part of the line that's visible can now be drawn.
2Aamir Sohail
3. Introduction cont…
This way, all those points of the line outside the screen
are clipped away and you don't need to waste any
execution time on them.
A good clipping algorithm is the Cohen-Sutherland line
clipping algorithm for this solution.
3Aamir Sohail
4. Cohen Sutherland Clipping Algorithm
When drawing a 2D line, if one endpoint of the line is
outside the screen, and the other inside, you have to
clip the line so that only the part of it that's inside the
screen remains.
Even if both endpoints are outside the screen, it's still
possible that a part of the line should be visible.
The clipping algorithm needs to find new endpoints of
the lines, that are inside or on the edges of the screen.
4Aamir Sohail
5. Cases
Here are a few cases, where the black rectangle
represents the screen, in red are the old endpoints,
and in blue the ones after clipping:
5Aamir Sohail
6. Cont…
Case A: Both end-points are inside the screen, so no
clipping needed.
6Aamir Sohail
7. Cont…
Case B: One end-point outside the screen, that one had to
be clipped.
7Aamir Sohail
8. Cont…
Case C: both endpoint are outside the screen, and no part of
the line is visible, don't draw it at all.
8Aamir Sohail
9. Cont…
Case D: both endpoint are outside the screen, and a part of
the line is visible, clip both endpoints and draw it.
9Aamir Sohail
10. Cohen Sutherland Clipping Algorithm
Now we will learn what is Cohen Sutherland Clipping Algorithm
and how it works.
This algorithm clips a line to the clipping rectangle. It
concerns itself with performing the simple cases quickly.
10Aamir Sohail
11. Cont…
In this algorithm it divides lines & edges into 2 cases.
1) Trivially Accept and
2) Trivially Reject.
11Aamir Sohail
12. Conditions of Trivially Accept
Xmin ≤ X ≤ Xmax
Ymin ≤ Y ≤ Ymax
Lines fulfill this conditions then we will
mark those lines as trivially accept.
Ymax
Ymin
Xmin Xmax
12Aamir Sohail
16. ABRL Cont…
The center region is the screen or Window Position (0000).
If the region is above the screen, the first bit is 1.
If the region is below the screen, the second bit is 1.
If the region is to the right of the screen, the third bit is 1.
If the region is to the left of the screen, the fourth bit is
1.
16Aamir Sohail
17. A (0100) B (0010)
AND Operation
Then get the new point C (0000)
17Aamir Sohail
18. C (0000) B (0010)
AND Operation
Then get the new point D (0000)
Then we have the final line after clipping is CD 18Aamir Sohail
19. Handling Similar Situations
If similar problems arrive then we have to clip
those according to mentioned method.
Some examples of similar situations
19Aamir Sohail
20. I hope there will be no
question in your mind…
20Aamir Sohail