public static boolean intersect ( PlaneGeneral3D_F64 a , PlaneGeneral3D_F64 b , LineParametric3D_F64 line ) { // Line's slope is the cross product of the two normal vectors GeometryMath_F64 . cross ( a . A , a . B , a . C , b . A , b . B , b . C , line . slope ) ; if ( line . slope . normSq ( ) == 0 ) return false ; // Closest point on plane 'a' to origin (0,0,0) double n2 = a . A * a . A + a . B * a . B + a . C * a . C ; double closestX = a . A * a . D / n2 ; double closestY = a . B * a . D / n2 ; double closestZ = a . C * a . D / n2 ; // Cross product between normal of 'a' and the line's slope. This points towards the intersection double slopeX = a . B * line . slope . z - a . C * line . slope . y ; double slopeY = a . C * line . slope . x - a . A * line . slope . z ; double slopeZ = a . A * line . slope . y - a . B * line . slope . x ; // Now find the intersection of the plane and a line containing point 'closest' and pointing towards the // the intersection. double top = b . D - b . A * closestX - b . B * closestY - b . C * closestZ ; double bottom = b . A * slopeX + b . B * slopeY + b . C * slopeZ ; double d = top / bottom ; line . p . x = closestX + d * slopeX ; line . p . y = closestY + d * slopeY ; line . p . z = closestZ + d * slopeZ ; return true ; }
Finds the line which is the intersection between the two planes . For a valid solution to be returned the planes must not be parallel to each other . If the planes are parallel then the slope of the returned line will have a value of zero for each element .