Skip to main content

Cohen Sutherland Line Clipping Algorithm

Cohen Sutherland Line Clipping Algorithm

The Cohen–Sutherland algorithm

is a computer graphics algorithm used for line clipping. The algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are visible in the center region of interest (the viewport).

We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right, and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set to 1 because it is the TOP-LEFT corner.


This Image describes the different regions of Viewport (device coordinate) used for clipping a line inside a device region. It contains a 4-bit digit in order as follows: Above,Below,Right,Left. Depending on the region the respective bit is made 1 rest other bits are 0.
Viewport
Image Credits: Tutorials Point

There are 3 possibilities for the line to lie  −
  • Line can be completely inside the window (This line should be visible).
  • Line can be completely outside the window (This line should be rejected).
  • Line can be partially inside the window (We find intersection point and draw only that portion of the line which lies inside the region).

Code:


If you can't see the code, then Enable JavaScript in you browser


Output:





Note: Just copy the files "CGA.bgi" & "EGAVGA.bgi" from bgi folder to bin. So, if you have copied these 2 files then you can skip writing "C:\\tc\\bgi" in initgraph with just ""(2 double quotes)

Comments

Post a Comment

Popular posts from this blog

Bresenham's Line Algorithm (THICK LINE)

Bresenham's Line Algorithm (THICK LINE) Bresenham's Line Algorithm (THICK LINE) The Bresenham algorithm is an incremental scan conversion algorithm.

Bresenham's Line Algorithm (DASHED LINE)

Bresenham's Line Algorithm (DASHED LINE) The Bresenham algorithm is an incremental scan conversion algorithm.

Bresenham's Line Algorithm (DOTTED LINE)

Bresenham's Line Algorithm (DOTTED LINE) Bresenham's Line Algorithm (DOTTED LINE) Bresenham's Line Algorithm (DOTTED LINE) The Bresenham algorithm is an incremental scan conversion algorithm.



Bestseller's in Electronics only at Amazon Best Selling Products on Amazon