public double DistanceToLine(clsLine3d l2) { //Perpendicular distance between 3d lines. Limited to the line segments. double myNormalDist; clsPoint3d v1; clsPoint3d v2; clsPoint3d M; double m2; clsPoint3d R; clsPoint3d P21; double t1; double t2; clsPoint3d Q1; clsPoint3d Q2; //Find the actual point on this line where the perpendicular distance hits. If it is off the line, then find the minimum distance between the end points v1 = new clsPoint3d(DX(), DY(), DZ()); v1.Normalise(); v2 = new clsPoint3d(l2.DX(), l2.DY(), l2.DZ()); v2.Normalise(); P21 = new clsPoint3d(l2.X1 - X1, l2.X1 - X1, l2.X1 - X1); M = v2.Cross(v1); m2 = M.Dot(M); if (m2 < mdlGeometry.myTol) { return(DistanceToPoint(l2.P1)); } //Parallel myNormalDist = Abs(P21.Dot(M)) / Sqrt(m2); //Perpendicular distance R = P21.Cross(M); R.Scale(1 / m2); t1 = R.Dot(v2); Q1 = P1 + t1 * v1; if (t1 < 0) { Q1 = P1; } if (t1 > Length) { Q1 = P2; } t2 = R.Dot(v1); Q2 = l2.P1 + t2 * v2; if (t2 < 0) { Q2 = l2.P1; } if (t2 > l2.Length) { Q2 = l2.P2; } return(Q1.Dist(Q2)); }