裁剪计算机图形学

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验报告(2013—2014学年第二学期)课程名称:计算机图形学姓名:学院:专业:年级:实验报告2014年5月15日第3·4节综合楼426号室进入实验室时间进入时仪器设备状况离开实验室时间离开时仪器设备状况机器号9:50良好11:30良好32实验项目名称二维图形变换一、实验目的1.掌握cohen-sutherland直线段裁剪算法2.掌握sutherland-hodgman多边形裁剪算法二、实验内容1.利用cohen-sutherland算法实现直线段关于矩形窗口的裁减2.利用sutherland-hodgman算法实现多边形关于矩形窗口的裁减实验过程:直线段的裁剪:1.编写相应程序(看教材代码),并调试。2.分完全可见、显然不可见、至少一个端点在窗口之外但非显然不可见三种情况,测试各种斜率的直线,查看程序运行结果。3.比较教材代码和下列代码,指出下列代码存在的问题。多边形的裁剪:1.写出intersect()函数的功能2.写出inside()函数的功能3.根据sutherland-hodgman算法,在sutherlandhodgmanpolygonclip()函数的/**/处写注释4.编写main函数,并调试三、实验过程(步骤)及结果(源程序)(1)直线裁剪代码:typedefstruct{intxmax;intxmin;intymax;intymin;}rectang;structOutCode{intall;inttop;intbottom;intleft;intright;};voidCompOutCode(floatx,floaty,rectang*rect,OutCode*outcode){outcode-all=0;outcode-top=outcode-bottom=0;if(y(float)rect-ymax){outcode-bottom=1;outcode-all+=1;}elseif(y(float)rect-ymin){outcode-top=1;outcode-all+=1;}outcode-right=outcode-left=0;if(x(float)rect-xmax){outcode-right=1;outcode-all+=1;}elseif(x(float)rect-xmin){outcode-left=1;outcode-all+=1;}}rectangCohenSuther(float*xx0,float*yy0,float*xx1,float*yy1,rectang*rect){intaccept,done;OutCodeoutcode0,outcode1;OutCode*outcodeout;floatx0,y0,x1,y1;x0=*xx0;y0=*yy0;x1=*xx1;y1=*yy1;floatx,y;accept=FALSE;done=FALSE;CompOutCode(x0,y0,rect,&outcode0);CompOutCode(x1,y1,rect,&outcode1);do{if(outcode0.all==0&&outcode1.all==0){//可见区域accept=TRUE;done=TRUE;}elseif((outcode0.all&outcode1.all)!=0)//显然不可见区域done=TRUE;else{//既非可见,又非显然可见区域if(outcode0.all!=0)//选窗口外的端点outcodeout=&outcode0;elseoutcodeout=&outcode1;if(outcodeout-left){//交点在左边y=y1+(y1-y0)*(rect-xmin-x1)/(x1-x0);x=(float)rect-xmin;}elseif(outcodeout-top){//交点在上边x=x1+(x1-x0)*(rect-ymin-y1)/(y1-y0);y=(float)rect-ymin;}elseif(outcodeout-right){//交点在右边y=y1+(y1-y0)*(rect-xmax-x1)/(x1-x0);x=(float)rect-xmax;}elseif(outcodeout-bottom){//交点在下边x=x1+(x1-x0)*(rect-ymax-y1)/(y1-y0);y=(float)rect-ymax;}if(outcodeout-all==outcode0.all){//丢弃显然不可见区域端点x0=x;y0=y;CompOutCode(x0,y0,rect,&outcode0);}else{x1=x;y1=y;CompOutCode(x1,y1,rect,&outcode1);}}}while(!done);//if(accept)//line((int)x0,(int)y0,(int)x1,(int)y1);rectangrectang1;rectang1.xmin=(int)x0;rectang1.ymin=(int)y0;rectang1.xmax=(int)x1;rectang1.ymax=(int)y1;returnrectang1;}//voidline(intx1,inty1,intx2,inty2){//CDC*pdc=GetDC();//pdc-MoveTo(x1,y1);//pdc-LineTo(x2,y2);//};//voidInitVGA(void)//{///intDrive=VGA;//intMode=VGAHI;//initgraph(&Drive,&Mode,..\\bgi);//}(2)窗口及直线代码:voidCline::OnLine(){//TODO:AddyourcontrolnotificationhandlercodehereCDC*ydc=GetDC();rectangrect;rect.xmax=110;rect.ymax=110;rect.xmin=20;rect.ymin=20;//rectang*prect=▭floatx0,y0,x1,y1;floatmx,my;CRectsysrt(rect.xmax,rect.ymax,rect.xmin,rect.ymin);//CRectsysrt(20,20,110,110);ydc-FillSolidRect(&sysrt,RGB(225,225,255));x0=mx0;y0=my0;x1=mx1;y1=my1;mx=(x0+x1)/2;my=(y0+y1)/2;CPenpen1;CPenpen2;pen1.CreatePen(PS_SOLID,5,RGB(0,225,0));pen2.CreatePen(PS_SOLID,10,RGB(225,0,0));ydc-SelectObject(&pen1);ydc-MoveTo((int)x0,(int)y0);ydc-LineTo((int)x1,(int)y1);Sleep(2000);///rectangrt=CohenSuther(&x0,&y0,&mx,&my,&rect);ydc-SelectObject(&pen2);////ydc-MoveTo(rt.xmin,rt.ymin);//ydc-LineTo(rt.xmax,rt.ymax);rectangrt2=CohenSuther(&x0,&y0,&x1,&y1,&rect);ydc-MoveTo(rt2.xmin,rt2.ymin);ydc-LineTo(rt2.xmax,rt2.ymax);//TODO:Addyourcontrolnotificationhandlercodehere}(2)矩形剪裁代码#defineTRUE1#defineFALSE0typedefstruct{floatx,y;}vertex;voidintersect(vertexp1,vertexp2,vertex*clipboundary,vertex*intersectp){if(clipboundary[0].y==clipboundary[1].y)/*水平边界*/{intersectpt-y=clipboundary[0].y;intersectpt-x=p1.x+(clipboundary[0].y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y);}else/*垂直边界*/{intersectpt-x=clipboundary[0].x;intersectpt-y=p1.y+(clipboundary[0].x-p1.x)*(p2.y-p1.y)/(p2.x-p1.x);}}intinside(vertextestvertex,vertex*clipboundary){if(clipboundary[1].xclipboundary[0].x)/*上边界*/if(testvertex.y=clipboundary[0].y)returnTRUE;if(clipboundary[1].xclipboundary[0].x)/*下边界*/if(testvertex.y=clipboundary[0].y)returnTRUE;if(clipboundary[1].yclipboundary[0].y)/*右边界*/if(testvertex.x=clipboundary[0].x)returnTRUE;if(clipboundary[1].yclipboundary[0].y)/*左边界*/if(testvertex.y=clipboundary[0].x)returnTRUE;returnFALSE;}outputvertex(vertexoutvertex,int*outlength,vertex*outvertexlist){outvertexlist[*outlength]=outvertex;(*outlength)++;}voidSutherland_Hodgman_Polygon_Clipping(vertex*invertexlist,vertex*outvertexlist,intinlength,int*outlength,vertex*clipboundary){vertexs,p,i;intj;*outlength=0;s=invertexlist[inlength-1];/*输入顶点序列的最后一个顶点*/for(j=0;jinlength;j++){p=invertexlist[j];if(inside(p,clipboundary)){if(inside(s,clipboundary))/**/outputvertex(p,outlength,outvertexlist);else/**/{intersect(s,p,clipboundary,&i);outputvertex(i,outlength,outvertexlist);outputvertex(p,outlength,outvertexlist);}}else{if(inside(s,clipboundary))/**/{intersect(s,p,clipboundary,&i);outputvertex(i,outlength,outvertexlist);}}/**/s=p;/*准备处理下一条边*/}}(3)对话框编辑图(4)实验结果截图如下:1、直线裁剪:2、多边形裁剪图四、存在问题及解决方法实验过程中遇到的问题大致如下:评语:

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功