public enum Direction extends java.lang.Enum<Direction>
Enum Constant and Description |
---|
DOWN |
DOWN_LEFT |
DOWN_RIGHT |
LEFT |
NONE |
RIGHT |
UP |
UP_LEFT |
UP_RIGHT |
Modifier and Type | Field and Description |
---|---|
static Direction[] |
CARDINALS
An array which holds only the four cardinal directions.
|
static Direction[] |
CARDINALS_CLOCKWISE
An array which holds only the four cardinal directions in clockwise order.
|
static Direction[] |
CARDINALS_COUNTERCLOCKWISE
An array which holds only the four cardinal directions in counter-clockwise order.
|
static Direction[] |
CLOCKWISE
An array which holds all eight OUTWARDS directions in clockwise order.
|
static Direction[] |
COUNTERCLOCKWISE
An array which holds all eight OUTWARDS directions in counter-clockwise order.
|
int |
deltaX
The x coordinate difference for this direction.
|
int |
deltaY
The y coordinate difference for this direction.
|
static Direction[] |
DIAGONALS
An array which holds only the four diagonal directions.
|
static Direction[] |
OUTWARDS
An array which holds all eight OUTWARDS directions.
|
Modifier and Type | Method and Description |
---|---|
Direction |
clockwise()
Returns the Direction one step clockwise including diagonals.
|
Direction |
counterClockwise()
Returns the Direction one step counterclockwise including diagonals.
|
static Direction |
getCardinalDirection(int x,
int y)
Returns the direction that most closely matches the input.
|
static Direction |
getDirection(int x,
int y)
Returns the direction that most closely matches the input.
|
static Direction |
getRoughDirection(int x,
int y)
Gets an estimate at the correct direction that a position lies in given the distance towards it on the x and y
axes.
|
boolean |
hasDown() |
boolean |
hasLeft() |
boolean |
hasRight() |
boolean |
hasUp() |
boolean |
isCardinal() |
boolean |
isDiagonal() |
Direction |
opposite()
Returns the direction directly opposite of this one.
|
static Direction |
toGoTo(Coord from,
Coord to) |
static Direction |
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.
|
static Direction[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final Direction UP
public static final Direction DOWN
public static final Direction LEFT
public static final Direction RIGHT
public static final Direction UP_LEFT
public static final Direction UP_RIGHT
public static final Direction DOWN_LEFT
public static final Direction DOWN_RIGHT
public static final Direction NONE
public static final Direction[] CARDINALS
public static final Direction[] CARDINALS_CLOCKWISE
public static final Direction[] CARDINALS_COUNTERCLOCKWISE
public static final Direction[] DIAGONALS
public static final Direction[] OUTWARDS
public static final Direction[] CLOCKWISE
public static final Direction[] COUNTERCLOCKWISE
public final int deltaX
public final int deltaY
public static Direction[] values()
for (Direction c : Direction.values()) System.out.println(c);
public static Direction valueOf(java.lang.String name)
name
- the name of the enum constant to be returned.java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is nullpublic static Direction getDirection(int x, int y)
NONE
in the case that x and y are both 0.
x
- the x position relative to the origin (0,0)y
- the y position relative to the origin (0,0)NONE
public static Direction getRoughDirection(int x, int y)
getDirection(int, int)
by avoiding trigonometry or any other math on doubles. If at least one of x or y
is 0, then this will also be accurate and will produce either a cardinal direction or NONE if both are 0. If x
and y are both non-zero, this will always produce a diagonal, even if a cardinal direction should be more
accurate; this behavior may sometimes be desirable to detect when some position is even slightly off from a true
cardinal direction.x
- the x position relative to the origin (0,0)y
- the y position relative to the origin (0,0)public static Direction getCardinalDirection(int x, int y)
NONE
in the case that x and y are both 0.
UP
was returned when DOWN
should have
been, and vice versa; the workaround used in various places was to negate y.
Now you should no longer have to negate y or treat it differently than x, and
earlier code should account for the vertical axis being corrected.x
- the x position relative to the origin (0,0)y
- the y position relative to the origin (0,0)NONE
public static Direction toGoTo(Coord from, Coord to)
from
- The starting point.to
- The desired point to reach.from
to to
. It
can be cardinal or diagonal.public Direction clockwise()
public Direction counterClockwise()
public Direction opposite()
public boolean isDiagonal()
public boolean isCardinal()
public boolean hasUp()
true
if this
has an upward component.public boolean hasDown()
true
if this
has a downward component.public boolean hasLeft()
true
if this
has a left component.public boolean hasRight()
true
if this
has a right component.Copyright © Eben Howard 2012–2022. All rights reserved.