Package org.bitbrawl.foodfight.util

## Class Direction

• ```@Immutable
public final class Direction
extends Object```
A direction on 2-dimensional plane of the field. A direction is essentially an angle measurement, in radians. The directions used are the ones commonly used in math, where `EAST` is a direction of 0 and `NORTH` is a direction of π2. The angle measurement of a direction will always be in the range [0, 2π).
Author:
Finn
• ### Nested Class Summary

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

Fields
Modifier and Type Field Description
`static Direction` `EAST`
The direction with measurement 0.
`static Direction` `NORTH`
The direction with measurement π2.
`static Direction` `SOUTH`
The direction with measurement 2.
`static Direction` `WEST`
The direction with measurement π.
• ### Constructor Summary

Constructors
Constructor Description
`Direction​(double direction)`
Constructs a direction object with the given direction measurement.
• ### Method Summary

All Methods
Modifier and Type Method Description
`Direction` `add​(double angle)`
Adds the given amount to this direction, returning the result of the addition.
`static double` ```difference​(Direction alpha, Direction beta)```
Returns the signed difference between two angles.
`boolean` `equals​(Object other)`
`double` `get​()`
Returns the measurement of this direction object, in radians, where `EAST` is 0.0 and measurements increase as they rotate counter-clockwise.
`Direction` `getOpposite​()`
Returns the direction that is the opposite of this one.
`int` `hashCode​()`
`static Direction` `random​()`
Returns a randomly-generated direction.
`Direction` `reflectAcrossX​()`
Reflects this direction across the x-axis, returning the result.
`Direction` `reflectAcrossY​()`
Reflects this direction across the y-axis, returning the result.
`String` `toString​()`
• ### Methods inherited from class java.lang.Object

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

• #### EAST

`public static final Direction EAST`
The direction with measurement 0.
• #### NORTH

`public static final Direction NORTH`
The direction with measurement π2.
• #### WEST

`public static final Direction WEST`
The direction with measurement π.
• #### SOUTH

`public static final Direction SOUTH`
The direction with measurement 2.
• ### Constructor Detail

• #### Direction

`public Direction​(double direction)`
Constructs a direction object with the given direction measurement. Note that calling `get()` on the returned object may return something different than the passed-in direction measurement if the given measurement is outside the range [0, 2π). For example, 4 would be converted to π4.
Parameters:
`direction` - the angle measurement of the desired direction
Throws:
`IllegalArgumentException` - if direction is not finite
• ### Method Detail

• #### random

`public static Direction random​()`
Returns a randomly-generated direction. The returned directions are uniformly distributed on the range [0, 2π).
Returns:
a random direction
• #### get

`public double get​()`
Returns the measurement of this direction object, in radians, where `EAST` is 0.0 and measurements increase as they rotate counter-clockwise. Direction objects are immutable, so this will always return the same value.
Returns:
the angle measurement of this direction

`public Direction add​(double angle)`
Adds the given amount to this direction, returning the result of the addition. Note that this direction is not modified by this method (because direction objects are immutable), but a new direction is created. Note that a negative number can be used to subtract from this direction, but if the result is outside the range [0, 2π), it will "wrap around."
Parameters:
`angle` - the angle to add to this direction's angle measurement
Returns:
the result of adding the angle to this direction
Throws:
`IllegalArgumentException` - if angle is not finite
• #### getOpposite

`public Direction getOpposite​()`
Returns the direction that is the opposite of this one. The returned angle will differ from this one by π. As an example, the opposite of a player's direction is the direction that they would move if they were to move backward.
Returns:
the opposite of this direction
• #### reflectAcrossY

`public Direction reflectAcrossY​()`
Reflects this direction across the y-axis, returning the result. This method does not modify the object it is called on. If the object that this method is called on has measurement θ, the resulting angle would have measurement π - θ, but in the range [0, 2π).
Returns:
the resulting direction after the reflection.
• #### reflectAcrossX

`public Direction reflectAcrossX​()`
Reflects this direction across the x-axis, returning the result. This method does not modify the object it is called on. If the object that this method is called on has measurement θ, the resulting angle would have measurement -θ, but in the range [0, 2π).
Returns:
the resulting direction after the reflection.
• #### difference

```public static double difference​(Direction alpha,
Direction beta)```
Returns the signed difference between two angles. If called on two directions with measurements α and β, the result would be β - α, but in the range [-π, π). If the `add(double)` method is used to add the result of this method to alpha, the resulting angle will be very close to beta, and will differ only due to issues with floating-point arithmetic.
Parameters:
`alpha` - the first angle
`beta` - the second angle
Returns:
the angular displacement of beta relative to alpha
• #### equals

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

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

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