- Object
-
- Rotation
-
public class Rotation extends Object implements Copyable<Rotation>
This class represents a rotation (in 2D space). The aim of this class is to reduce as much as possible the use of trigonometric function calls (like Math.sin/cos or Math.atan2) because the majority of those are very slow to compute. This can be achieved by pre-computing the sin and cos of the angle of the rotation. A Rotation object is essentially a vector with norm 1. This class encapsulates the above so the user need not directly use and compute those trigonometric values. This also provides implicit validation as the user cannot create a Rotation with invalid values (values not derived from cos/sin of some angle). The receiver of a Rotation object can be sure it always represents a valid rotation.- Since:
- 3.4.0
- Version:
- 5.0.0
- Author:
- Manolis Tsamis
-
-
Constructor Summary
Constructors Modifier Constructor Description Rotation()
Default constructor.Rotation(double angle)
Creates aRotation
from the given angle.protected
Rotation(double cost, double sint)
Internal constructor that directly sets the cost and sint fields of theRotation
without additional validation.Rotation(Rotation rotation)
Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compare(Rotation other)
Compares thisRotation
with another one, based on the angle between them (The one with -π ≤ θ ≤ π) Returns 1 if θ > 0, -1 if θ < 0 and 0 otherwiseint
compare(Vector2 other)
Rotation
copy()
Returns a deep copy of this object.double
cross(Rotation rotation)
double
dot(Rotation rotation)
boolean
equals(double angle)
Returns true if the cos and sin components of thisRotation
are the same as the given angleboolean
equals(double angle, double error)
Returns true if the cos and sin components of thisRotation
are the same as the given angle given the specified error.boolean
equals(Object obj)
boolean
equals(Rotation rotation)
boolean
equals(Rotation rotation, double error)
double
getCost()
Returns the value of cos(θ) for thisRotation
.Rotation
getInversed()
Negates this rotation and returns a new rotation.Rotation
getRotated(double angle)
Rotates this rotation by the given angle and returns a new rotation.Rotation
getRotated(double c, double s)
Rotation
getRotated(Rotation rotation)
Rotates this rotation by the given rotation and returns a new rotation.Rotation
getRotated135()
Rotates this rotation 135 degrees and returns a new rotation.Rotation
getRotated180()
Rotates this rotation 180 degrees and returns a new rotation.Rotation
getRotated225()
Rotates this rotation 225 degrees and returns a new rotation.Rotation
getRotated270()
Rotates this rotation 270 degrees and returns a new rotation.Rotation
getRotated315()
Rotates this rotation 315 degrees and returns a new rotation.Rotation
getRotated45()
Rotates this rotation 45 degrees and returns a new rotation.Rotation
getRotated90()
Rotates this rotation 90 degrees and returns a new rotation.Rotation
getRotationBetween(Rotation rotation)
Rotation
getRotationBetween(Vector2 vector)
double
getSint()
Returns the value of sin(θ) for thisRotation
.int
hashCode()
Rotation
inverse()
Negates this rotation and returns this rotation.boolean
isIdentity()
Returns true if this rotation is an identity rotation.boolean
isIdentity(double error)
Returns true if this rotation is an identity rotation within the given error.static Rotation
of(double angle)
Alternative way to create a newRotation
from a given angle.static Rotation
of(double cost, double sint)
Static method to create aRotation
from a pair of values that lie on the unit circle; That is a pair of values (x, y) such that x = cos(θ), y = sin(θ) for some value θ This method is provided for the case where the cos and sin values are already computed and the overhead can be avoided.static Rotation
of(Transform transform)
static Rotation
of(Vector2 direction)
static Rotation
ofDegrees(double angle)
Alternative way to create a newRotation
from a given angle, in degrees.Rotation
rotate(double angle)
Rotates this rotation by the given angle and returns this rotation.Rotation
rotate(Rotation rotation)
Rotates this rotation by the given rotation and returns this rotation.Rotation
rotate135()
Rotates this rotation 135 degrees and returns this rotation.Rotation
rotate180()
Rotates this rotation 180 degrees and returns this rotation.Rotation
rotate225()
Rotates this rotation 225 degrees and returns this rotation.Rotation
rotate270()
Rotates this rotation 270 degrees and returns this rotation.Rotation
rotate315()
Rotates this rotation 315 degrees and returns this rotation.Rotation
rotate45()
Rotates this rotation 45 degrees and returns this rotation.Rotation
rotate90()
Rotates this rotation 90 degrees and returns this rotation.static Rotation
rotation0()
Creates a newRotation
of 0 degrees.static Rotation
rotation135()
Creates a newRotation
of 135 degrees.static Rotation
rotation180()
Creates a newRotation
of 180 degrees.static Rotation
rotation225()
Creates a newRotation
of 225 degrees.static Rotation
rotation270()
Creates a newRotation
of 270 degrees.static Rotation
rotation315()
Creates a newRotation
of 315 degrees.static Rotation
rotation45()
Creates a newRotation
of 45 degrees.static Rotation
rotation90()
Creates a newRotation
of 90 degrees.Rotation
set(double angle)
Sets thisRotation
to the given angle.Rotation
set(Rotation rotation)
Rotation
setIdentity()
Sets thisRotation
to be the identity.double
toDegrees()
Returns the angle in degrees for thisRotation
.double
toRadians()
Returns the angle in radians for thisRotation
.String
toString()
Vector2
toVector()
Returns thisRotation
as a unit length direction vector.Vector2
toVector(double magnitude)
Returns thisRotation
as a direction vector with the given magnitude.
-
-
-
Constructor Detail
-
Rotation
protected Rotation(double cost, double sint)
Internal constructor that directly sets the cost and sint fields of theRotation
without additional validation.- Parameters:
cost
- The cosine of some anglesint
- The sine of the same angle
-
Rotation
public Rotation()
Default constructor. Creates an identityRotation
.
-
Rotation
public Rotation(Rotation rotation)
Copy constructor.- Parameters:
rotation
- theRotation
to copy from
-
Rotation
public Rotation(double angle)
Creates aRotation
from the given angle.- Parameters:
angle
- the angle in radians
-
-
Method Detail
-
of
public static Rotation of(double angle)
Alternative way to create a newRotation
from a given angle.- Parameters:
angle
- in radians- Returns:
- A
Rotation
for that angle
-
ofDegrees
public static Rotation ofDegrees(double angle)
Alternative way to create a newRotation
from a given angle, in degrees.- Parameters:
angle
- in degrees- Returns:
- A
Rotation
for that angle
-
of
public static Rotation of(double cost, double sint)
Static method to create aRotation
from a pair of values that lie on the unit circle; That is a pair of values (x, y) such that x = cos(θ), y = sin(θ) for some value θ This method is provided for the case where the cos and sin values are already computed and the overhead can be avoided. This method will check whether those values are indeed on the unit circle and otherwise throw anIllegalArgumentException
.- Parameters:
cost
- The x value = cos(θ)sint
- The y value = sin(θ)- Returns:
- A
Rotation
defined by (cost, sint) - Throws:
IllegalArgumentException
- if (cost, sint) is not on the unit circle
-
rotation90
public static Rotation rotation90()
Creates a newRotation
of 90 degrees.- Returns:
Rotation
-
rotation180
public static Rotation rotation180()
Creates a newRotation
of 180 degrees.- Returns:
Rotation
-
rotation270
public static Rotation rotation270()
Creates a newRotation
of 270 degrees.- Returns:
Rotation
-
rotation45
public static Rotation rotation45()
Creates a newRotation
of 45 degrees.- Returns:
Rotation
-
rotation135
public static Rotation rotation135()
Creates a newRotation
of 135 degrees.- Returns:
Rotation
-
rotation225
public static Rotation rotation225()
Creates a newRotation
of 225 degrees.- Returns:
Rotation
-
rotation315
public static Rotation rotation315()
Creates a newRotation
of 315 degrees.- Returns:
Rotation
-
copy
public Rotation copy()
Description copied from interface:Copyable
Returns a deep copy of this object.
-
equals
public boolean equals(Rotation rotation)
- Parameters:
rotation
- theRotation
to compare to- Returns:
- boolean
-
equals
public boolean equals(Rotation rotation, double error)
Returns true if the cos and sin components of thisRotation
are the same as the givenRotation
given the specified error.- Parameters:
rotation
- theRotation
to compare toerror
- the error- Returns:
- boolean
-
equals
public boolean equals(double angle)
Returns true if the cos and sin components of thisRotation
are the same as the given angle- Parameters:
angle
- the angle in radians- Returns:
- boolean
-
equals
public boolean equals(double angle, double error)
Returns true if the cos and sin components of thisRotation
are the same as the given angle given the specified error.- Parameters:
angle
- the angle in radianserror
- the error- Returns:
- boolean
-
setIdentity
public Rotation setIdentity()
Sets thisRotation
to be the identity.- Returns:
Rotation
this rotation
-
set
public Rotation set(double angle)
Sets thisRotation
to the given angle.- Parameters:
angle
- the angle in radians- Returns:
Rotation
this rotation
-
getCost
public double getCost()
Returns the value of cos(θ) for thisRotation
.- Returns:
- double
-
getSint
public double getSint()
Returns the value of sin(θ) for thisRotation
.- Returns:
- double
-
toRadians
public double toRadians()
Returns the angle in radians for thisRotation
.- Returns:
- double
-
toDegrees
public double toDegrees()
Returns the angle in degrees for thisRotation
.- Returns:
- double
-
toVector
public Vector2 toVector()
Returns thisRotation
as a unit length direction vector.- Returns:
Vector2
-
toVector
public Vector2 toVector(double magnitude)
Returns thisRotation
as a direction vector with the given magnitude.- Parameters:
magnitude
- the magnitude- Returns:
Vector2
-
rotate45
public Rotation rotate45()
Rotates this rotation 45 degrees and returns this rotation.- Returns:
Rotation
-
getRotated45
public Rotation getRotated45()
Rotates this rotation 45 degrees and returns a new rotation.- Returns:
Rotation
-
rotate90
public Rotation rotate90()
Rotates this rotation 90 degrees and returns this rotation.- Returns:
Rotation
-
getRotated90
public Rotation getRotated90()
Rotates this rotation 90 degrees and returns a new rotation.- Returns:
Rotation
-
rotate135
public Rotation rotate135()
Rotates this rotation 135 degrees and returns this rotation.- Returns:
Rotation
-
getRotated135
public Rotation getRotated135()
Rotates this rotation 135 degrees and returns a new rotation.- Returns:
Rotation
-
rotate180
public Rotation rotate180()
Rotates this rotation 180 degrees and returns this rotation.- Returns:
Rotation
-
getRotated180
public Rotation getRotated180()
Rotates this rotation 180 degrees and returns a new rotation.- Returns:
Rotation
-
rotate225
public Rotation rotate225()
Rotates this rotation 225 degrees and returns this rotation.- Returns:
Rotation
-
getRotated225
public Rotation getRotated225()
Rotates this rotation 225 degrees and returns a new rotation.- Returns:
Rotation
-
rotate270
public Rotation rotate270()
Rotates this rotation 270 degrees and returns this rotation.- Returns:
Rotation
-
getRotated270
public Rotation getRotated270()
Rotates this rotation 270 degrees and returns a new rotation.- Returns:
Rotation
-
rotate315
public Rotation rotate315()
Rotates this rotation 315 degrees and returns this rotation.- Returns:
Rotation
-
getRotated315
public Rotation getRotated315()
Rotates this rotation 315 degrees and returns a new rotation.- Returns:
Rotation
-
inverse
public Rotation inverse()
Negates this rotation and returns this rotation.Let θ be the rotation, then -θ is the inverse rotation.
- Returns:
Rotation
-
getInversed
public Rotation getInversed()
Negates this rotation and returns a new rotation.Let θ be the rotation, then -θ is the inverse rotation.
- Returns:
Rotation
-
getRotated
public Rotation getRotated(double c, double s)
Internal method that return a newRotation
representing thisRotation
after being rotated by an angle θ.- Parameters:
c
- cos(θ)s
- sin(θ)- Returns:
Rotation
-
rotate
public Rotation rotate(Rotation rotation)
Rotates this rotation by the given rotation and returns this rotation.
-
getRotated
public Rotation getRotated(Rotation rotation)
Rotates this rotation by the given rotation and returns a new rotation.
-
rotate
public Rotation rotate(double angle)
Rotates this rotation by the given angle and returns this rotation.- Parameters:
angle
- the rotation in radians- Returns:
Rotation
-
getRotated
public Rotation getRotated(double angle)
Rotates this rotation by the given angle and returns a new rotation.- Parameters:
angle
- the rotation in radians- Returns:
Rotation
-
isIdentity
public boolean isIdentity()
Returns true if this rotation is an identity rotation.- Returns:
- boolean
-
isIdentity
public boolean isIdentity(double error)
Returns true if this rotation is an identity rotation within the given error.- Parameters:
error
- the error- Returns:
- boolean
-
dot
public double dot(Rotation rotation)
Returns the dot product of the thisRotation
and the givenRotation
which is essentially the sine of the angle between those rotations.- Parameters:
rotation
- theRotation
- Returns:
- double
-
cross
public double cross(Rotation rotation)
Returns the cross product of the thisRotation
and the givenRotation
which is essentially the sine of the angle between those rotations.- Parameters:
rotation
- theRotation
- Returns:
- double
-
compare
public int compare(Rotation other)
Compares thisRotation
with another one, based on the angle between them (The one with -π ≤ θ ≤ π) Returns 1 if θ > 0, -1 if θ < 0 and 0 otherwise- Parameters:
other
- theRotation
to compare to- Returns:
- int
-
compare
public int compare(Vector2 other)
Compares thisRotation
with aVector2
, based on the angle between them (The one with -π ≤ θ ≤ π) Returns 1 if θ > 0, -1 if θ < 0 and 0 otherwise- Parameters:
other
- theVector2
to compare to- Returns:
- int
-
-