5(a)PROGRAM:
#include
#include
#include
float cxa,cxb,cya,cyb;
code(float,float);
void clip(float,float,float,float);
void rect(float,float,float,float);
void main()
{
float x1,x2,y1,y2;
int g=0,d;
initgraph(&g,&d," ");
setbkcolor(9);
settextstyle(3,0,1);
outtextxy(40,15,"Before Clipping");
printf("\n\n\n\nEnter left right top
bottom of clipping window:\n");
scanf("%f%f%f%f",&cxa,&cyb,&cxb,&cya);
getch();
printf("\n Enter
the line x1,y1,x2,y2:\n");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
getch();
rect(cxa,cyb,cxb,cya);
line(x1,y1,x2,y2);
getch();
cleardevice();
setbkcolor(9);
settextstyle(3,0,1);
outtextxy(40,15,"After Clipping");
clip(x1,y1,x2,y2);
getch();
closegraph();
}
void clip(float x1,float y1,float x2,float y2)
{
int c,c1,c2;
float x,y;
c1=code(x1,y1);
c2=code(x2,y2);
getch();
while((c1!=0)||(c2!=0))
{
if((c1&c2)!=0)
goto out;
c=c1;
if(c==0)
c=c2;
if((c&1)==1)
{
y=y1+((y2-y1)*(cxa-x1))/(x2-x1);
x=cxa;
}
else
if((c&2)==2)
{
y=y1+((y2-y1)*(cxb-x1))/(x2-x1);
x=cxb;
}
else
if((c&8)==8)
{
x=x1+((x2-x1)*(cyb-y1))/(y2-y1);
y=cyb;
}
else
if((c&4)==4)
{
x=x1+((x2-x1)*(cya-y1))/(y2-y1);
y=cya;
}
if(c==c1)
{
x1=x;
y1=y;
c1=code(x,y);
}
else
{
x2=x;
y2=y;
c2=code(x,y);
//goto out;
}
}
line(x1,y1,x2,y2);
out:
rect(cxa,cyb,cxb,cya);
}
code(float x,float y)
{
int c=0,a=0,b=0;
if(x
a=1;
else if(x>cxb)
a=2;
if(y
b=8;
else if(y>cya)
b=4;
c=a+b;
return c;
}
void rect(float x1,float yb,float xr,float yt)
{
line(x1,yb,xr,yb);
line(xr,yb,xr,yt);
line(xr,yt,x1,yt);
line(x1,yt,x1,yb);
}
No comments:
Post a Comment