Cohen-Sutherland Line Clipping Algorithm with C/C++

Theory
In this method, every line endpoint is assigned a four-digit binary code(region code) that identifies the location of the point relative to the boundary.
b1 : left
b2 : right
b3 : below
b4 : above
The value 1 indicates its relative position. If a point is within clipping rectangle then region code is 0000. So,
If x – xwmin < 0 , b1 = 1
If xwmax – x < 0 , b2 = 1
If y – ywmin < 0 , b3 = 1
If ywmin – y < 0 , b4 = 1
If the region codes of both end points are 0000 then we accept
the line.  Any line that have one in the same bit position is rejected i.e If RA AND RB ≠ 0 ,line is completely outside.
The lines which can not be identified as completely inside or outside a window by these tests are checked for intersection with the window boundary. Such lines may or may not cross into the window interior.

Source Code
Note: to run this code in your machine with Code::blocks IDE, add a link library libgdi32.a (it is usually inside MinGWlib )  in linker setting.

Output

CohenSutherland

SHARE Cohen-Sutherland Line Clipping Algorithm with C/C++

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Share