Class PolarCoordinates
- java.lang.Object
-
- org.apache.commons.geometry.euclidean.twod.PolarCoordinates
-
- All Implemented Interfaces:
Spatial
public final class PolarCoordinates extends java.lang.Object implements Spatial
Class representing polar coordinates in 2 dimensional Euclidean space.Polar coordinates are defined by a distance from a reference point and an angle from a reference direction. The distance value is called the radial coordinate, or radius, and the angle is called the angular coordinate, or azimuth. This class follows the standard mathematical convention of using the positive x-axis as the reference direction and measuring positive angles counter-clockwise, toward the positive y-axis. The origin is used as the reference point. Polar coordinate are related to Cartesian coordinates as follows:
x = r * cos(θ) y = r * sin(θ) r = √(x^2 + y^2) θ = atan2(y, x)
where r is the radius and θ is the azimuth of the polar coordinates.In order to ensure the uniqueness of coordinate sets, coordinate values are normalized so that
radiusis in the range[0, +Infinity)andazimuthis in the range[0, 2pi).- See Also:
- Polar Coordinate System
-
-
Constructor Summary
Constructors Modifier Constructor Description privatePolarCoordinates(double radius, double azimuth)Simple constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object other)Test for the equality of two sets of polar coordinates.static PolarCoordinatesfromCartesian(double x, double y)Convert the given Cartesian coordinates to polar form.static PolarCoordinatesfromCartesian(Vector2D vec)Convert the given Cartesian coordinates to polar form.doublegetAzimuth()Return the azimuth angle in radians.intgetDimension()Returns the number of dimensions in the space that this element belongs to.doublegetRadius()Return the radius value.inthashCode()Get a hashCode for this set of polar coordinates.booleanisFinite()Returns true if all values in this element are finite, meaning they are not NaN or infinite.booleanisInfinite()Returns true if any value in this element is infinite and none are NaN; otherwise, returns false.booleanisNaN()Returns true if any value in this element is NaN; otherwise returns false.static doublenormalizeAzimuth(double azimuth)Normalize an azimuth value to be within the range[0, 2pi).static PolarCoordinatesof(double radius, double azimuth)Return a new instance with the given polar coordinate values.static PolarCoordinatesparse(java.lang.String input)Parse the given string and return a new polar coordinates instance.Vector2DtoCartesian()Convert this set of polar coordinates to Cartesian coordinates.static Vector2DtoCartesian(double radius, double azimuth)Convert the given polar coordinates to Cartesian form.java.lang.StringtoString()
-
-
-
Method Detail
-
getRadius
public double getRadius()
Return the radius value. The value will be greater than or equal to 0.- Returns:
- radius value
-
getAzimuth
public double getAzimuth()
Return the azimuth angle in radians. The value will be in the range[0, 2pi).- Returns:
- azimuth value in radians.
-
getDimension
public int getDimension()
Returns the number of dimensions in the space that this element belongs to.- Specified by:
getDimensionin interfaceSpatial- Returns:
- the number of dimensions in the element's space
-
isNaN
public boolean isNaN()
Returns true if any value in this element is NaN; otherwise returns false.
-
isInfinite
public boolean isInfinite()
Returns true if any value in this element is infinite and none are NaN; otherwise, returns false.- Specified by:
isInfinitein interfaceSpatial- Returns:
- true if any value in this element is infinite and none are NaN
-
isFinite
public boolean isFinite()
Returns true if all values in this element are finite, meaning they are not NaN or infinite.
-
toCartesian
public Vector2D toCartesian()
Convert this set of polar coordinates to Cartesian coordinates.- Returns:
- A 2-dimensional vector with an equivalent set of coordinates in Cartesian form
-
hashCode
public int hashCode()
Get a hashCode for this set of polar coordinates.All NaN values have the same hash code.
- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code value for this object
-
equals
public boolean equals(java.lang.Object other)
Test for the equality of two sets of polar coordinates.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.NaNvalues are considered to globally affect the coordinates and be equal to each other - i.e, if either (or all) values of the coordinate set are equal toDouble.NaN, the set as a whole is considered to equalNaN.- Overrides:
equalsin classjava.lang.Object- Parameters:
other- Object to test for equality to this- Returns:
- true if two PolarCoordinates objects are equal, false if object is null, not an instance of PolarCoordinates, or not equal to this PolarCoordinates instance
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
of
public static PolarCoordinates of(double radius, double azimuth)
Return a new instance with the given polar coordinate values. The values are normalized so thatradiuslies in the range[0, +Infinity)andazimuthin the range[0, 2pi).- Parameters:
radius- Radius value.azimuth- Azimuth angle in radians.- Returns:
- new
PolarCoordinatesinstance
-
fromCartesian
public static PolarCoordinates fromCartesian(double x, double y)
Convert the given Cartesian coordinates to polar form.- Parameters:
x- X coordinate valuey- Y coordinate value- Returns:
- polar coordinates equivalent to the given Cartesian coordinates
-
fromCartesian
public static PolarCoordinates fromCartesian(Vector2D vec)
Convert the given Cartesian coordinates to polar form.- Parameters:
vec- vector containing Cartesian coordinates- Returns:
- polar coordinates equivalent to the given Cartesian coordinates
-
toCartesian
public static Vector2D toCartesian(double radius, double azimuth)
Convert the given polar coordinates to Cartesian form.- Parameters:
radius- Radius value.azimuth- Azimuth angle in radians.- Returns:
- A 2-dimensional vector with an equivalent set of coordinates in Cartesian form
-
parse
public static PolarCoordinates parse(java.lang.String input)
Parse the given string and return a new polar coordinates instance. The parsed coordinates are normalized as in theof(double, double)method. The expected string format is the same as that returned bytoString().- Parameters:
input- the string to parse- Returns:
- new
PolarCoordinatesinstance - Throws:
java.lang.IllegalArgumentException- if the string format is invalid.
-
normalizeAzimuth
public static double normalizeAzimuth(double azimuth)
Normalize an azimuth value to be within the range[0, 2pi).- Parameters:
azimuth- azimuth value in radians- Returns:
- equivalent azimuth value in the range
[0, 2pi).
-
-