Search code examples
mathlanguage-agnosticplane

Line of intersection between two planes


How can I find the line of intersection between two planes?

I know the mathematics idea, and I did the cross product between the the planes normal vectors

but how to get the line from the resulted vector programmatically


Solution

  • Finding the line between two planes can be calculated using a simplified version of the 3-plane intersection algorithm.

    The 2'nd, "more robust method" from bobobobo's answer references the 3-plane intersection.

    While this works well for 2 planes (where the 3rd plane can be calculated using the cross product of the first two), the problem can be further reduced for the 2-plane version.

    • No need to use a 3x3 matrix determinant,
      instead we can use the squared length of the cross product between the first and second plane (which is the direction of the 3'rd plane).
    • No need to include the 3rd planes distance,
      (calculating the final location).
    • No need to negate the distances.
      Save some cpu-cycles by swapping the cross product order instead.

    Including this code-example, since it may not be immediately obvious.

    // Intersection of 2-planes: a variation based on the 3-plane version.
    // see: Graphics Gems 1 pg 305
    //
    // Note that the 'normal' components of the planes need not be unit length
    bool isect_plane_plane_to_normal_ray(
            const Plane& p1, const Plane& p2,
            // output args
            Vector3f& r_point, Vector3f& r_normal)
    {
        // logically the 3rd plane, but we only use the normal component.
        const Vector3f p3_normal = p1.normal.cross(p2.normal);
        const float det = p3_normal.length_squared();
        
        // If the determinant is 0, that means parallel planes, no intersection.
        // note: you may want to check against an epsilon value here.
        if (det != 0.0) {
            // calculate the final (point, normal)
            r_point = ((p3_normal.cross(p2.normal) * p1.d) +
                       (p1.normal.cross(p3_normal) * p2.d)) / det;
            r_normal = p3_normal;
            return true;
        }
        else {
            return false;
        }
    }
    

    Adding this answer for completeness, since at time of writing, none of the answers here contain a working code-example which directly addresses the question.

    Though other answers here already covered the principles.