Package edu.cnu.mdi.geometry
Class Sphere
java.lang.Object
edu.cnu.mdi.geometry.Sphere
A sphere in three-dimensional Cartesian space.
Distances are measured relative to the sphere surface: signed distances are negative inside the sphere, zero on the surface, and positive outside the sphere.
- Author:
- heddle
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondoubledistance(double x, double y, double z) Computes the absolute shortest distance from coordinates to the sphere surface.Gets the center of the sphere.doubleGets the radius of the sphere.booleanisInside(double x, double y, double z) Tests whether coordinates are strictly inside the sphere.booleansegmentIntersects(double x1, double y1, double z1, double x2, double y2, double z2) Tests whether a finite line segment intersects the sphere.doublesignedDistance(double x, double y, double z) Computes the signed shortest distance from coordinates to the sphere surface.doubleComputes the signed shortest distance from a point to the sphere surface.
-
Constructor Details
-
Sphere
Creates a sphere with the given center and radius.- Parameters:
center- the center of the sphereradius- the radius of the sphere; must be non-negative- Throws:
NullPointerException- ifcenterisnullIllegalArgumentException- ifradiusis negative or non-finite, or a center coordinate is non-finite
-
Sphere
public Sphere(double[] center, double radius) Creates a sphere from an xyz center array and a radius.- Parameters:
center- an array containing the center coordinatesx,y, andzradius- the radius of the sphere; must be non-negative- Throws:
NullPointerException- ifcenterisnullIndexOutOfBoundsException- ifcenter.length < 3IllegalArgumentException- ifradiusis negative or non-finite, or a center coordinate is non-finite
-
Sphere
public Sphere(double radius) Creates a sphere centered at the origin.- Parameters:
radius- the radius of the sphere; must be non-negative- Throws:
IllegalArgumentException- ifradiusis negative or non-finite
-
-
Method Details
-
getCenter
Gets the center of the sphere.- Returns:
- a copy of the center point
-
getRadius
public double getRadius()Gets the radius of the sphere.- Returns:
- the radius of the sphere
-
signedDistance
Computes the signed shortest distance from a point to the sphere surface.- Parameters:
p- the point- Returns:
- negative inside the sphere, zero on the surface, and positive outside
- Throws:
NullPointerException- ifpisnull
-
signedDistance
public double signedDistance(double x, double y, double z) Computes the signed shortest distance from coordinates to the sphere surface.- Parameters:
x- the x coordinatey- the y coordinatez- the z coordinate- Returns:
- negative inside the sphere, zero on the surface, and positive outside
-
distance
public double distance(double x, double y, double z) Computes the absolute shortest distance from coordinates to the sphere surface.- Parameters:
x- the x coordinatey- the y coordinatez- the z coordinate- Returns:
- the absolute distance to the sphere surface
-
isInside
public boolean isInside(double x, double y, double z) Tests whether coordinates are strictly inside the sphere.- Parameters:
x- the x coordinatey- the y coordinatez- the z coordinate- Returns:
trueif the point is strictly inside the sphere
-
segmentIntersects
public boolean segmentIntersects(double x1, double y1, double z1, double x2, double y2, double z2) Tests whether a finite line segment intersects the sphere.Tangency counts as intersection.
- Parameters:
x1- the x coordinate of one segment endpointy1- the y coordinate of one segment endpointz1- the z coordinate of one segment endpointx2- the x coordinate of the other segment endpointy2- the y coordinate of the other segment endpointz2- the z coordinate of the other segment endpoint- Returns:
trueif the segment intersects or is tangent to the sphere
-