【NX二次开发】直线与平面交点
两点式直线与平面交点
/** * Brief: 直线与平面的交点 * Overview: 直线(两点式方程)与平面的交点 * Param: * douLineStart 输入直线起点 * douLineEnd 输入直线终点 * douPlanePoint 输入面上的点 * douPlaneVector 输入面的方向(非单位向量也可以) * douInterPoint 输出直线与面的交点 * Return: 0:成功 1:直线与面平行,无交点 */ int getLineInterPlanePoint(double douLineStart[3], double douLineEnd[3], double douPlanePoint[3], double douPlaneVector[3], double douInterPoint[3]) { //直线两点式方程 //(x-x1)/(x2-x1) = (y-y1)/(y2-y1) = (z-z1)/(z2-z1) = t //x=t*(x2-x1)+x1 //y=t*(y2-y1)+y1 //z=t*(z2-z1)+z1 //平面点法式方程 //A(x-x0)+B(y-y0)+C(z-z0)=0 //A((t*(x2-x1)+x1)-x0)+B((t*(y2-y1)+y1)-y0)+C(t*(z2-z1)+z1-z0)=0 //t=(A(x0-x1)+B(y0-y1)+C(z0-z1)) / (A(x2-x1)+B(y2-y1)+C(z2-z1)) double A = douPlaneVector[0]; double B = douPlaneVector[1]; double C = douPlaneVector[2]; double x0 = douPlanePoint[0]; double y0 = douPlanePoint[1]; double z0 = douPlanePoint[2]; double x1 = douLineStart[0]; double y1 = douLineStart[1]; double z1 = douLineStart[2]; double x2 = douLineEnd[0]; double y2 = douLineEnd[1]; double z2 = douLineEnd[2]; if ((A*(x2 - x1) + B*(y2 - y1) + C*(z2 - z1))==0) { return 1; } double t = (A*(x0 - x1) + B*(y0 - y1) + C*(z0 - z1)) / (A*(x2 - x1) + B*(y2 - y1) + C*(z2 - z1)); douInterPoint[0] = t*(x2 - x1) + x1; douInterPoint[1] = t*(y2 - y1) + y1; douInterPoint[2] = t*(z2 - z1) + z1; return 0; }
点向式直线与平面交点
/** * Brief: 直线与平面的交点 * Overview: 直线(点向式方程)与平面的交点,方向反了也是可以的 * Param: * douLineStart 输入直线起点 * douLineVec 输入直线方向 * douPlanePoint 输入面上的点 * douPlaneVector 输入面的方向 * douInterPoint 输出直线与面的交点 * Return: 0:成功 1:直线与面平行,无交点 */ int getLineInterPlanePoint2(double douLineStart[3], double douLineVec[3], double douPlanePoint[3], double douPlaneVector[3], double douInterPoint[3]) { double douLineEnd[3] = { 0.0,0.0,0.0 }; douLineEnd[0] = douLineStart[0] + douLineVec[0]; douLineEnd[1] = douLineStart[1] + douLineVec[1]; douLineEnd[2] = douLineStart[2] + douLineVec[2]; //直线两点式方程 //(x-x1)/(x2-x1) = (y-y1)/(y2-y1) = (z-z1)/(z2-z1) = t //x=t*(x2-x1)+x1 //y=t*(y2-y1)+y1 //z=t*(z2-z1)+z1 //平面点法式方程 //A(x-x0)+B(y-y0)+C(z-z0)=0 //A((t*(x2-x1)+x1)-x0)+B((t*(y2-y1)+y1)-y0)+C(t*(z2-z1)+z1-z0)=0 //t=(A(x0-x1)+B(y0-y1)+C(z0-z1)) / (A(x2-x1)+B(y2-y1)+C(z2-z1)) double A = douPlaneVector[0]; double B = douPlaneVector[1]; double C = douPlaneVector[2]; double x0 = douPlanePoint[0]; double y0 = douPlanePoint[1]; double z0 = douPlanePoint[2]; double x1 = douLineStart[0]; double y1 = douLineStart[1]; double z1 = douLineStart[2]; double x2 = douLineEnd[0]; double y2 = douLineEnd[1]; double z2 = douLineEnd[2]; if ((A*(x2 - x1) + B*(y2 - y1) + C*(z2 - z1)) == 0) { return 1; } double t = (A*(x0 - x1) + B*(y0 - y1) + C*(z0 - z1)) / (A*(x2 - x1) + B*(y2 - y1) + C*(z2 - z1)); douInterPoint[0] = t*(x2 - x1) + x1; douInterPoint[1] = t*(y2 - y1) + y1; douInterPoint[2] = t*(z2 - z1) + z1; return 0; }