You are on page 1of 12

cohensutherland-line-

clipping-algorithm
• 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. 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 algorithm for this solution.
• In line clipping, we will cut the portion of line which is outside of window and
keep only the portion that is inside the window.
There are different cases,

Case A: Both the end points are inside the screen, so no


clipping needed.
Case B: One end-point outside the screen, that one had to be
clipped.
Case C: both endpoint are outside the screen, so line is not
visible.
Case D: Both the endpoints are outside the screen, and the
part of the line is visible, clip both the endpoints and draw it.
In this algorithm it divides lines and edges into 2 cases,

1. Trivially Accept

2. Trivially Reject
Algorithm:
• Step 1 − Assign a region code for each endpoints.
• Step 2 − If both endpoints have a region code 0000 then accept this line.
• Step 3 − Else, perform the logical ANDoperation for both region codes.
• Step 3.1 − If the result is not 0000, then reject the line.
• Step 3.2 − Else you need clipping.
• Step 3.2.1 − Choose an endpoint of the line that is outside the window.
• Step 3.2.2 − Find the intersection point at the window boundary (base on region
code).
• Step 3.2.3 − Replace endpoint with the intersection point and update the region
code.
• Step 3.2.4 − Repeat step 2 until we find a clipped line either trivially accepted or
trivially rejected.
• Step 4 − Repeat step 1 for other lines.

You might also like