分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
如果直线不与平面平行,将存在交点。如下图所示,已知直线L过点m(m1,m2,m3),且方向向量为VL(v1,v2,v3),平面P过点n(n1,n2,n3),且法线方向向量为VP(vp1,vp2,vp3),求得直线与平面的交点O的坐标(x,y,z):
将直线方程写成参数方程形式,即有:
x = m1+ v1 * t
y = m2+ v2 * t (1)
z = m3+ v3 * t
将平面方程写成点法式方程形式,即有:
vp1 * (x – n1) + vp2 * (y – n2) + vp3 * (z – n3) = 0 (2)
则直线与平面的交点一定满足式(1)和(2),联立两式,求得:
t = ((n1 – m1)*vp1+(n2 – m2)*vp2+(n3 – m3)*vp3) / (vp1* v1+ vp2* v2+ vp3* v3) (3)
如果(3)式中分母(vp1* v1+ vp2* v2+ vp3* v3)为0,则表示直线与平面平行,即直线与平面没有交点。求解出t后,然后将t代入式(1)即可求得交点O的坐标(x,y,z)。定义一个求直线与平面交点坐标的函数CalPlaneLineIntersectPoint(),其代码如下:
/// <summary>/// 求一条直线与平面的交点/// </summary>/// <param name="planeVector">平面的法线向量,长度为3</param>/// <param name="planePoint">平面经过的一点坐标,长度为3</param>/// <param name="lineVector">直线的方向向量,长度为3</param>/// <param name="linePoint">直线经过的一点坐标,长度为3</param>/// <returns>返回交点坐标,长度为3</returns>private float[] CalPlaneLineIntersectPoint(float[] planeVector, float[] planePoint, float[] lineVector, float[] linePoint){float[] returnResult = new float[3];float vp1, vp2, vp3, n1, n2, n3, v1, v2, v3, m1, m2, m3, t,vpt;vp1 = planeVector[0];vp2 = planeVector[1];vp3 = planeVector[2];n1 = planePoint[0];n2 = planePoint[1];n3 = planePoint[2];v1 = lineVector[0];v2 = lineVector[1];v3 = lineVector[2];m1 = linePoint[0];m2 = linePoint[1];m3 = linePoint[2];vpt = v1 * vp1 + v2 * vp2 + v3 * vp3;//首先判断直线是否与平面平行if (vpt == 0){returnResult = null;}else{t = ((n1 - m1) * vp1 + (n2 - m2) * vp2 + (n3 - m3) * vp3) / vpt;returnResult[0] = m1 + v1 * t;returnResult[1] = m2 + v2 * t;returnResult[2] = m3 + v3 * t;}return returnResult;}