给定任意一点p(x,y,z),直线AB,其中A(a0,b0,c0)、B(a,b,c),旋转的角度θ=theta,可对其进行如下实现:
1 private double[] random_rotateObject(double x, double y, double z, double a, double b, double c,double a0,double b0,double c0,double theta) 2 { 3 //(a0,b0,c0)轴线的始点 4 //(a,b,c)轴线L末点 5 //(x,y,z)初始点位置 6 //数组xyz 绕L旋转后的位置 7 //行(1,0,0,-m)(0,1,0,-n)(0,0,1,-k)(0,0,0,1)移至原点 8 double u = Math.Sqrt(3)/3; ; 9 double v = Math.Sqrt(3)/3; 10 double w = Math.Sqrt(3)/3; //方向向量(u,v,w)需为单位向量!!! 11 double PI = 3.1415926535898 12 double[] xyz=new double[3]; 13 double SinA = Math.Sin(theta * PI / 180); 14 double CosA = Math.Cos(theta * PI / 180); 15 16 double uu=u*u; 17 double vv=v*v; 18 double ww=w*w; 19 double uv=u*v; 20 double uw=u*w; 21 double vw=v*w; 22 23 float t00 = uu + (vv + ww) * CosA; 24 float t10 = uv * (1 - CosA) + w * SinA; 25 float t20 = uw * (1 - CosA) - v * SinA; 26 float t30 = 0; 27 28 float t01 = uv * (1 - CosA) - w * SinA; 29 float t11 = vv + (uu + ww) * CosA; 30 float t21 = vw * (1 - CosA) + u * SinA; 31 float t31 = 0; 32 33 float t02 = uw * (1 - CosA) + v * SinA; 34 float t21 = vw * (1 - CosA) - u * SinA; 35 float t22 = ww + (uu + vv) * CosA; 36 float t32 = 0; 37 38 float t03 = (a0 * (vv + ww) - u * (b0 * v + c0 * w)) * (1 - CosA) + (b0 * w - c0 * v) * SinA; 39 float t13 = (b0 * (uu + ww) - v * (a0 * u + c0 * w)) * (1 - CosA) + (c0 * u - a0 * w) * SinA; 40 float t23 = (c0 * (uu + vv) - w * (a0 * u + b0 * v)) * (1 - CosA) + (a0 * v - b0 * u) * SinA; 41 float t33 = 1; 42 43 xyz[0] = t00 * x + t01 * y + t02 * z + t03; 44 xyz[1] = t10 * x + t11 * y + t12 * z + t13; 45 xyz[2] = t20 * x + t21 * y + t22 * z + t23; 46 return xyz; 47 }