COMPUTER GRAPHICS - Implement Cohen Sutherland Line Clipping Algorithm.

 Experiment No.06

Aim: 

Implement Cohen Sutherland Line Clipping Algorithm.          

Prerequisite:

1. C Language.

2. Geometric Concepts.

3. Concept of 2D basic Transformations.

4. Clipping Concepts.


Outcome:             

After successful completion of this experiment students will be able to,

Apply the transformations and clipping algorithms on graphical objects.

Theory: 

Line Clipping

The concept of line clipping is same as point clipping. 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.

Cohen-Sutherland Line Clippings

This algorithm uses the clipping window as shown in the following figure. The minimum coordinate for the clipping region is (XWmin,YWmin)(XWmin,YWmin) and the maximum coordinate for the clipping region is (XWmax,YWmax)(XWmax,YWmax).

Cohen-Sutherland Line Clipping

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.

TOP-LEFT Corner

There are 3 possibilities for the line −

  1. Line can be completely inside the window (This line should be accepted).
  2. Line can be completely outside of the window (This line will be completely removed from the region).
  3. Line can be partially inside the window (We will find intersection point and draw only that portion of line that is inside region).

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.

Illustration of Line Clipping

Before Clipping

https://www.cs.helsinki.fi/group/goa/viewing/leikkaus/exam1.gif

1. Consider the line segment AD. 

Point A has an outcode of 0000 and point D has an outcode of 1001. The logical AND of these outcodes is zero; therefore, the line cannot be trivally rejected. Also, the logical OR of the outcodes is not zero; therefore, the line cannot be trivally accepted. The algorithm then chooses D as the outside point (its outcode contains 1's). By our testing order, we first use the top edge to clip AD at B. The algorithm then recomputes B's outcode as 0000. With the next iteration of the algorithm, AB is tested and is trivially accepted and displayed.

2. Consider the line segment EI 

Point E has an outcode of 0100, while point I's outcode is 1010. The results of the trivial tests show that the line can neither be trivally rejected or accepted. Point E is determined to be an outside point, so the algorithm clips the line against the bottom edge of the window. Now line EI has been clipped to be line FI. Line FI is tested and cannot be trivially accepted or rejected. Point F has an outcode of 0000, so the algorithm chooses point I as an outside point since its outcode is1010. The line FI is clipped against the window's top edge, yielding a new line FH. Line FH cannot be trivally accepted or rejected. Since H's outcode is 0010, the next iteration of the algorthm clips against the window's right edge, yielding line FG. The next iteration of the algorithm tests FG, and it is trivially accepted and display.

After Clipping

After clipping the segments AD and EI, the result is that only the line segment AB and FG can be seen in the window. 

https://www.cs.helsinki.fi/group/goa/viewing/leikkaus/exam2.gif

Procedure: 

Algortihm:

1. Read two End points of the line say p1(x1,y1) and p2(x2,y2)

2. Read two corners(Left-Top, Right-Bottom) of the windw,say(WX1,WY1 and WX2,WY2); 

3. Assign the region codes for two endpoints, P1 and P2 using following stapes:

                     Initialize code with bits 0000

                      Set Bit1-if(x<WX1)

                      Set Bit2-if(x>WX2)

                      Set Bit1-if(y<WY2)

                      Set Bit1-if(x>Y1)

4. Check for visibility of Line P1,P2

A) If region codes for both endpoints P1 and P2 are Zero then the line is completely visible. Hence draw the line and go step 9. 

B) If region codes for both endpoints P1 and P2 are not Zero and Logical ANDing of them is also nonzero then the line is completely invisible. Hence reject the line and go step 9. 

C) If region codes for two endpoints do not satisfy the condition in 4(A) and 4(B) then the line is partially visible.

5. Determine the interesting edge of the clipping window by inspecting the region codes of two end points.

6. Divide the line segments considering intersection points.

7. Reject the line segment if any one end point of it appears outsides the clipping window.

8. Draw the remaining line segments.

9. Stop.


Code : 

#include<conio.h>

#include<stdio.h>

#include<graphics.h>

#include<math.h>

void bytecode();

void sutherland();

int a[4],b[4];

float m,xnew,ynew;

float xl = 100, yl = 100, xh = 300, yh = 300,xa = 10,ya = 200,xb =

250, yb = 150;

void main()

{

int gd = DETECT,gm;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

setcolor(5);

line(xa,ya,xb,yb);

setcolor(12);

rectangle(xl,yl,xh,yh);

m = (yb-ya)/(xb-xa);

bytecode();

sutherland();

getch();

}

void bytecode()

{

if(xa < xl)

a[3] = 1;

else a[3] = 0;

if(xa>xh)

a[2] = 1;

else a[2] = 0;

if(ya < yl)

a[1] = 1;

else a[1] = 0;

if (ya > yh)

a[0] = 1;

else a[0] = 0;

if(xb < xl)

b[3] = 1;

else b[3] = 0;

if(xb>xh)

b[2] = 1;

else b[2] = 0;

if(yb < yl)

b[1] = 1;

else b[1] = 0;

if (yb > yh)

b[0] = 1;

else b[0] = 0;

}

void sutherland()

{

printf("press a key to continue");

getch();

if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && b[0] == 0 &&

b[1] == 0 && b[2] == 0 && b[3] == 0 )

{

printf("no clipping");

line(xa,ya,xb,yb);

}

else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] || a[3]&&b[3])

{

clrscr();

printf("line discarded");

rectangle(xl,yl,xh,yh);

}

else

{

if(a[3] == 1 && b[3]==0)

{

ynew = (m * (xl-xa)) + ya;

setcolor(12);

rectangle(xl,yl,xh,yh);

setcolor(0);

line(xa,ya,xb,yb);

setcolor(15);

line(xl,ynew,xb,yb);

}

else if(a[2] == 1 && b[2] == 0)

{

ynew = (m * (xh-xa)) + ya;

setcolor(12);

rectangle(xl,yl,xh,yh);

setcolor(0);

line(xa,ya,xb,yb);

setcolor(15);

line(xl,ynew,xb,yb);

}

else if(a[1] == 1 && b[1] == 0)

{

xnew = xa + (yl-ya)/m;

setcolor(0);

line(xa,ya,xb,yb);

setcolor(15);

line(xnew,yh,xb,yb);

}

else if(a[0] == 1 && b[0] == 0)

{

xnew = xa + (yh-ya)/m;

setcolor(0);

line(xa,ya,xb,yb);

setcolor(15);

line(xnew,yh,xb,yb);

}

}

}


Output: 


Observations and learning:

In this experiment we cut the portion of line which is outside of window and keep only the portion that is inside the window with the help of line clipping algorithm.

Conclusion:

In conclusion we learnt the line clipping algorithm by performing this experiment.



Comments