定义一个求直线与平面交点坐标的函数CalPlaneLineIntersectPoint(),
其代码如下:
1///2/// 求一条直线与平面的交点 3///4///平面的法线向量,长度为35///平面经过的一点坐标,长度为36///直线的方向向量,长度为37///直线经过的一点坐标,长度为38///返回交点坐标,长度为3910privatefloat[] CalPlaneLineIntersectPoint(float[] planeVector,float[] planePoint,float[] lineVector,float[] linePoint) 11{ 12float[] returnResult =newfloat[3]; 13float vp1, vp2, vp3, n1, n2, n3, v1, v2, v3, m1, m2, m3, t,vpt; 14 vp1 = planeVector[0]; 15 vp2 = planeVector[1]; 16 vp3 = planeVector[2]; 17 n1 = planePoint[0]; 18 n2 = planePoint[1]; 19 n3 = planePoint[2]; 20 v1 = lineVector[0]; 21 v2 = lineVector[1]; 22 v3 = lineVector[2]; 23 m1 = linePoint[0]; 24 m2 = linePoint[1]; 25 m3 = linePoint[2]; 26 vpt = v1 * vp1 + v2 * vp2 + v3 * vp3; 27//首先判断直线是否与平面平行28if (vpt ==0) 29 { 30 returnResult =null; 31 } 32else33 { 34 t = ((n1 - m1) * vp1 + (n2 - m2) * vp2 + (n3 - m3) * vp3) / vpt; 35 returnResult[0] = m1 + v1 * t; 36 returnResult[1] = m2 + v2 * t; 37 returnResult[2] = m3 + v3 * t; 38 } 39return returnResult; 40} 41