public double Distance(Line3D line) { // test for parallel line which must be handled differently var rx = Math.Abs(m_v.X / line.m_v.X); var ry = Math.Abs(m_v.Y / line.m_v.Y); var rz = Math.Abs(m_v.Z / line.m_v.Z); if (Math.Abs(rx - ry) < kContainsEpsilon && Math.Abs(ry - rz) < kContainsEpsilon) return DistanceOfParallelLine(line); return DistanceOfSkewLine(line); }
public double DistanceOfParallelLine(Line3D line) { var mpLp = line.m_p - m_p; // vector from m_p to line.m_p var proj_v_mpLp = mpLp.ProjectSelfOnto(m_v); var perp = mpLp - proj_v_mpLp; return perp.Magnitude(); }
// http://math.stackexchange.com/questions/13734/how-to-find-shortest-distance-between-two-skew-lines-in-3d public double DistanceOfSkewLine(Line3D line) { return m_v.Cross(line.m_v).ToUnitVector().Dot(m_p - line.m_p); }