资源描述:
《中点分割裁剪算法.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*sutherland_cohen裁剪算法*/#defineLEFT1#defineRIGHT2#defineBOTTOM4#defineTOP8#defineXL150#defineXR350#defineYB150#defineYT300#include"math.h"#include"graphics.h"#include"conio.h"#include"stdio.h"voidinitialize(void){intgraphdriver;intgraphmode;interrorcode;graphdriver=DETECT;initgraph(&grap
2、hdriver,&graphmode,"d:\tc");errorcode=graphresult();if(errorcode!=0){printf("graphicssystemerreor:%s",grapherrormsg(errorcode));exit(1);}}main(){initialize();setcolor(12);line(XL,YT,XR,YT);line(XL,YB,XR,YB);line(XL,YT,XL,YB);line(XR,YT,XR,YB);setcolor(14);draw_ett();getch();closegraph()
3、;}encode(x,y,code)intx,y;int*code;{intc;c=0;if(x4、LEFT;elseif(x>XR)c=c
5、RIGHT;if(y6、BOTTOM;elseif(y>YT)c=c
7、TOP;*code=c;return;}draw_ett(){intx1,x2,y1,y2,x,y;intcode1,code2,code;x1=50;y1=250;x2=400;y2=300;setcolor(1);line(x1,y1,x2,y2);encode(x1,y1,&code1);encode(x2,y2,&code2);w
8、hile((code1!=0)
9、
10、(code2!=0)){if((code1&code2)!=0)return;code=code1;if(code1==0)code=code2;if((LEFT&code)!=0){x=XL;y=y1+(y2-y1)*(XL-x1)/(x2-x1);}elseif((RIGHT&code)!=0){x=XR;y=y1+(y2-y1)*(XR-x1)/(x2-x1);}elseif((BOTTOM&code)!=0){y=YB;x=x1+(x2-x1)*(YB-y1)/(y2-y1);}elseif((TOP&code)!=0){y=Y
11、T;x=x1+(x2-x1)*(YT-y1)/(y2-y1);}if(code==code1){x1=x;y1=y;encode(x,y,&code1);}else{x2=x;y2=y;encode(x,y,&code2);}}setcolor(14);line(x1,y1,x2,y2);return;}#defineLEFT1#defineRIGHT2#defineBOTTOM4#defineTOP8#defineXL150#defineXR350#defineYB150#defineYT300#include"math.h"#include"graphics.h"
12、#include"conio.h"#include"stdio.h"voidinitialize(void){intgraphdriver;intgraphmode;interrorcode;graphdriver=DETECT;initgraph(&graphdriver,&graphmode,"d:\tc");errorcode=graphresult();if(errorcode!=0){printf("graphicssystemerreor:%s",grapherrormsg(errorcode));exit(1);}}main(){intx1,y1,x2,
13、y2,xx,yy,xxx,yyy;initialize();scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);cleardevice();xx=0;yy=0;xxx=0;yyy=0;setcolor(12);line(XL,YT,XR,YT);line(XL,YB,XR,YB);line(XL,YT,XL,YB);line(XR,YT,XR,YB);setcolor(14);draw_ett(x1,y1,x2,y2,&xx,&yy);draw_ett(x2,y2,xx,yy,&xxx,&yyy);line(xx,y