Package org.bitbrawl.foodfight.util

## Class Vector

• ```@Immutable
public final class Vector
extends Object```
A two-dimensional vector in the horizontal plane of the field. This is often used to represent location, as in the `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)`.

Author:
Finn
• ### Nested Class Summary

Nested Classes
Modifier and Type Class Description
`static class ` `Vector.Deserializer`
A deserializer for vectors.
`static class ` `Vector.Serializer`
A serializer for vectors.
• ### Field Summary

Fields
Modifier and Type Field Description
`static Vector` `ZERO`
The zero vector, with both an x and y of zero, pointing east.
• ### Method Summary

All Methods
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​()`
• ### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Field Detail

• #### ZERO

`public static final Vector ZERO`
The zero vector, with both an x and y of zero, pointing east.
• ### Method Detail

• #### cartesian

```public static Vector cartesian​(double x,
double y)```
Constructs a vector with Cartesian coordinates. If this vector is used to represent location on the field, then (when viewing the field from above), x is the distance of the location from the left wall, and y is the distance of the location from the bottom wall.
Parameters:
`x` - the x-coordinate of the vector
`y` - the y-coordinate of the vector
Returns:
a vector with the desired parameters
Throws:
`IllegalArgumentException` - if x or y is not finite
• #### polar

```public static Vector polar​(double magnitude,
Direction direction)```
Constructs a vector with polar coordinates. If this vector is used to represent translation (for example, from one player to another), then the magnitude would represent the distance between the two players, and the direction would represent the direction from the first player to the second.
Parameters:
`magnitude` - the magnitude of the vector
`direction` - the direction of the vector
Returns:
a vector with the desired parameters
Throws:
`IllegalArgumentException` - if magnitude is not finite and non-negative
`NullPointerException` - if direction is null
• #### getX

`public double getX​()`
Returns the x-coordinate of this vector.
Returns:
this vector's x-coordinate
• #### getY

`public double getY​()`
Returns the y-coordinate of this vector.
Returns:
this vector's y-coordinate
• #### getMagnitude

`public double getMagnitude​()`
Returns the magnitude of this vector.
Returns:
this vector's magnitude.
• #### getDirection

`public Direction getDirection​()`
Returns the direction of this vector.
Returns:
this vector's direction
• #### getOpposite

`public Vector getOpposite​()`
Returns the vector with the same magnitude as this one, but in the opposite direction.
Returns:
this vector's opposite

`public Vector add​(Vector other)`
Adds the given vector to this one, returning the result. Both vector's x-coordinates are added to get the new x-coordinate, and both vector's y-coordinates are added to get the new y-coordinate.
Parameters:
`other` - the vector to add to this one
Returns:
the sum of the two vectors
Throws:
`NullPointerException` - if other is null
• #### subtract

`public Vector subtract​(Vector other)`
Subtracts the given vector from this one. This is equivalent to calling the `add(Vector)` method on other's opposite.
Parameters:
`other` - the vector to subtract from this one
Returns:
the difference of the two vectors
Throws:
`NullPointerException` - if other is null
• #### multiply

`public Vector multiply​(double scalar)`
Multiplies the vector by given scalar. The x and y coordinates are multiplied by the given scalar. The direction will be unchanged, unless the scalar is negative, in which case the new direction will be the opposite of the current one.
Parameters:
`scalar` - the amount by which to multiply the vector
Returns:
the result of the multiplication
Throws:
`IllegalArgumentException` - if scalar is not finite
• #### divide

`public Vector divide​(double scalar)`
Divides the vector by the given scalar. This is equivalent to multiplying by scalar's reciprocal.
Parameters:
`scalar` - the amount by which to divide the scalar
Returns:
the result of the division
Throws:
`IllegalArgumentException` - if scalar is not finite or is zero
• #### dot

`public double dot​(Vector other)`
Performs the scalar product of this vector with the given one.
Parameters:
`other` - the vector with which to multiply this one
Returns:
the dot product of the two vectors
Throws:
`NullPointerException` - if other is null
• #### componentAlong

`public double componentAlong​(Vector other)`
Returns the scalar projection of this vector onto other. This is also the magnitude of the vector projection of this vector onto other, or the component of this vector along other.
Parameters:
`other` - the vector to project this vector onto
Returns:
the component of this vector along other
Throws:
`NullPointerException` - if other is null
• #### average

```public static Vector average​(Vector v1,
Vector v2)```
Computes the average of the two vectors. If the two vectors represent locations, this is equivalent to the midpoint of the two locations.
Parameters:
`v1` - the first vector to average
`v2` - the second vector to average
Returns:
the average of the two vectors
Throws:
`NullPointerException` - if either vector is null
• #### toString

`public String toString​()`
Overrides:
`toString` in class `Object`
• #### equals

`public boolean equals​(Object other)`
Overrides:
`equals` in class `Object`
• #### hashCode

`public int hashCode​()`
Overrides:
`hashCode` in class `Object`