Class Sphere

java.lang.Object
edu.cnu.mdi.geometry.Sphere

public class Sphere extends Object
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
    Constructor
    Description
    Sphere(double radius)
    Creates a sphere centered at the origin.
    Sphere(double[] center, double radius)
    Creates a sphere from an xyz center array and a radius.
    Sphere(Point center, double radius)
    Creates a sphere with the given center and radius.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    distance(double x, double y, double z)
    Computes the absolute shortest distance from coordinates to the sphere surface.
    Gets the center of the sphere.
    double
    Gets the radius of the sphere.
    boolean
    isInside(double x, double y, double z)
    Tests whether coordinates are strictly inside the sphere.
    boolean
    segmentIntersects(double x1, double y1, double z1, double x2, double y2, double z2)
    Tests whether a finite line segment intersects the sphere.
    double
    signedDistance(double x, double y, double z)
    Computes the signed shortest distance from coordinates to the sphere surface.
    double
    Computes the signed shortest distance from a point to the sphere surface.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Sphere

      public Sphere(Point center, double radius)
      Creates a sphere with the given center and radius.
      Parameters:
      center - the center of the sphere
      radius - the radius of the sphere; must be non-negative
      Throws:
      NullPointerException - if center is null
      IllegalArgumentException - if radius is 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 coordinates x, y, and z
      radius - the radius of the sphere; must be non-negative
      Throws:
      NullPointerException - if center is null
      IndexOutOfBoundsException - if center.length < 3
      IllegalArgumentException - if radius is 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 - if radius is negative or non-finite
  • Method Details

    • getCenter

      public Point 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

      public double signedDistance(Point p)
      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 - if p is null
    • 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 coordinate
      y - the y coordinate
      z - 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 coordinate
      y - the y coordinate
      z - 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 coordinate
      y - the y coordinate
      z - the z coordinate
      Returns:
      true if 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 endpoint
      y1 - the y coordinate of one segment endpoint
      z1 - the z coordinate of one segment endpoint
      x2 - the x coordinate of the other segment endpoint
      y2 - the y coordinate of the other segment endpoint
      z2 - the z coordinate of the other segment endpoint
      Returns:
      true if the segment intersects or is tangent to the sphere