实验三裁剪法Word文档下载推荐.docx
《实验三裁剪法Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验三裁剪法Word文档下载推荐.docx(17页珍藏版)》请在冰豆网上搜索。
#defineTOP8
#defineXL150
#defineXR350
#defineYB200
#defineYT300
#include<
math.h>
#include<
graphics.h>
stdio.h>
encode(x,y,code)
floatx,y;
int*code;
{
intc=0;
if(x<
XL)c=c|LEFT;
elseif(x>
XR)c=c|RIGHT;
if(y<
YB)c=c|BOTTOM;
elseif(y>
YT)c=c|TOP;
*code=c;
return*code;
}
voidM_lieCLip(floatx1,floaty1,floatx2,floaty2,float*x,float*y)
intcode1,code2,code;
floatt=1,xx,yy;
encode(x1,y1,&
code1);
encode(x2,y2,&
code2);
if(code1==0){*x=x1;
*y=y1;
return;
}
while(code1&
code2==0)
L1:
xx=(x1+x2)/2;
yy=(y1+y2)/2;
encode(x,y,&
code);
if(abs((x2-xx)*(x2-xx)+(y2-yy)*(y2-yy))<
t)
{*x=xx;
*y=yy;
if(code&
code1!
=0){x2=xx;
y2=yy;
else{x1=xx;
y1=yy;
voidmain()
{
floatx1,y1,x2,y2,xx,yy,xxx,yyy,t;
intgdriver=DETECT,gmode;
initgraph(&
gdriver,&
gmode,"
"
);
setcolor(4);
line(XL,YT,XR,YT);
line(XL,YB,XR,YB);
line(XL,YT,XL,YB);
line(XR,YT,XR,YB);
x1=50;
y1=150;
x2=400;
y2=300;
setcolor(7);
line(x1,y1,x2,y2);
xx=0;
yy=0;
xxx=0;
yyy=0;
M_lieCLip(x1,y1,x2,y2,&
xx,&
yy);
M_lieCLip(x2,y2,xx,yy,&
xxx,&
yyy);
setcolor(11);
line(xx,yy,xxx,yyy);
getch();
closegraph();
运行结果图:
裁剪之前裁剪之后
实验题2:
实现Sutherland-Hodgeman多边形裁剪算法。
对矩形窗口中一多边形进行裁剪。
/*Sutherland-Hodgman算法*/
#defineLENsizeof(structnode)
#include"
math.h"
stdio.h"
graphics.h"
structnode
intdx,dy;
structnode*next;
};
structnode*h,*q,*r;
structnode*creat()
{
intp[8][2]={100,120,160,50,180,100,200,80,240,160,210,220,170,160,140,190};
inti;
setcolor(10);
for(i=0;
i<
7;
i++)line(p[i][0],p[i][1],p[i+1][0],p[i+1][1]);
line(p[0][0],p[0][1],p[7][0],p[7][1]);
rectangle(120,200,230,70);
h=NULL;
8;
i++)
q=(structnode*)malloc(LEN);
q->
dx=p[i][0];
dy=p[i][1];
if(h==NULL)
h=q;
elser->
next=q;
r=q;
r->
next=NULL;
return(h);
intx;
structnode*builx(h,x)
structnode*h;
ints[2],j[2];
structnode*hh,*p,*q;
intmax,min;
p=h;
hh=NULL;
s[0]=p->
dx;
s[1]=p->
dy;
p=p->
next;
while(p!
=NULL)
j[0]=x;
j[1]=s[1]+(p->
dy-s[1])*(x-s[0])/(p->
dx-s[0]);
max=s[0];
min=p->
if(s[0]<
p->
dx)
max=p->
min=s[0];
if((j[0]>
=min)&
&
(j[0]<
=max))
dx=j[0];
q->
dy=j[1];
if(hh==NULL)
hh=q;
else
if(p->
dx>
=x)
{q=(structnode*)malloc(LEN);
dx=p->
dy=p->
if(hh==NULL)hh=q;
j[1]=s[1]+(p->
dx){max=p->
min=s[0];
return(hh);
structnode*builxx(h,x)
{ints[2],j[2];
hh=NULL;
s[1]=p->
dx-s[0]+0.1);
min=p->
if(hh==NULL)
dx<
structnode*buily(h,y)
inty;
j[1]=y;
j[0]=s[0]+(p->
dx-s[0])*(y-s[1])/(p->
dy-s[1]+0.1);
max=s[1];
if(s[1]<
dy){max=p->
min=s[1];
if((j[1]>
(j[1]<
dy>
=y)
structnode*builyy(h,y)
dy<
voidInitialize(void)
intgdriver=DETECT,gmode;
initgraph(&
gdriver,&
setbkcolor(BLACK);
main()
{intmax,min;
structnode*head,*r,*q;
ints[2];
Initialize();
head=creat();
q=head;
while(q->
next!
{putpixel(q->
dx,q->
dy,14);
q=q->
putpixel(q->
q=builx(head,120);
head=q;
dy,15);
q=buily(head,70);
dy,2);
q=builxx(head,230);
dy,1);
q=builyy(head,200);
s[0]=q->
s[1]=q->
setcolor(6);
while(q!
{line(s[0],s[1],q->
dy);
line(s[0],s[1],q->
getch();