《实验四图形几何变换地实现.doc》由会员分享,可在线阅读,更多相关《实验四图形几何变换地实现.doc(9页珍藏版)》请在课桌文档上搜索。
1、word 实验四 图形几何变换的实现班级: 学号: 姓名: 分数:一、实验目的和要求:1、进一步掌握理解二维、三维的数学知识、变换原理、变换种类、变换方法;2、利用VC+语言实现二维、三维图形的根本变换和复合变换。;3、理解采用齐次坐标进展图形变换的必要性变换的连续性,使复合变换得以实现。二、实验内容:源程序分别实现了对二维图形进展的平移变换根本变换;对三维图形进展的绕某一个坐标轴旋转变换以与相对于立方体中心的比例变换复合变换。三维几何变换:(1) 比例变换:=T3D=局部比例变换:=其中a、b、j分别为在x、y、z方向的比例系数。整体比例变换:=其中s为在xyz方向的等比例系数。S1时,整体
2、缩小;s1时,整体放大。(2) 旋转变换:旋转变换的角度方向为沿坐标轴的反方向看去,各轴按逆时针方向旋转绕z轴旋转:=绕x轴旋转:=绕y轴旋转:=三、实验结果分析/*三维图形立方体旋转变换、比例变换*/#include #include #include #include #include #include int turn13;/*0rx,1ry,3zoom*/typedef structfloat x; float y; float z;point;typedef structfloat x; float y;point2d;typedef struct float x; float y;
3、 float h;point biao8;fanti;void make_box(float x,float y,float h,fanti *p)p-x=x;p-y=y;p-h=h;p-biao0.x=x/2; p-biao0.y=y/2; p-biao0.z=h/2;p-biao1.x=-x/2; p-biao1.y=y/2; p-biao1.z=h/2;p-biao2.x=-x/2; p-biao2.y=-y/2; p-biao2.z=h/2;p-biao3.x=x/2; p-biao3.y=-y/2; p-biao3.z=h/2;p-biao4.x=x/2; p-biao4.y=y/2
4、; p-biao4.z=-h/2;p-biao5.x=-x/2; p-biao5.y=y/2; p-biao5.z=-h/2;p-biao6.x=-x/2; p-biao6.y=-y/2; p-biao6.z=-h/2;p-biao7.x=x/2; p-biao7.y=-y/2; p-biao7.z=-h/2;void trun2d(point *p,point2d *q)q-x=p-x+p-z*cos(0.25);q-y=p-y+p-z*sin(0.25);void initm(float mat4)int count;for(count=0;countx+tm10*p-y+tm20*p-z
5、+tm30;yv=tm01*p-x+tm11*p-y+tm21*p-z+tm31;zw=tm02*p-x+tm12*p-y+tm22*p-z+tm32;p-x=xu; p-y=yv; p-z=zw;return;void rotationx(point *p,float alfa,float tm4)float rad=0.0174532925; initm(tm);tm11=cos(rad*alfa); tm12=sin(rad*alfa);tm21=-tm12; tm22=tm11;return;void rotationz(point *p,float alfa,float tm4)fl
6、oat rad=0.0174532925; initm(tm);tm00=cos(rad*alfa); tm01=sin(rad*alfa);tm10=-tm01; tm11=tm00;return;void rotationy(point *p,float alfa,float tm4)float rad=0.0174532925; initm(tm);tm00=cos(rad*alfa); tm20=sin(rad*alfa);tm02=-tm20; tm22=tm00;return;void adjust(point *p,point *q)float t44; switch (turn
7、10) case 1:rotationy(p,2,t);transfrom(p,q,t);break;case -1:rotationy(p,-2,t);transfrom(p,q,t);break; default:break;switch (turn11) case 1:rotationz(p,2,t);transfrom(p,q,t);break;case -1:rotationz(p,-2,t);transfrom(p,q,t);break; default:break;switch (turn12) case 1:q-x=ZOOM_IN*p-x;q-y=ZOOM_IN*p-y;q-z
8、=ZOOM_IN*p-z;break;case -1: q-x=ZOOM_OUT*p-x;q-y=ZOOM_OUT*p-y;q-z=ZOOM_OUT*p-z;break; default:break;void drawbox(fanti *p)point2d fan2d8;int i;for(i=0;ibiaoi,&p-biaoi);trun2d(&p-biaoi,&fan2di);fan2di.x+=300;fan2di.y+=200;cleardevice();outtext( - :right - :left :up v :down);moveto(0,20); outtext( pag
9、e up:zoom in page down:zoom out space :Redraw Esc :exit);for(i=0;i=3;i+)if(i=3)line(fan2di.x,fan2di.y,fan2d0.x,fan2d0.y);line(fan2di+4.x,fan2di+4.y,fan2d4.x,fan2d4.y);elseline(fan2di.x,fan2di.y,fan2di+1.x,fan2di+1.y);line(fan2di+4.x,fan2di+4.y,fan2di+5.x,fan2di+5.y); line(fan2di.x,fan2di.y,fan2di+4.
10、x,fan2di+4.y);void main()int gd=DETECT,gm,i,j;char key; float x,y,h;fanti a1; x=100; y=100; h=100;initgraph(&gd,&gm, );setcolor(GREEN);make_box(x,y,h,&a1);for(;)turn10=0;turn11=0;turn12=0;key=getch();switch(key)case 77: turn10=1; break; /*RIGHT*/ case 75: turn10=-1; break; /*LIFT*/case 72: turn11=1; break; /*UP*/case 80: turn11=-1; break; /*DOWN*/case 73: turn12=1; break; /*Zoom In*/ case 81: turn12=-1; break; /*Zoom Out*/case 32: make_box(x,y,h,&a1); break; /*Redraw*/case 27: exit(0); break;default : key=0; break;if(key!=0) drawbox(&a1);closegraph();实验结果:原始图形:左右旋转图形:上下旋转图形:缩小图形:放大图形:文档