HDU 2898 空间点关于直线旋转

来源:互联网 发布:查看域名注册商 编辑:程序博客网 时间:2024/05/08 23:39

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2898

Problem Description
求空间中一个点绕一穿过原点的轴线旋转一定角度后的坐标,沿着旋转轴往原点看旋转的角度为顺时针(Angles are measured clockwise when looking along the rotation axis toward the origin. )。
 

Input
一个点的3个坐标 x,y,z,
轴线上除原点外的一点 xn,yn,zn
角度 angle(弧度)
 

Output
旋转以后的坐标 (小数点后保留3位)
 

Sample Input
2.0 3.0 6.05.0 5.0 5.0 3.14 100.0 200.0 300.0500.0 600.0 700.0 1.25
 

Sample Output
5.336 4.330 1.334185.988 132.594 296.357
 

Source
2009 Multi-University Training Contest 10 - Host by NIT
思路:可以把空间三维的化为二位平面,平面上点关于直线的旋转就简单多了。
二维平面内点(x,y)关于原点旋转角度s得点(x1,y1)的基本推理公式由三角公式可得,化简得:
x1=x*cos(s)+y*sin(s);
y1=-x*sin(s)+y*cos(s);
至于怎么把三维化为二维就要把x,y,z其中之一的影响化去,也就是让旋转轴与坐标轴其中之一重合,再化回来。
以下就是旋转轴与z轴重合的AC代码:
#include <stdio.h>#include <string.h>#include <math.h>/*author:YangSirtime:2014/5/2*/struct node{double x,y,z;}p,q,r,t,t2,t3,t4,t5;int main(){double a,b,hu;while(~scanf("%lf%lf%lf",&p.x,&p.y,&p.z)){scanf("%lf%lf%lf",&q.x,&q.y,&q.z);scanf("%lf",&hu);a=atan(q.y/q.x);//偏向xozr.x=sqrt(q.x*q.x+q.y*q.y);r.y=0;r.z=q.z;b=atan(r.x/r.z);//偏向zt.x=p.x*cos(a)+p.y*sin(a);t.y=-p.x*sin(a)+p.y*cos(a);t.z=p.z;//点同时翻过去t2.x=t.x*cos(b)-t.z*sin(b);t2.z=t.x*sin(b)+t.z*cos(b);t2.y=t.y;t3.x=t2.x*cos(hu)-t2.y*sin(hu);//点旋转t3.y=t2.x*sin(hu)+t2.y*cos(hu);t3.z=t2.z;//返回去t4.x=t3.x*cos(b)+t3.z*sin(b);t4.z=-t3.x*sin(b)+t3.z*cos(b);t4.y=t3.y;t5.x=t4.x*cos(a)-t4.y*sin(a);t5.y=t4.x*sin(a)+t4.y*cos(a);t5.z=t4.z;printf("%.3lf %.3lf %.3lf\n",t5.x,t5.y,t5.z);}return 0;}


0 0
原创粉丝点击