line clipping program

#include
#include
#include
#include
struct wind
{
int xmin,ymin;
int xmax,ymax;
};
struct point
{
int x,y;
char code[5];
};

void gencode(struct wind w,struct point p)
{
if(p.x p.code[0]='1'; else p.code[0]='0';
if(p.x>w.xmax)
p.code[1]='1'; else p.code[1]='0';
if(p.y p.code[2]='1'; else p.code[2]='0';
if(p.y>w.ymax)
p.code[3]='1'; else p.code[3]='0';

}


struct point clipline(struct wind w,struct point p,struct point pt)
{
int i,flag=0;
float m=(p.y-pt.y)/(p.x-pt.x);
gencode(w,p);
while(!flag)
{
flag=1;
if(p.code[0] == '1')
{
p.y+=(w.xmin-p.x)*m;
p.x=w.xmin;
gencode(w,p);
flag=0;
}
else if(p.code[1] == '1')
{
p.y+=(w.xmax-p.x)*m;
p.x=w.xmax;
gencode(w,p);
flag=0;
}
else if(p.code[2] == '1')
{
p.x+=(w.ymin-p.y)/m;
p.y=w.ymin;
gencode(w,p);
flag=0;
}
else if(p.code[3] == '1')
{
p.x+=(w.ymax-p.y)/m;
p.y=w.ymax;
gencode(w,p);
flag=0;
}

}
cout< getch();
return p;
}



void main()
{

int gdriver=DETECT,gm;
struct point p1,p2;
struct wind w;

initgraph(&gdriver,&gm," ");
printf("Window ");
scanf("%d%d%d%d",&w.xmin,&w.ymin,&w.xmax,&w.ymax);
printf("Line ");
scanf("%d%d%d%d",&p1.x,&p1.y,&p2.x,&p2.y);
p1=clipline(w,p1,p2);
p2=clipline(w,p2,p1);
rectangle(w.xmin,w.ymin,w.xmax,w.ymax);
printf("%d%d%d%d",p1.x,p1.y,p2.x,p2.y);
line(p1.x,p1.y,p2.x,p2.y);
getch();
closegraph();
}

Comments

Popular posts from this blog

Listing/Delisting of an article in to an Assortment in SAP SCM Retail

How To Install LEX and YACC in Linux or Ubuntu

How to Create Live Search In Html Using Ajax Ad PhP - Google Live Search / Instant Search Code And Example