Example #1
0
// Returns the point of intersection of a line and a plane
Vector3d LinePlaneIntersectionPoint(Vector3d normal, Vector3d lineStart, Vector3d lineEnd, float distance) {
	Vector3d intersectionPoint;
	Vector3d lineDirection;
	double Numerator = 0.0, Denominator = 0.0, dist = 0.0;

	lineDirection = lineEnd - lineStart;  // Get the Vector of the line
	lineDirection = lineDirection.GetUnit();  // Normalize the line vector

	// Use the plane equation with the normal and the line
	Numerator = -(normal.x * lineStart.x +
				  normal.y * lineStart.y +
				  normal.z * lineStart.z + distance);

	Denominator = Vector3d::DotProduct(normal, lineDirection);

	if( Denominator == 0.0)	 // Dont divide by zero
		return lineStart;

	dist = Numerator / Denominator;

	// Travel along the lineDirection by dist and add this to the line start
	intersectionPoint.x = (float)(lineStart.x + (lineDirection.x * dist));
	intersectionPoint.y = (float)(lineStart.y + (lineDirection.y * dist));
	intersectionPoint.z = (float)(lineStart.z + (lineDirection.z * dist));

	return intersectionPoint;
}