- Object
-
- Vector2
-
public class Vector2 extends Object implements Copyable<Vector2>
This class represents a vector or point in 2D space.The operations
setMagnitude(double)
,getNormalized()
,project(Vector2)
, andnormalize()
require theVector2
to be non-zero in length.Some methods also return the vector to facilitate chaining. For example:
Vector a = new Vector(); a.zero().add(1, 2).multiply(2);
- Since:
- 1.0.0
- Version:
- 4.0.0
- Author:
- William Bittle
-
-
Constructor Summary
Constructors Constructor Description Vector2()
Default constructor.Vector2(double direction)
Creates a unit length vector in the given direction.Vector2(double x, double y)
Optional constructor.Vector2(double x1, double y1, double x2, double y2)
Creates aVector2
from the first point to the second point.Vector2(Vector2 vector)
Copy constructor.Vector2(Vector2 p1, Vector2 p2)
Creates aVector2
from the first point to the second point.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Vector2
add(double x, double y)
Vector2
add(Vector2 vector)
Vector2
copy()
Returns a deep copy of this object.static Vector2
create(double magnitude, double direction)
Returns a newVector2
given the magnitude and direction.Vector2
cross(double z)
double
cross(double x, double y)
double
cross(Vector2 vector)
Vector2
difference(double x, double y)
Vector2
difference(Vector2 vector)
double
distance(double x, double y)
Returns the distance from this point to the given point.double
distance(Vector2 point)
Returns the distance from this point to the given point.double
distanceSquared(double x, double y)
Returns the distance from this point to the given point squared.double
distanceSquared(Vector2 point)
Returns the distance from this point to the given point squared.Vector2
divide(double scalar)
Divides thisVector2
by the given scalar.double
dot(double x, double y)
double
dot(Vector2 vector)
boolean
equals(double x, double y)
Returns true if the x and y components of thisVector2
are the same as the given x and y components.boolean
equals(Object obj)
boolean
equals(Vector2 vector)
double
getAngleBetween(double otherAngle)
Returns the smallest angle between the givenVector2
and the given angle.double
getAngleBetween(Vector2 vector)
Returns the smallest angle between the givenVector2
s.double
getDirection()
Returns the direction of thisVector2
as an angle in radians.Vector2
getLeftHandOrthogonalVector()
Returns the left-handed normal of this vector.double
getMagnitude()
Returns the magnitude of thisVector2
.double
getMagnitudeSquared()
Returns the magnitude of thisVector2
squared.Vector2
getNegative()
Vector2
getNormalized()
Vector2
getRightHandOrthogonalVector()
Returns the right-handed normal of this vector.Vector2
getXComponent()
Returns the x component of thisVector2
.Vector2
getYComponent()
Returns the y component of thisVector2
.int
hashCode()
Vector2
inverseRotate(double theta)
Rotates about the origin by the inverse angle -ϑ.Vector2
inverseRotate(double theta, double x, double y)
Rotates about the given coordinates by the inverse angle -ϑ.Vector2
inverseRotate(double theta, Vector2 point)
Rotates theVector2
about the given point by the inverse angle -ϑ.Vector2
inverseRotate(Rotation rotation)
Rotates about the origin by the inverse angle -ϑ.Vector2
inverseRotate(Rotation rotation, double x, double y)
Rotates about the given coordinates by the inverse angle -ϑ.Vector2
inverseRotate(Rotation rotation, Vector2 point)
Rotates theVector2
about the given point by the inverse angle -ϑ.boolean
isOrthogonal(double x, double y)
boolean
isOrthogonal(Vector2 vector)
boolean
isZero()
Vector2
left()
Sets this vector to the left-handed normal of this vector.Vector2
multiply(double scalar)
Multiplies thisVector2
by the given scalar.Vector2
negate()
Negates thisVector2
.double
normalize()
Vector2
product(double scalar)
Vector2
project(Vector2 vector)
Vector2
quotient(double scalar)
Vector2
right()
Sets this vector to the right-handed normal of this vector.Vector2
rotate(double theta)
Rotates about the origin.Vector2
rotate(double theta, double x, double y)
Rotates theVector2
about the given coordinates.Vector2
rotate(double theta, Vector2 point)
Rotates theVector2
about the given point.Vector2
rotate(Rotation rotation)
Rotates about the origin.Vector2
rotate(Rotation rotation, double x, double y)
Rotates theVector2
about the given coordinates.Vector2
rotate(Rotation rotation, Vector2 point)
Rotates theVector2
about the given point.Vector2
set(double x, double y)
Vector2
set(Vector2 vector)
Vector2
setDirection(double angle)
Sets the direction of thisVector2
.Vector2
setMagnitude(double magnitude)
Sets the magnitude of theVector2
.Vector2
subtract(double x, double y)
Vector2
subtract(Vector2 vector)
Vector2
sum(double x, double y)
Vector2
sum(Vector2 vector)
Vector2
to(double x, double y)
Vector2
to(Vector2 vector)
String
toString()
static Vector2
tripleProduct(Vector2 a, Vector2 b, Vector2 c)
The triple product ofVector2
s is defined as:Vector2
zero()
-
-
-
Constructor Detail
-
Vector2
public Vector2()
Default constructor.
-
Vector2
public Vector2(Vector2 vector)
Copy constructor.- Parameters:
vector
- theVector2
to copy from
-
Vector2
public Vector2(double x, double y)
Optional constructor.- Parameters:
x
- the x componenty
- the y component
-
Vector2
public Vector2(double x1, double y1, double x2, double y2)
Creates aVector2
from the first point to the second point.- Parameters:
x1
- the x coordinate of the first pointy1
- the y coordinate of the first pointx2
- the x coordinate of the second pointy2
- the y coordinate of the second point
-
Vector2
public Vector2(Vector2 p1, Vector2 p2)
Creates aVector2
from the first point to the second point.- Parameters:
p1
- the first pointp2
- the second point
-
Vector2
public Vector2(double direction)
Creates a unit length vector in the given direction.- Parameters:
direction
- the direction in radians- Since:
- 3.0.1
-
-
Method Detail
-
create
public static Vector2 create(double magnitude, double direction)
Returns a newVector2
given the magnitude and direction.
-
copy
public Vector2 copy()
Description copied from interface:Copyable
Returns a deep copy of this object.
-
distance
public double distance(double x, double y)
Returns the distance from this point to the given point.- Parameters:
x
- the x coordinate of the pointy
- the y coordinate of the point- Returns:
- double
-
distance
public double distance(Vector2 point)
Returns the distance from this point to the given point.- Parameters:
point
- the point- Returns:
- double
-
distanceSquared
public double distanceSquared(double x, double y)
Returns the distance from this point to the given point squared.- Parameters:
x
- the x coordinate of the pointy
- the y coordinate of the point- Returns:
- double
-
distanceSquared
public double distanceSquared(Vector2 point)
Returns the distance from this point to the given point squared.- Parameters:
point
- the point- Returns:
- double
-
tripleProduct
public static Vector2 tripleProduct(Vector2 a, Vector2 b, Vector2 c)
The triple product ofVector2
s is defined as:a x (b x c)
However, this method performs the following triple product:(a x b) x c
this can be simplified to:-a * (b · c) + b * (a · c)
or:b * (a · c) - a * (b · c)
-
equals
public boolean equals(Vector2 vector)
- Parameters:
vector
- theVector2
to compare to- Returns:
- boolean
-
equals
public boolean equals(double x, double y)
Returns true if the x and y components of thisVector2
are the same as the given x and y components.
-
set
public Vector2 set(double x, double y)
-
getXComponent
public Vector2 getXComponent()
Returns the x component of thisVector2
.- Returns:
Vector2
-
getYComponent
public Vector2 getYComponent()
Returns the y component of thisVector2
.- Returns:
Vector2
-
getMagnitude
public double getMagnitude()
Returns the magnitude of thisVector2
.- Returns:
- double
-
getMagnitudeSquared
public double getMagnitudeSquared()
Returns the magnitude of thisVector2
squared.- Returns:
- double
-
setMagnitude
public Vector2 setMagnitude(double magnitude)
Sets the magnitude of theVector2
.- Parameters:
magnitude
- the magnitude- Returns:
Vector2
this vector
-
getDirection
public double getDirection()
Returns the direction of thisVector2
as an angle in radians.- Returns:
- double angle in radians [-π, π]
-
setDirection
public Vector2 setDirection(double angle)
Sets the direction of thisVector2
.- Parameters:
angle
- angle in radians- Returns:
Vector2
this vector
-
add
public Vector2 add(double x, double y)
-
sum
public Vector2 sum(double x, double y)
-
subtract
public Vector2 subtract(double x, double y)
-
difference
public Vector2 difference(double x, double y)
-
to
public Vector2 to(double x, double y)
-
multiply
public Vector2 multiply(double scalar)
Multiplies thisVector2
by the given scalar.- Parameters:
scalar
- the scalar- Returns:
Vector2
this vector
-
divide
public Vector2 divide(double scalar)
Divides thisVector2
by the given scalar.- Parameters:
scalar
- the scalar- Returns:
Vector2
this vector- Since:
- 3.4.0
-
quotient
public Vector2 quotient(double scalar)
- Parameters:
scalar
- the scalar- Returns:
Vector2
- Since:
- 3.4.0
-
dot
public double dot(double x, double y)
-
cross
public double cross(double x, double y)
-
cross
public Vector2 cross(double z)
-
isOrthogonal
public boolean isOrthogonal(Vector2 vector)
Returns true if the givenVector2
is orthogonal (perpendicular) to thisVector2
.If the dot product of this vector and the given vector is zero then we know that they are perpendicular
- Parameters:
vector
- theVector2
- Returns:
- boolean
-
isOrthogonal
public boolean isOrthogonal(double x, double y)
-
isZero
public boolean isZero()
- Returns:
- boolean
-
rotate
public Vector2 rotate(double theta)
Rotates about the origin.- Parameters:
theta
- the rotation angle in radians- Returns:
Vector2
this vector
-
inverseRotate
public Vector2 inverseRotate(double theta)
Rotates about the origin by the inverse angle -ϑ.- Parameters:
theta
- the rotation angle in radians- Returns:
Vector2
this vector- Since:
- 3.4.0
-
inverseRotate
public Vector2 inverseRotate(Rotation rotation)
Rotates about the origin by the inverse angle -ϑ.
-
rotate
public Vector2 rotate(double theta, double x, double y)
Rotates theVector2
about the given coordinates.- Parameters:
theta
- the rotation angle in radiansx
- the x coordinate to rotate abouty
- the y coordinate to rotate about- Returns:
Vector2
this vector
-
rotate
public Vector2 rotate(Rotation rotation, double x, double y)
Rotates theVector2
about the given coordinates.
-
inverseRotate
public Vector2 inverseRotate(double theta, double x, double y)
Rotates about the given coordinates by the inverse angle -ϑ.- Parameters:
theta
- the rotation angle in radiansx
- the x coordinate to rotate abouty
- the y coordinate to rotate about- Returns:
Vector2
this vector- Since:
- 3.4.0
-
inverseRotate
public Vector2 inverseRotate(Rotation rotation, double x, double y)
Rotates about the given coordinates by the inverse angle -ϑ.
-
rotate
public Vector2 rotate(double theta, Vector2 point)
Rotates theVector2
about the given point.- Parameters:
theta
- the rotation angle in radianspoint
- the point to rotate about- Returns:
Vector2
this vector
-
rotate
public Vector2 rotate(Rotation rotation, Vector2 point)
Rotates theVector2
about the given point.
-
inverseRotate
public Vector2 inverseRotate(double theta, Vector2 point)
Rotates theVector2
about the given point by the inverse angle -ϑ.- Parameters:
theta
- the rotation angle in radianspoint
- the point to rotate about- Returns:
Vector2
this vector- Since:
- 3.4.0
-
inverseRotate
public Vector2 inverseRotate(Rotation rotation, Vector2 point)
Rotates theVector2
about the given point by the inverse angle -ϑ.
-
getRightHandOrthogonalVector
public Vector2 getRightHandOrthogonalVector()
Returns the right-handed normal of this vector.
-
right
public Vector2 right()
Sets this vector to the right-handed normal of this vector.- Returns:
Vector2
this vector- See Also:
getRightHandOrthogonalVector()
-
getLeftHandOrthogonalVector
public Vector2 getLeftHandOrthogonalVector()
Returns the left-handed normal of this vector.
-
left
public Vector2 left()
Sets this vector to the left-handed normal of this vector.- Returns:
Vector2
this vector- See Also:
getLeftHandOrthogonalVector()
-
getNormalized
public Vector2 getNormalized()
Returns a unitVector2
of thisVector2
.This method requires the length of this
Vector2
is not zero.- Returns:
Vector2
-
normalize
public double normalize()
Converts thisVector2
into a unitVector2
and returns the magnitude before normalization.This method requires the length of this
Vector2
is not zero.- Returns:
- double
-
getAngleBetween
public double getAngleBetween(Vector2 vector)
Returns the smallest angle between the givenVector2
s.Returns the angle in radians in the range -π to π.
- Parameters:
vector
- theVector2
- Returns:
- angle in radians [-π, π]
-
getAngleBetween
public double getAngleBetween(double otherAngle)
Returns the smallest angle between the givenVector2
and the given angle.Returns the angle in radians in the range -π to π.
- Parameters:
otherAngle
- the angle. Must be in the range -π to π- Returns:
- angle in radians [-π, π]
- Since:
- 3.4.0
-
-