Friday 27 September 2013

Cohen Sutherland Line Clipping Aglorithm | Computer Graphics Lab Manual | IT department | Final Year





#include
#include
#include
#include
int xleft=100,ytop=100,xright=200,ybot=200,k=0;
int encode(float,float);
void clip(float,float,float,float);
void main()
{
int gd=VGA,gm=VGAMED;
int i;
float f[5][5]={{175,300,250,300},{135,145,160,180},{30,160,175,160},{30,30,250,250},{120,140,120,375}};
initgraph(&gd,&gm,"c:\\tc\\bgi");
outtextxy(10,10,"before clipping");
rectangle(xleft,ytop,xright,ybot);
for(i=0;i<5 i="" p=""> line(f[i][0],f[i][1],f[i][2],f[i][3]);
getch();
cleardevice();
setbkcolor(15);
setcolor(8);
outtextxy(10,10,"after clipping");
rectangle(xleft,ytop,xright,ybot);
for(i=0;i<5 i="" p=""> clip(f[i][0],f[i][1],f[i][2],f[i][3]);
getch();
closegraph();
}
void clip(float x1,float y1,float x2,float y2){
int c1,c2,x,y,c;
c1=encode(x1,y1);
c2=encode(x2,y2);
if((c1==0)&&(c2==0)){
line(x1,y1,x2,y2);
return;}
if((c1>0)&&(c2>0))
k=1;
if(c1&c2)
return;
while(c1||c2)
{ c=c1;
if(c==0)
c=c2;
if(c&1)
{
y=y1+(y2-y1)*(xleft-x1)/(x2-x1);
x=xleft;
}
if(c&2){
y=y1+(y2-y1)*(xright-x1)/(x2-x1);
x=xright;
}
if(c&4)
{
x=x1+(x2-x1)*(ybot-y1)/(y2-y1);
y=ybot;
}
if(c&8){
x=x1+(x2-x1)*(ytop-y1)/(y2-y1);
y=ytop;
}
if(c==c1)
{ if(k!=1)
line(x,y,x2,y2);
x1=x;
y1=y;
c1=encode(x1,y1);
}
if(c==c2)
{ if(k!=1)
line(x1,y1,x,y);
x2=x;
y2=y;
c2=encode(x2,y2);
}}
if(k==1)
line(x1,y1,x2,y2);
}
int encode(float x,float y)
{
int code=0;
if(x code=code|1;
if(x>xright)
code=code|2;
if(y>ybot)
code=code|4;
if(y code=code|8;
return(code);
getch();
}




No comments:

Don't You Think this Awesome Post should be shared ??
| Cohen Sutherland Line Clipping Aglorithm | Computer Graphics Lab Manual | IT department | Final Year |
Back To Top Related Posts Plugin for WordPress, Blogger...