I\'m trying to implement at line-plane intersection algorithm. According to Wikipedia I need three non-colinear points on the plane to do that.
I therefore tried imp
A plane can be defined with several ways. Typically a point on the plane and a normal vector is used. To get the normal vector from three points (P1
, P2
, P3
) take the cross product of the side of the triangle
P1 = {x1, y1, z1};
P2 = {x2, y2, z2};
P3 = {x3, y3, z3};
N = UNIT( CROSS( P2-P1, P3-P1 ) );
Plane P = { P1, N }
The reverse, to go from a point P1
and normal N
to three points, you start from any direction G
not along the normal N
such that DOT(G,N)!=0
. The two orthogonal directions along the plane are then
//try G={0,0,1} or {0,1,0} or {1,0,0}
G = {0,0,1};
if( MAG(CROSS(G,N))<TINY ) { G = {0,1,0}; }
if( MAG(CROSS(G,N))<TINY ) { G = {1,0,0}; }
U = UNIT( CROSS(N, G) );
V = CROSS(U,N);
P2 = P1 + U;
P3 = P1 + V;
A line is defined by a point and a direction. Typically two points (Q1
, Q2
) define the line
Q1 = {x1, y1, z1};
Q2 = {x2, y2, z2};
E = UNIT( Q2-Q1 );
Line L = { Q1, E }
The intersection of the line and plane are defined by the point on the line r=Q1+t*E
that intersects the plane such that DOT(r-P1,N)=0
. This is solved for the scalar distance t
along the line as
t = DOT(P1-Q1,N)/DOT(E,N);
and the location as
r = Q1+(t*E);
NOTE: The DOT()
returns the dot-product of two vector, CROSS()
the cross-product, and UNIT()
the unit vector (with magnitude=1).
DOT(P,Q) = P[0]*Q[0]+P[1]*Q[1]+P[2]*Q[2];
CROSS(P,Q) = { P[1]*Q[2]-P[2]*Q[1], P[2]*Q[0]-P[0]*Q[2], P[0]*Q[1]-P[1]*Q[0] };
UNIT(P) = {P[0]/sqrt(DOT(P,P)), P[1]/sqrt(DOT(P,P)), P[2]/sqrt(DOT(P,P))};
t*P = { t*P[0], t*P[1], t*P[2] };
MAG(P) = sqrt(P[0]*P[0]+P[1]*P[1]+P[2]*P[2]);
Where N=(Nx,Ny,Nz)
is the normal, you could project the points N
, (Ny,Nz,Nx)
, (Nz,Nx,Ny)
onto the plane: they're guaranteed to be distinct.
Alternatively, if P
and Q
are on the plane, P+t(Q-P)xN
is also on the plane for any t!=0
where x
is the cross product.
Alternatively if M!=N
is an arbitrary vector, K=MxN
and L=KxN
are colinear with the plane and any point p
on the plane can be written as p=Origin+sK+tL
for some s,t
.
One approach you may find easy to implement is to see where the plane intersects the coordinate axes. For the plane given by the equationaX + bY + cZ - d = 0
hold two variables at 0 and solve for the third. So the solutions would be (assuming a
, b
, c
, and d
are all non-zero):
(d/a, 0, 0)
(0, d/b, 0)
(0, 0, d/c)
You will need to consider the cases where one or more of the coefficients are 0 so you don't get a degenerate or colinear solutions. As an example if exactly one of the coefficients is 0 (say a=0
) you instead use
(1, d/b, 0)
(0, d/b, 0)
(0, 0, d/c)
If exactly two of the coefficients are 0 (say a=0
and b=0
) you can use:
(1, 0, d/c)
(0, 1, d/c)
(0, 0, d/c)
If d=0
, the plane intersects the three axes at the origin, and so you can use:
(1, 0, -a/c)
(0, -c/b, 1)
(-b/a, 1, 0)
You will need to work out simular cases for d
and exactly one other coefficient being 0, as well as d
and two others being 0. There should be a total of 16 cases, but there are a few things that come to mind which should make that somewhat more manageable.