Intersection of Two Planes John Krumm Microsoft Research Redmond, WA, USA 5/15/00 Everyone knows that the intersection of two planes in 3D is a line, and it’s easy to compute the line’s parameters. But, the cookbook formulae for the line are not necessarily the best nor most intuitive way of representing the line. This short note gives the derivation and the formula that I came up with when I was faced with this problem and couldn't find a solution l liked anywhere. It is also a simple example of how to use Lagrange multipliers. The line of intersection is normally represented as a point on the line p ( p x , p y , p z ) and a direction vector n (n x , n y , n z ) emanating from this point. While the direction vector, n , is easy to compute and obvious to represent (can be normalized to unit length), the point on the line, p , can legitimately be any point on the line. Mathematically it doesn't matter what the point is, as long as it's on the line. But it is easier to understand the line if the point is somehow canonical or somehow close to the other points we are concerned with. The formula below gives the point on the line that is closest to some other point p o that can be specified by the user. The point p o could be the origin, giving a canonical representation of the line. Or it could be some other point, like the midpoint of a line connecting points on the two planes, giving a point on the line that is nearby the other points in the problem's representation. At the end of this note I give examples of both kinds of choices for p o . The two planes are represented by their normal vectors, n1 (n1x , n1 y , n1z ) and n2 (n2 x , n2 y , n2 z ) , and points on the two planes, p1 ( p1x , p1 y , p1z ) and p 2 ( p 2 x , p 2 y , p 2 z ) . The direction vector of the line of intersection is the cross product of the two normal vectors, i.e. n n1 n2 . This can be normalized for a canonical representation, but the normalization is not necessary for what follows. We will compute the point on the line, p , as the point on the line that is closest to some arbitrarily chosen point p o ( p 0 x , p 0 y , p 0 z ) . This point could be the origin ( po (0,0,0) ) or any other point. In the second example below we choose p o to be the midpoint of a line segment connecting p1 and p 2 . The point p must be on both planes, so we have two constraints: (1) ( p p1 ) n1 0 ( p p 2 ) n2 0 (2) The point p should also be as close as possible to the point p o , so we want to minimize the distance between the two. The distance is p p0 2 p x p0 x p y p0 y p z p0 z 2 2 2 (3) This is a textbook example of a problem for Lagrange multipliers, with one objective function (Equation (3)) and two constraints (Equations (1) and (2)). The function w containing the constraints and objective function is 2 w p p0 ( p p1 ) n1 ( p p 2 ) n2 ( p x p0 x ) 2 ( p y p0 y ) 2 ( p z p0 z ) 2 (4) p x n1x p y n1 y p z n1z p1 n1 p x n2 x p y n2 y p z n2 z p 2 n2 Here and are the two Lagrange multipliers (one for each constraint). The expanded form of w has been written to make the next step easier, which is the partial derivatives. Continuing the recipe for Lagrange multipliers, we compute the partial derivatives and set them to zero. w 2 p x p 0 x n1 x n2 x 0 p x w 2 p y p 0 y n1 y n2 y 0 p y w 2 p z p 0 z n1z n2 z 0 p z (5) w p x n1 x p y n1 y p z n1 z p1 n1 0 w p x n2 x p y n2 y p z n2 z p 2 n2 0 We are interested in solving these equations for p ( p x , p y , p z ) , but we will also have to solve for the Lagrange multipliers. Equation (5) represents five linear equations in five unknowns. In matrix form, the five equations are 0 0 n1x n2 x p x 2 p0 x 2 0 2 0 n n 2 p p 1 y 2 y 0 y y 0 0 2 n1z n2 z p z 2 p0 z (6) 0 p1 n1 n1x n1 y n1z 0 n2 x n2 y n2 z 0 0 p2 n2 T Solving this matrix equation for the unknown vector px , px , pz , , gives the point on the line closest to p o . We don’t care about the values of the two Lagrange multipliers, but we have to compute them anyway. Example 1 – Simple This is a simple example just to see if the solution confirms what we know is the right answer. Suppose one of the two planes corresponds to the xy plane. We can choose n1 0,0,3 and p1 7,11,0 . Suppose that the second plane cuts through the x axis at 1,0,0 , cuts through the y axis at 0,1,0 and is parallel to the z axis. A point on this plane is p 1,0,0 and its normal is n 5,5,0 . This is illustrated in Figure 1. We will 2 2 find the point of intersection closest to the origin, thus po (0,0,0) . The resulting line of intersection should have a direction of 1,1,0 . The computed direction of the line of intersection is n n1 n2 15,15,0 , which is along the direction we expected. From the figure we can tell that the point on the line of intersection closest to the origin is 0.5,0.5,0 . The matrix equation becomes 2 0 0 0 5 p x 0 0 2 0 0 5 p 0 y 0 0 2 3 0 p z 0 (7) 0 0 3 0 0 0 5 5 0 0 0 5 The solution is p x , p x , p z , , 0.5,0.5,0,0,0.2 , which confirms our intuition for the point on the line closest to the origin. T y 1.2 1 0.8 0.6 0.4 0.2 x -0.2 0.2 0.4 0.6 0.8 1 1.2 -0.2 Figure 1: Plane 1 is the xy plane, and plane 2 cuts across to make the diagonal line. Example – Two Planar Patches This is an example that might come from a computer graphics application that uses polygons to model a 3D object. As shown in Figure 2, there are two triangular polygons that share a common edge. We want to find the line that represents this edge. The vertices of the two polygons are 0,0,0, 4,1,1, 4,1,1 for the first one and 0,0,0, 4,1,1, 4,1,1 for the second one. The normals of the two planes are (from cross products of their vertices) n1 2,8,0 and n2 2,0,8 . Points on the two planes are (from the means of their vertices) p1 8 , 2 ,0 and p 2 8 ,0, 2 . We want 3 3 3 3 to find a point on the line of intersection that is close to the two polygons, so we will pick p o as the midpoint of p1 and p 2 : a line segment connecting p1 and po p1 p 2 / 2 8 , 1 , 1 . The figure shows 3 3 3 p 2 , and p o is the midpoint of this line segment. The direction vector of the line of intersection is 1 n n1 n2 64,16,16 4,1,1 , which points along the intersection as it 16 should. Plugging in everything to the matrix equation, we have 2 0 0 2 2 p x 16 3 0 2 0 8 0 p 2 y 3 0 0 2 0 8 p z 2 3 2 8 0 0 0 0 2 0 8 0 0 0 The solution is p x , p x , p z , , T (8) 1 68,17,17,2,2 2.52,0.63,0.63,0.07,0.07 . 27 Figure 2: Two planar patches intersect along a line. We find a point on the line that is closest to the midpoint of a line connecting the two patches.