hdu 3320 计算几何(三维图形几何变换)
openGL
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 170????Accepted Submission(s): 77
Now, He tells you the position of the vertex and the transformations. Please help Jiaoshou find the position of the vertex after several transformations.
?
Input The input will start with a line giving the number of test cases, T.Each case will always begin with “glBegin(GL_POINTS);”.Then the case will be followed by 5 kinds of function.
1. glTranslatef(x,y,z);
??This function will translate the vertex(x’,y’,z’) to vertex(x+x’,y+y’,z+z’).
2. glRotatef(angle,x,y,z);
??This function will turn angle radians counterclockwise around the axis (0,0,0)->(x,y,z).
3. glScalef(x,y,z);
??This function wiil translate the vertex(x’,y’,z’) to vertex(x*x’,y*y’,z*z’).
4. glVertex3f(x,y,z);
??This function will draw an initial vertex at the position(x,y,z). It will only appear once in one case just before “glEnd();”. In openGL, the transformation matrices are right multiplied by vertex matrix. So you should do the transformations in the?reverse?order.?
5. glEnd();
??This function tells you the end of the case.
In this problem angle,x,y,z are real numbers and range from -50.0 to 50.0. And the number of functions in each case will not exceed 100.
?
Output For each case, please output the position of the vertex after several transformations x,y,z in one line rounded to 1 digits after the decimal point , separated with a single space. We guarantee that x,y,z are not very large.?
Sample Input 1 glBegin(GL_POINTS); glScalef(2.0,0.5,3.0); glTranslatef(0.0,1.0,0.0); glVertex3f(1.0,1.0,1.0); glEnd();?
Sample Output 2.0 1.0 3.0 Hint In this sample, we first let the vertex do “glTranslatef(x,y,z);” this function, then do “glScalef(x,y,z)”.?
Author Water Problem SecKill Expert?
Source HDU “Valentines Day” Open Programming Contest 2010-02-14題目大意:給一個(gè)點(diǎn)的坐標(biāo),對(duì)它進(jìn)行多種變換(平移變化、比例變換、繞任意軸旋轉(zhuǎn)變換),輸出它的最終坐標(biāo)。不過它給出各變換的操作順序是反過來的。?
解題思路:構(gòu)造各變換的變化矩陣,用矩陣乘法乘起來就是最終坐標(biāo)。不會(huì)構(gòu)造變化矩陣的詳情請(qǐng)看下面:
?
三維幾何變換
?
1. 三位平移變換是使立體在空間平移一段距離,其形狀和大小保持不變。變化矩陣為
? ? ? ? ? ? ? ? ?
?
? ? ? ? ? ? ??
2. 三維局部比例變換,關(guān)于原點(diǎn)的比例變換的變換矩陣為
? ? ??
3. 三維立體繞通過原點(diǎn)的任意軸旋轉(zhuǎn)角的變換。
設(shè)ON為過坐標(biāo)原點(diǎn)的一根任意軸,它對(duì)坐標(biāo)軸的前方向余弦分別為
? ? ? ? ? ? ? ??
中間過程就不多說了詳情請(qǐng)看計(jì)算機(jī)圖形學(xué)教程(第2版)P176-P184,它的變換矩陣為
?
? ? ??
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 using namespace std; 6 7 int cnt; 8 char operat[105][105]; 9 double ang,x,y,z,xx,yy,zz; 10 11 struct Matrix 12 { 13 double m[4][4]; 14 }; 15 16 Matrix mult(Matrix a,Matrix b)//矩陣乘法 17 { 18 Matrix c; 19 for(int i=0;i<4;i++) 20 { 21 for(int j=0;j<4;j++) 22 { 23 c.m[i][j]=0.0; 24 for(int k=0;k<4;k++) 25 c.m[i][j]+=a.m[i][k]*b.m[k][j]; 26 } 27 } 28 return c; 29 } 30 31 Matrix Translate(int i)//平移變換 32 { 33 sscanf(operat[i],"glTranslatef(%lf,%lf,%lf);",&x,&y,&z); 34 Matrix tmp={1,0,0,0,0,1,0,0,0,0,1,0,x,y,z,1}; 35 return tmp; 36 } 37 Matrix RatioTranslate(int i)//局部比例變換 38 { 39 sscanf(operat[i],"glScalef(%lf,%lf,%lf);",&x,&y,&z); 40 Matrix tmp={x,0,0,0,0,y,0,0,0,0,z,0,0,0,0,1}; 41 return tmp; 42 } 43 44 Matrix RotateTranslate(int i)//繞通過原點(diǎn)的任意軸旋轉(zhuǎn)變換 45 { 46 sscanf(operat[i],"glRotatef(%lf,%lf,%lf,%lf);",&ang,&x,&y,&z); 47 double t=sqrt(x*x+y*y+z*z); 48 double n1=x/t;//cos(a),a是與x軸的夾角 49 double n2=y/t;//cos(b),b是與y軸的夾角 50 double n3=z/t;//cos(c),c是與z軸的夾角 51 double S=sin(ang),C=cos(ang); 52 Matrix tmp={n1*n1+(1-n1*n1)*C,n1*n2*(1-C)+n3*S,n1*n3*(1-C)-n2*S,0, 53 n1*n2*(1-C)-n3*S,n2*n2+(1-n2*n2)*C,n2*n3*(1-C)+n1*S,0, 54 n1*n3*(1-C)+n2*S,n2*n3*(1-C)-n1*S,n3*n3+(1-n3*n3)*C,0, 55 0,0,0,1}; 56 return tmp; 57 } 58 59 Matrix solve() 60 { 61 Matrix ret={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1}; 62 sscanf(operat[cnt-2], "glVertex3f(%lf,%lf,%lf);",&xx,&yy,&zz); 63 for(int i=cnt-3;i>0;i--) 64 { 65 if(operat[i][2]=='T') 66 ret=mult(ret,Translate(i)); 67 else if(operat[i][2]=='S') 68 ret=mult(ret,RatioTranslate(i)); 69 else if(operat[i][2]=='R') 70 ret=mult(ret,RotateTranslate(i)); 71 } 72 return ret; 73 } 74 75 int main() 76 { 77 int t; 78 scanf("%d",&t); 79 getchar(); 80 while(t--) 81 { 82 cnt=0; 83 while(1) 84 { 85 gets(operat[cnt++]); 86 if(operat[cnt-1][2]=='E') 87 break; 88 } 89 Matrix ans=solve(); 90 printf("%.1lf %.1lf %.1lf\n",xx*ans.m[0][0]+yy*ans.m[1][0]+zz*ans.m[2][0]+ans.m[3][0], 91 xx*ans.m[0][1]+yy*ans.m[1][1]+zz*ans.m[2][1]+ans.m[3][1], 92 xx*ans.m[0][2]+yy*ans.m[1][2]+zz*ans.m[2][2]+ans.m[3][2]); 93 } 94 return 0; 95 }?
轉(zhuǎn)載于:https://www.cnblogs.com/xiong-/p/3936168.html
總結(jié)
以上是生活随笔為你收集整理的hdu 3320 计算几何(三维图形几何变换)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 腾讯官网快速修改QQ游戏防沉迷方法
- 下一篇: 腾达 A8 无线路由器设置宾馆模式上网操