Tuesday, April 13, 2010

Cohen Sutherland(Line Clipping Algo)

#include
#include
#include
#include
void main()
{ float xmin,ymin,xmax,ymax,x1,x2,y1,y2;
int gd=DETECT,gm;
void clip(float,float,float,float,float,float,float,float);
printf("Enter the window co-ordinate:");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
printf("Enter the co-ordinate:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(15);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
clip(xmin,ymin,xmax,ymax,x1,y1,x2,y2);
}
void clip(float xmin,float ymin,float xmax,float ymax,float x1,float y1,float x2,float y2)
{ static int c[4],c1[4],c2[4],flag1,flag2,i;
 float x,y;
 void code(float,float,float,float,float,float,int*);
 code(x1,y1,xmin,ymin,xmax,ymax,c1);
 code(x2,y2,xmin,ymin,xmax,ymax,c2);
 flag1=c1[0]||c1[1]||c1[2]||c1[3];
 flag2=c2[0]||c2[1]||c2[2]||c2[3];
 while(flag1||flag2)
 {
  if(c1[0]&&c2[0]||c1[1]&&c2[1]||c1[2]&&c2[2]||c1[3]&&c2[3])
  {
   closegraph();
   printf("Line is outside the window:");
   goto abc;
  }
 if(flag1==0)
 for(i=0;i<4;i++)
 c[i]=c2[i];
 else
  for(i=0;i<4;i++)
 c[i]=c1[i];
 if(c[0]==1)
 {
  y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
  x=xmin;
 }
 else
 if(c[1]==1)
 {
  y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
  x=xmax;
 }
 else if(c[2]==1)
 {
  x=x1+(x2-x1)*(y1-ymax)/(y2-y1);
  x=ymax;
 }
 else if(c[3]==1)
 {
  x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
  x=ymin;
 }
 if(flag1==0)
 {
  x2=x;
  y2=y;
  code(x2,y2,xmin,ymin,xmax,ymax,c2);
  flag2=c2[0]||c2[1]||c2[2]||c2[3];
 }
  else
  {
  x1=x;
  y1=y;
  code(x1,y1,xmin,ymin,xmax,ymax,c1);
  flag1=c1[0]||c1[1]||c1[2]||c1[3];
  }
 }
 setcolor(15);
 line(x1,y1,x2,y2);
 abc:
 getch();
}void code(float x,float y,float xmin,float ymin,float xmax,float ymax,int* c)
{
 if(x
 {
  c[0]=1;
  c[1]=0;
 }
 else if(x>xmax)
 {
  c[0]=0;
  c[1]=1;
 }
else c[0]=c[1]=0;
if(y>ymax)
 {
  c[2]=1;
  c[3]=0;
 }
else if(y
 {
  c[2]=0;
  c[3]=1;
 }
else c[2]=c[3]=0;

}

0 comments:

Post a Comment

 
ShareThis

Visitor

Website counter
Copyright 2009 Code's. Powered by Blogger
Blogger Templates created by Deluxe Templates
Wordpress by Wpthemescreator
Blogger Showcase