Here is a function in C++ to find the intersection of a plane and a line in 3-space:
Point3D* LinePlaneIntersection(DirectedPoint3D line, DirectedPoint3D plane)
{
// Returns the point of intersection with the plane.
// Assumes the line and plane are not parallel.
Point3D *p; // Return point pointer.
float t = 0; // Parameter t of p along the input line.
float sfNumerator = 0;
float sfDenominator = 0;
float x0 = line.getPoint()->getX();
float y0 = line.getPoint()->getY();
float z0 = line.getPoint()->getZ();
float dx = line.getDirection()->getX();
float dy = line.getDirection()->getY();
float dz = line.getDirection()->getZ();
float Qx = plane.getPoint()->getX();
float Qy = plane.getPoint()->getY();
float Qz = plane.getPoint()->getZ();
float Vx = plane.getDirection()->getX();
float Vy = plane.getDirection()->getY();
float Vz = plane.getDirection()->getZ();
// Solve the plane and line equations for parameter t:
sfNumerator = Vx * (Qx - x0) + Vy * (Qy - y0) + Vz * (Qz - z0);
sfDenominator = Vx * dx + Vy * dy + Vz * dz;
if (sfDenominator == 0)
{
cout << "Error in LinePlaneIntersection: Attempted division by zero." << endl;
t = 1 / gsfEpsilon; // Ten billion is close enough to infinity.
}
else
{
t = sfNumerator / sfDenominator;
}
x0 *= t;
y0 *= t;
z0 *= t;
p = new Point3D(x0, y0, z0);
return p;
}