//绘制空间圆柱: 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(); //========================================= }
//通过三点找圆心: 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); }