//通过三点找圆心: private P3D getO(P3D s, P3D m, P3D e) { double xsm = m.X - s.X; //(xsm,ysm,zsm)为向量SM double ysm = m.Y - s.Y; double zsm = m.Z - s.Z; double xme = e.X - m.X; //(xme,yme,zme)为向量ME double yme = e.Y - m.Y; double zme = e.Z - m.Z; double xp = s.X + (m.X - s.X) / 2; //(xp,yp,zp)为SM中点 double yp = s.Y + (m.Y - s.Y) / 2; double zp = s.Z + (m.Z - s.Z) / 2; double xq = m.X + (e.X - m.X) / 2; //(xq,yq,zq)为SM中点 double yq = m.Y + (e.Y - m.Y) / 2; double zq = m.Z + (e.Z - m.Z) / 2; double XN = ysm * zme - yme * zsm; //(XN,YN1,ZN)为向量N,其值为向量SM和向量ME的差乘 double YN1 = xme * zsm - xsm * zme; double ZN = xsm * yme - xme * ysm; double D_N = Math.Sqrt(XN * XN + YN1 * YN1 + ZN * ZN); double xn = XN / D_N; double yn1 = YN1 / D_N; double zn = ZN / D_N; double A11 = yme * zn - zme * yn1; //矩阵A代数余子式 double A12 = xn * zme - xme * zn; double A13 = xme * yn1 - xn * yme; double A21 = yn1 * zsm - ysm * zn; double A22 = xsm * zn - xn * zsm; double A23 = xn * ysm - xsm * yn1; double A31 = ysm * zme - yme * zsm; double A32 = xme * zsm - xsm * zme; double A33 = xsm * yme - xme * ysm; double D_A = xsm * yme * zn - xsm * yn1 * zme - xme * ysm * zn + xn * ysm * zme + xme * yn1 * zsm - xn * yme * zsm;//矩阵A行列式 double b1 = xp * xsm + yp * ysm + zp * zsm; double b2 = xq * xme + yq * yme + zq * zme; double b3 = s.X * xn + s.Y * yn1 + s.Z * zn; P3D O = new P3D(); O.X = (A11 * b1 + A21 * b2 + A31 * b3) / D_A; //O为空间圆弧的圆心坐标 O.Y = (A12 * b1 + A22 * b2 + A32 * b3) / D_A; O.Z = (A13 * b1 + A23 * b2 + A33 * b3) / D_A; double xoe = e.X - O.X; double yoe = e.Y - O.Y; double zoe = e.Z - O.Z; double R2 = xoe * xoe + yoe * yoe + zoe * zoe; double R1 = Math.Sqrt(R2); //R1为空间圆弧的半径 return O; }
//绘制空间圆柱: private void DwCylinder(P3D v1,P3D v2,double r) { //计算方向矢量N(A,B,C): //P3D v1 = new P3D(); P3D v2 = new P3D(); P3D v = new P3D(); //double r = 0.5; //v1.X = 0; v1.Y = -1; v1.Z = 0; //v2.X = 1; v2.Y = 1; v2.Z = 1; double A, B, C; A = v1.X - v2.X; B = v1.Y - v2.Y; C = v1.Z - v2.Z; //计算方向矢量之模(请确保p,m不等于0): double p = Math.Sqrt(A * A + B * B); double m = Math.Sqrt(A * A + B * B + C * C); //计算向量之方向余弦: //ca=Cos(a);cb=Cos(b);cc=Cos(c) double ca = A / m; double cb = B / m; double cc = C / m; //绘制空间圆柱-2: GL.Begin(BeginMode.Quads); //画方块 int[] ar = new int[4] { 0, 1, 1, 0 }; int[] br = new int[4] { 0, 0, 1, 1 }; double ds = m/2; double dt = 0.35; for (double s = 0; s < m; s += ds) { for (double t = 0; t < 2 * Math.PI; t += dt) { double[] x = new double[4]; double[] y = new double[4]; double[] z = new double[4]; for (int k = 0; k < 4; k++) { double sv = s + ar[k] * ds; double tv = t + br[k] * dt; double st = Math.Sin(tv); double ct = Math.Cos(tv); x[k] = v1.X - sv * ca + r * (st * A * C / (m * p) + ct * B / p); y[k] = v1.Y - sv * cb + r * (st * B * C / (m * p) - ct * A / p); z[k] = v1.Z - sv * cc - r * (st * p / m); GL.Color3(Math.Sin(sv-.6), Math.Cos(sv * tv), Math.Cos(.4+tv)); GL.Vertex3(x[k], y[k], z[k]); } } } GL.End(); //========================================= }