Point and sphere
Given a point and a sphere there are two operations we want to perform. First, we want to check whether a test point is inside the sphere or not. Alternately, we may want to get the closest point to the test point along the surface of the sphere:
Getting ready
To test whether a point is within a sphere we have to compare the distance from the center of the sphere and the test point to the radius of the sphere. If the distance is less than the radius, the sphere contains the point. We can get the point on the surface of the sphere closest to a test point by obtaining a vector that points from the center of the sphere to the test point. This vector should have the same magnitude as the radius of the sphere.
How to do it…
Perform the following steps to implement point tests for a sphere:
Declare
PointInSphere
andClosestPoint
inGeometry3D.h
:bool PointInSphere(const Point& point, const Sphere& sphere); Point ClosestPoint(const Sphere& sphere, const Point&...