A plane can be defined in several ways. A point on the plane and a normal vector are usually used. To get a normal vector of three points ( P1
, P2
, P3
), we take the transverse 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 opposite, to go from point P1
and normal N
to three points, you start from any direction G
not in normal N
, so DOT(G,N)!=0
. Two orthogonal directions along the plane 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;
The line is determined by the point and direction. Usually two points ( Q1
, Q2
) define a line
Q1 = {x1, y1, z1}; Q2 = {x2, y2, z2}; E = UNIT( Q2-Q1 ); Line L = { Q1, E }
The intersection of the line and the plane is determined by a point on the line r=Q1+t*E
, which 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 location as
r = Q1+(t*E);
NOTE. DOT()
returns the dot product of two vectors, CROSS()
cross product and UNIT()
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]);