@Immutable public final class Vector extends Object
Locatable
interface, but it can
also be used to represent other concepts that can be represented by vectors,
such as velocity or translation.
Note: this class has no public constructors. To construct a vector, use one
of the two factory methods, cartesian(double, double)
and
polar(double, Direction)
.
Modifier and Type | Class | Description |
---|---|---|
static class |
Vector.Deserializer |
A deserializer for vectors.
|
static class |
Vector.Serializer |
A serializer for vectors.
|
Modifier and Type | Field | Description |
---|---|---|
static Vector |
ZERO |
The zero vector, with both an x and y of zero, pointing east.
|
Modifier and Type | Method | Description |
---|---|---|
Vector |
add(Vector other) |
Adds the given vector to this one, returning the result.
|
static Vector |
average(Vector v1,
Vector v2) |
Computes the average of the two vectors.
|
static Vector |
cartesian(double x,
double y) |
Constructs a vector with Cartesian coordinates.
|
double |
componentAlong(Vector other) |
Returns the scalar projection of this vector onto other.
|
Vector |
divide(double scalar) |
Divides the vector by the given scalar.
|
double |
dot(Vector other) |
Performs the scalar product of this vector with the given one.
|
boolean |
equals(Object other) |
|
Direction |
getDirection() |
Returns the direction of this vector.
|
double |
getMagnitude() |
Returns the magnitude of this vector.
|
Vector |
getOpposite() |
Returns the vector with the same magnitude as this one, but in the
opposite direction.
|
double |
getX() |
Returns the x-coordinate of this vector.
|
double |
getY() |
Returns the y-coordinate of this vector.
|
int |
hashCode() |
|
Vector |
multiply(double scalar) |
Multiplies the vector by given scalar.
|
static Vector |
polar(double magnitude,
Direction direction) |
Constructs a vector with polar coordinates.
|
Vector |
subtract(Vector other) |
Subtracts the given vector from this one.
|
String |
toString() |
public static final Vector ZERO
public static Vector cartesian(double x, double y)
x
- the x-coordinate of the vectory
- the y-coordinate of the vectorIllegalArgumentException
- if x or y is not finitepublic static Vector polar(double magnitude, Direction direction)
magnitude
- the magnitude of the vectordirection
- the direction of the vectorIllegalArgumentException
- if magnitude is not finite and non-negativeNullPointerException
- if direction is nullpublic double getX()
public double getY()
public double getMagnitude()
public Direction getDirection()
public Vector getOpposite()
public Vector add(Vector other)
other
- the vector to add to this oneNullPointerException
- if other is nullpublic Vector subtract(Vector other)
add(Vector)
method on other's opposite.other
- the vector to subtract from this oneNullPointerException
- if other is nullpublic Vector multiply(double scalar)
scalar
- the amount by which to multiply the vectorIllegalArgumentException
- if scalar is not finitepublic Vector divide(double scalar)
scalar
- the amount by which to divide the scalarIllegalArgumentException
- if scalar is not finite or is zeropublic double dot(Vector other)
other
- the vector with which to multiply this oneNullPointerException
- if other is nullpublic double componentAlong(Vector other)
other
- the vector to project this vector ontoNullPointerException
- if other is nullpublic static Vector average(Vector v1, Vector v2)
v1
- the first vector to averagev2
- the second vector to averageNullPointerException
- if either vector is null