public final class SphericalCoordinates extends Object implements Spatial
Spherical coordinates for a point are defined by three values:
x = r cos(θ) sin(Φ) y = r sin(θ) sin(Φ) z = r cos(Φ) r = √(x^2 + y^2 + z^2) θ = atan2(y, x) Φ = acos(z/r)where r is the radius, θ is the azimuth angle, and Φ is the polar angle of the spherical coordinates.
There are numerous, competing conventions for the symbols used to represent spherical coordinate values. For
example, the mathematical convention is to use (r, θ, Φ) to represent radius, azimuth angle, and
polar angle, whereas the physics convention flips the angle values and uses (r, Φ, θ). As such,
this class avoids the use of these symbols altogether in favor of the less ambiguous formal names of the values,
e.g. radius
, azimuth
, and polar
.
In order to ensure the uniqueness of coordinate sets, coordinate values
are normalized so that radius
is in the range [0, +Infinity)
,
azimuth
is in the range [0, 2pi)
, and polar
is in the
range [0, pi]
.
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other)
Test for the equality of two sets of spherical coordinates.
|
static SphericalCoordinates |
fromCartesian(double x,
double y,
double z)
Convert the given set of Cartesian coordinates to spherical coordinates.
|
static SphericalCoordinates |
fromCartesian(Vector3D vec)
Convert the given set of Cartesian coordinates to spherical coordinates.
|
double |
getAzimuth()
Return the azimuth angle in radians.
|
int |
getDimension() |
double |
getPolar()
Return the polar angle in radians.
|
double |
getRadius()
Return the radius value.
|
int |
hashCode()
Get a hashCode for this set of spherical coordinates.
|
boolean |
isFinite() |
boolean |
isInfinite() |
boolean |
isNaN() |
static double |
normalizeAzimuth(double azimuth)
Normalize an azimuth value to be within the range
[0, 2pi) . |
static double |
normalizePolar(double polar)
Normalize a polar value to be within the range
[0, +pi] . |
static SphericalCoordinates |
of(double radius,
double azimuth,
double polar)
Return a new instance with the given spherical coordinate values.
|
static SphericalCoordinates |
parse(String input)
Parse the given string and return a new
SphericalCoordinates instance. |
static Vector3D |
toCartesian(double radius,
double azimuth,
double polar)
Convert the given set of spherical coordinates to Cartesian coordinates.
|
String |
toString() |
Vector3D |
toVector()
Convert this set of spherical coordinates to a Cartesian form.
|
public double getRadius()
[0, +Infinity)
.public double getAzimuth()
[0, 2pi)
.public double getPolar()
[0, pi]
.public int getDimension()
getDimension
in interface Spatial
public boolean isInfinite()
isInfinite
in interface Spatial
public Vector3D toVector()
public int hashCode()
All NaN values have the same hash code.
public boolean equals(Object other)
If all values of two sets of coordinates are exactly the same, and none are
Double.NaN
, the two sets are considered to be equal.
NaN
values are considered to globally affect the coordinates
and be equal to each other - i.e, if any (or all) values of the
coordinate set are equal to Double.NaN
, the set as a whole
is considered to equal NaN.
public static SphericalCoordinates of(double radius, double azimuth, double polar)
radius
lies in the range [0, +Infinity)
, azimuth
lies in the range
[0, 2pi)
, and polar
lies in the range [0, +pi]
.radius
- the length of the line segment from the origin to the coordinate point.azimuth
- the angle in the x-y plane, measured in radians counter-clockwise
from the positive x-axis.polar
- the angle in radians between the positive z-axis and the ray from the origin
to the coordinate point.SphericalCoordinates
instance representing the same point as the given set of
spherical coordinates.public static SphericalCoordinates fromCartesian(double x, double y, double z)
x
- X coordinate valuey
- Y coordinate valuez
- Z coordinate valuepublic static SphericalCoordinates fromCartesian(Vector3D vec)
vec
- vector containing Cartesian coordinates to convertpublic static Vector3D toCartesian(double radius, double azimuth, double polar)
radius
- The spherical radius value.azimuth
- The spherical azimuth angle in radians.polar
- The spherical polar angle in radians.public static SphericalCoordinates parse(String input)
SphericalCoordinates
instance. The parsed
coordinate values are normalized as in the of(double, double, double)
method.
The expected string format is the same as that returned by toString()
.input
- the string to parseSphericalCoordinates
instanceIllegalArgumentException
- if the string format is invalid.public static double normalizeAzimuth(double azimuth)
[0, 2pi)
. This
is exactly equivalent to PolarCoordinates.normalizeAzimuth(double)
.azimuth
- azimuth value in radians[0, 2pi)
.PolarCoordinates.normalizeAzimuth(double)
public static double normalizePolar(double polar)
[0, +pi]
. Since the
polar angle is the angle between two vectors (the zenith direction and the
point vector), the sign of the angle is not significant as in the azimuth angle.
For example, a polar angle of -pi/2
and one of +pi/2
will both
normalize to pi/2
.polar
- polar value in radians[0, +pi]
Copyright © 2016–2021 The Apache Software Foundation. All rights reserved.