### Line Clipping Algorithm | Cohen Sutherland

Line Clipping Algorithm:-

If one point of a line is outside the given window and one point is inside the window , then we will clip the line inside the window. In other words , we will find the intersection of the line with the corresponding boundaries of the window such that the point lying outside the window will be discarded and line segment inside the window is displayed.

- It involves two point i.e a line.
- We need to compute the intersection point inside of the part of line lying inside the window.

Now , there are three types of lines that need to be clipped if it is displayed.

**Both the line lie inside the window****Both the line lie outside the window.****Both lines lie inside or outside partially.**

- If both the line lie inside the window , then there is no need to clip.
- If both the line lie outside the window , then there is no viewpoint to display.
- If line lie partially inside or outside , we will find the intersection point of the line with window boundary.

## Cohen Sutherland Algorithm :-

This method results in the less computation time by performing the initial test on the line segments.

It avoids performing calculation on the line segment which is already outside the window.

- Every line in the window is assigned a 4 digit binary code which is called a region code.
- Regions are allocated as shown in the figure below.

- Now every bit in a region corresponds to left , right , below , above regions.
- We now check that the lines lies in which region. A line can be located in more than one region.

Algorithm :-

**A line will be given with the points P**_{1}(x_{1},y_{1}) and P_{2}(x_{2},y_{2}).**We will compute the codes for each of the point.**

**If both the codes are 0000 i.e OR of both codepoints are 0000, then the line lie inside the window. It can be easily accepted.****If the AND of both codes have some 1’s, then the line is totally outside the window. It can be trivially rejected.**

**If a line is partially inside or outside the window , then there is atleast one point which lies outside the window. So , this line must be clipped to the edge of the window.****For this, we have to find the intersection point that coincide on the edge of the window.****When the Intersection point ‘I’ is found , compute the replace the outside point with ‘I’ and repeat the Algorithm.**

### Illustration of the Algorithm:-

*1: let us consider the first line segment AE.*

*The Point A has outcode of 0101 and Point G has outcode of 0010. The Logical AND of these is zero; therefore the line cannot be trivally rejected. Also, the OR of this line is not zero , therefore it cannot be trivally accepted.*

*So ,the line is clipped at Point C and Point D.*

*2: For the first line segment JG.*

*The Point J has outcode of 1000 and Point E has outcode of 1000. The Logical AND of these is zero; therefore the line cannot be trivally rejected. Also, the OR of this line is not zero , therefore it cannot be trivally accepted.*

*So ,the line is clipped at Point I and Point H.*

### Cohen Sutherland Solved Example

### Write A Program in C to clip a line using Cohen-Sutherland Algorithm.

#include<graphics.h> #include<iostream.h> #include<conio.h> #include<process.h> #include<math.h> int outcode(int x,int y,int xmin,int ymin,int xmax,int ymax) { int code=0; if(y>ymax) code|=1; else if(y<ymin) code|=2; if(x>xmax) code|=4; else if(x<xmin) code|=8; return code; } int main() { int gdriver=DETECT,gmode,errorcode; int x1,y1,x2,y2,xmax,ymax,xmin,ymin; cout<<"Enter First Point\n"; cin>>x1>>y1; cout<<"Enter Second Point \n"; cin>>x2>>y2; cout<<"Enter Max Clip Boundry points\n"; cin>>xmax>>ymax; cout<<"Enter Min Clip Boundry points\n"; cin>>xmin>>ymin; initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI"); errorcode=graphresult(); if(errorcode!=grOk) { cout<<"Graphics Error"<<grapherrormsg(errorcode); getch(); exit(1); } int draw=0,i=0,x=0,y=0; int code_out; setcolor(WHITE); line(xmin,ymin,xmin,ymax); line(xmin,ymax,xmax,ymax); line(xmax,ymax,xmax,ymin); line(xmax,ymin,xmin,ymin); setcolor(RED); line(x1,y1,x2,y2); int code_pt1=outcode(x1,y1,xmin,ymin,xmax,ymax); int code_pt2=outcode(x2,y2,xmin,ymin,xmax,ymax); do { if(!(code_pt1|code_pt2)) { draw=1; i=1; } else if(code_pt1&code_pt2) { i=1; } else { code_out=code_pt1 ?code_pt1:code_pt2; if(code_out & 1) { x=x1+(((x2-x1)/(y2-y1))*(ymax-y1)); y=ymax; } else if(code_out & 2) { x=x1+(((x2-x1)/(y2-y1))*(ymin-y1)); y=ymin; } else if(code_out & 4) { y=y1+(((y2-y1)/(x2-x1))*(xmax-x1)); x=xmax; } else { y=y1+(((y2-y1)/(x2-x1))*(xmin-x1)) ; x=xmin; } if(code_out==code_pt1) { x1=x; y1=y; code_pt1=outcode(x1,y1,xmin,ymin,xmax,ymax); } else { x2=x; y2=y; code_pt2=outcode(x2,y2,xmin,ymin,xmax,ymax); } } }while(i==0); if(draw) { setcolor(BLUE); line(x1,y1,x2,y2); } getch(); closegraph(); return 0; }

CommentsNo comment yet.