您的位置:首页 > 运维架构

hdoj.3320 openGL【空间变换】 2015/04/22

2015-04-22 20:05 190 查看

openGL

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 207 Accepted Submission(s): 86

[align=left]Problem Description[/align]
Jiaoshou selected a course about “openGL” this semester. He was quite interested in modelview, which is a part of “openGL”. Just using three functions, it could make the model to move, rotate and largen or lessen. But he was puzzled
with the theory of the modelview. He didn’t know a vertex after several transformations where it will be.

Now, He tells you the position of the vertex and the transformations. Please help Jiaoshou find the position of the vertex after several transformations.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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();


[align=left]Sample Output[/align]

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)”.


[align=left]Author[/align]
Water Problem SecKill Expert

[align=left]Source[/align]
HDU “Valentines Day” Open Programming Contest 2010-02-14
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
struct node{
char name;
double w;
double x,y,z;
}gl[110];
int t,flag,ans;
double a,b,c,aa,bb,cc;
char s[15],ss;
void tran(int k){
aa += gl[k].x;
bb += gl[k].y;
cc += gl[k].z;
}
void sca(int k){
aa *= gl[k].x;
bb *= gl[k].y;
cc *= gl[k].z;
}
void rot(int k){
double si=sin(gl[k].w);
double co=cos(gl[k].w);
double m=sqrt(gl[k].x*gl[k].x+gl[k].y*gl[k].y+gl[k].z*gl[k].z);
double x1,y1,z1;
double x2,y2,z2;
x1 = gl[k].x / m;
y1 = gl[k].y / m;
z1 = gl[k].z / m;
x2 = (x1 * x1 * (1 - co) + co)*aa + (x1 * y1 * (1 - co) - z1 * si)*bb + (x1 * z1 * (1 - co) + y1 * si)*cc;
y2 = (y1 * x1 * (1 - co) + z1 * si)*aa + (y1 * y1 * (1 - co) + co)*bb + (y1 * z1 * (1 - co) - x1 * si)*cc;
z2 = (x1 * z1 * (1 - co) - y1 * si)*aa + (y1 * z1 * (1 - co) + x1 * si)*bb + (z1 * z1 * (1 - co) + co)*cc;
aa = x2;
bb = y2;
cc = z2;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%*s");
flag = 0;
while( 1 ){
int i = 0;
while(1){
scanf("%c",&ss);
if( ss == '(' ) break;
else s[i++] = ss;
}
//puts(s);
//printf("%c\n",s[3]);
if( s[3] == 'E' ){
scanf("%*c%*c");
break;
}
if( s[3] == 'R' ){
scanf("%lf,%lf,%lf,%lf);",&gl[flag].w,&gl[flag].x,&gl[flag].y,&gl[flag].z);
gl[flag].name = s[3];
flag++;
continue;
}
scanf("%lf,%lf,%lf);",&a,&b,&c);
//printf("%lf,%lf,%lf\n",a,b,c);
if( s[3] == 'V' ){
aa = a;
bb = b;
cc = c;
ans = flag;
}
gl[flag].name = s[3];
gl[flag].x = a;
gl[flag].y = b;
gl[flag].z = c;
flag++;
}
while( ans >= 0 ){
switch( gl[--ans].name ){
case 'S':sca(ans);break;
case 'T':tran(ans);break;
case 'R':rot(ans);break;
}
}
printf("%.1lf %.1lf %.1lf\n",aa,bb,cc);
}
return 0;
}
注:空间变换,glRotatef(angle,x,y,z); 意思为按角度为angle的逆时针旋转,式子比较麻烦,只要知道这一点就好做了,空间想象能力要好,还有就是计算时按从下往上进行的,倒数第二行的三个数字即为开始数字(x,y,z)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: