Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line/Plane intersection based on points

I have two points in space, L1 and L2 that defines two points on a line.

I have three points in space, P1, P2 and P3 that 3 points on a plane.

So given these inputs, at what point does the line intersect the plane?

Fx. the plane equation A*x+B*y+C*z+D=0 is:

A = p1.Y * (p2.Z - p3.Z) + p2.Y * (p3.Z - p1.Z) + p3.Y * (p1.Z - p2.Z)
B = p1.Z * (p2.X - p3.X) + p2.Z * (p3.X - p1.X) + p3.Z * (p1.X - p2.X)
C = p1.X * (p2.Y - p3.Y) + p2.X * (p3.Y - p1.Y) + p3.X * (p1.Y - p2.Y)
D = -(p1.X * (p2.Y * p3.Z - p3.Y * p2.Z) + p2.X * (p3.Y * p1.Z - p1.Y * p3.Z) + p3.X * (p1.Y * p2.Z - p2.Y * p1.Z))

But what about the rest?

like image 740
Morten Nielsen Avatar asked Feb 08 '11 20:02

Morten Nielsen


2 Answers

The simplest (and very generalizable) way to solve this is to say that

L1 + x*(L2 - L1) = (P1 + y*(P2 - P1)) + (P1 + z*(P3 - P1))

which gives you 3 equations in 3 variables. Solve for x, y and z, and then substitute back into either of the original equations to get your answer. This can be generalized to do complex things like find the point that is the intersection of two planes in 4 dimensions.

For an alternate approach, the cross product N of (P2-P1) and (P3-P1) is a vector that is at right angles to the plane. This means that the plane can be defined as the set of points P such that the dot product of P and N is the dot product of P1 and N. Solving for x such that (L1 + x*(L2 - L1)) dot N is this constant gives you one equation in one variable that is easy to solve. If you're going to be intersecting a lot of lines with this plane, this approach is definitely worthwhile.

Written out explicitly this gives:

N = cross(P2-P1, P3 - P1)
Answer = L1 + (dot(N, P1 - L1) / dot(N, L2 - L1)) * (L2 - L1)

where

cross([x, y, z], [u, v, w]) = x*u + y*w + z*u - x*w - y*u - z*v
dot([x, y, z], [u, v, w]) = x*u + y*v + z*w

Note that that cross product trick only works in 3 dimensions, and only for your specific problem of a plane and a line.

like image 174
btilly Avatar answered Sep 27 '22 21:09

btilly


This is how I ended up doing it in come code. Luckily one code library (XNA) had half of what I needed, and the rest was easy.

var lv = L2-L1;
var ray = new Microsoft.Xna.Framework.Ray(L1,lv);
var plane = new Microsoft.Xna.Framework.Plane(P1, P2, P3);

var t = ray.Intersects(plane); //Distance along line from L1
///Result:
var x = L1.X + t * lv.X;
var y = L1.Y + t * lv.Y;
var z = L1.Z + t * lv.Z;

Of course I would prefer having just the simple equations that takes place under the covers of XNA.

like image 26
Morten Nielsen Avatar answered Sep 27 '22 21:09

Morten Nielsen