64,282
社区成员
发帖
与我相关
我的任务
分享
|
|s
|
|
|_______
/ \
/b0 \b1
`
| .
|s0 /s1
| /
| /
|/________
/
/b
b.x = 0;
b.y = 1;
b.z = 0;
s.x = 0;
s.y = 0;
s.z = 1;
r = rotate ( b,s,45.0f);//45度角
#include <iostream>
#include <math.h>
#define PI 3.141592653f
using namespace std;
struct VECTOR3
{
float x;
float y;
float z;
};
VECTOR3 rotate( VECTOR3 b,VECTOR3 st,float ank)//st 为要围绕的向量
{
/*
http://zh.wikipedia.org/zh-cn/%E6%97%8B%E8%BD%AC%E7%9F%A9%E9%98%B5
b.x b.y b.z
//旋转矩陈
cos(t) + ( 1 - cos(t)) st.x * st.x ( 1 - cos(t)) st.x * st.y - sin(t) * st.z , ( 1 - cos(t) ) * st.x * st.z + (sin(t))* st.y
( 1 - cos (t) ) * st.y * st.x + sin(t) * st.z cos(t) + ( 1 -cos (t)) st.y * st.y ( 1- cos (t) ) * st.y * st.z - (sin(t)* st.x
( 1 - cos (t) ) * st.z * st.x - ( sin (t )) * st.y ( 1 - cos (t)) * st.z * st.y + (sin(t)) * st.x cos(t) + ( 1 - cos (t)) * st.z * st.z
t1 q1 p1
t2 q2 p2
t3 q3 p3
*/
VECTOR3 result;
float t = PI * ank / 180;
cout<< cos(t ) << " " <<sin(t) << endl;
float t1 = cos(t) + ( 1 - cos(t)) * st.x * st.x;
float t2 = ( 1 - cos (t) ) * st.x * st.y + sin(t) * st.z;
float t3 = ( 1 - cos (t) ) * st.z * st.x - ( sin (t )) * st.y;
float q1 = ( 1 - cos(t)) * st.x * st.y - sin(t) * st.z;
float q2 = cos(t) + ( 1 -cos (t)) * st.y * st.y ;
float q3 = ( 1 - cos (t)) * st.z * st.y + (sin(t)) * st.x;
float p1 = (1 - cos(t) )* st.x * st.z + (sin(t))* st.y;
float p2 = ( 1- cos (t) ) * st.y * st.z - (sin(t)* st.x );
float p3 = cos(t) + ( 1 - cos (t)) * st.z * st.z;
result.x = b.x * t1 + b.y * t2 + b.z * t3;
result.y = b.x * q1 + b.y * q2 + b.z * q3;
result.z = b.x * p1 + b.y * p2 + b.z * p3;
return result;
}
int main()
{
VECTOR3 b , s;
//z= 0 在x-y 平面 测试
b.x = 0;
b.y = 1;
b.z = 0;
s.x = 0;
s.y = 0;
s.z = 1;
VECTOR3 r;
r = rotate ( b,s,45.0f);
cout << r.x << " " << r.y << " " <<r.z <<endl;
::getchar ();
}