Cohen-Sutherland Line Clipping Algorithm
Cohen-Sutherland Line Clipping
It is one of the most popular line-clipping algorithms. The concept of assigning 4-bit region codes to the endpoints of a line and subsequent checking and operation of the endpoint codes to determine totally visible lines and invisible lines (lying completely at one side of the clip window externally). Cohen-Sutherland Line Clipping Algorithm was originally introduced by Danny Cohen and Ivan Sutherland.
Steps of Cohen-Sutherland Line Clipping Algorithm
1. Input: x1, xR, yT, yB, P1(x1, y1), P2(x2, y2) Initialize i=1 while i<=2 i=i+1 end while i=1 2. Intialize j=1 while j<=2 3. if codes of P1 and P2 are both equal to zero then draw P1P2 (Totally Visible) 4. if logical intersection or AND operation of code - P1 and code -P2 is not equal to zero then ignore P1P2 (Totally invisible) 5. if code -P1=0 then swap P1 and P2 along with their flags and set i=1 6. if code - P1< >0 then for i=1 { if C1 left=1 then find interaction (xL, y'L) with left edge assign code to (xL, y'L) P1=(xL, y'L) end if i=i+1 go to 3 } for i=2 { if C1 right=1 then find interaction (xR, y'R) with right edge assign code to (xR, y'R) P1=(xR, y'R) end if i=i+1 go to 3 } for i=3 { if C1 bottom=1 then find interaction (x'B, yB) with bottom edge assign code to (x'B, yB) P1=(x'B, yB) end if i=i+1 go to 3 } for i=4 { if C1 top=1 then find interaction (x'T, yT) with top edge assign code to (x'T, yT) P1=(x'T, yT) end if i=i+1 go to 3 } end