Package squidpony.squidgrid
Enum Direction
- All Implemented Interfaces:
Serializable
,Comparable<Direction>
,Constable
public enum Direction extends Enum<Direction>
Represents the eight grid directions and the deltaX, deltaY values associated
with those directions.
The grid referenced has x positive to the right and y positive downwards on
screen.
- Author:
- Eben Howard - http://squidpony.com - howard@squidpony.com
-
Nested Class Summary
-
Enum Constant Summary
-
Field Summary
Fields Modifier and Type Field 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. -
Method Summary
Modifier and Type Method 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(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.
-
Enum Constant Details
-
Field Details
-
CARDINALS
An array which holds only the four cardinal directions. -
CARDINALS_CLOCKWISE
An array which holds only the four cardinal directions in clockwise order. -
CARDINALS_COUNTERCLOCKWISE
An array which holds only the four cardinal directions in counter-clockwise order. -
DIAGONALS
An array which holds only the four diagonal directions. -
OUTWARDS
An array which holds all eight OUTWARDS directions. -
CLOCKWISE
An array which holds all eight OUTWARDS directions in clockwise order. -
COUNTERCLOCKWISE
An array which holds all eight OUTWARDS directions in counter-clockwise order. -
deltaX
The x coordinate difference for this direction. -
deltaY
The y coordinate difference for this direction.
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
getDirection
Returns the direction that most closely matches the input. This can be any direction, including cardinal and diagonal directions as well asNONE
in the case that x and y are both 0.
This can be used to get the primary intercardinal direction from an origin point to an event point, such as a mouse click on a grid. If the point given is exactly on a boundary between directions then the direction clockwise is returned.- Parameters:
x
- the x position relative to the origin (0,0)y
- the y position relative to the origin (0,0)- Returns:
- the closest matching Direction enum, which may be
NONE
-
getRoughDirection
Gets an estimate at the correct direction that a position lies in given the distance towards it on the x and y axes. If x and y are both between -1 and 1 inclusive, this will always be accurate, and should be faster thangetDirection(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.- Parameters:
x
- the x position relative to the origin (0,0)y
- the y position relative to the origin (0,0)- Returns:
- the Direction that x,y lies in, roughly; will always be accurate for arguments between -1 and 1 inclusive
-
getCardinalDirection
Returns the direction that most closely matches the input. This can be any of the four cardinal directions as well asNONE
in the case that x and y are both 0.
This can be used to get the primary cardinal direction from an origin point to an event point, such as a mouse click on a grid. If the point given is directly diagonal then the direction clockwise is returned.
This method returned an incorrect value for several years (the bug was found on April 28, 2020 but the method hadn't been modified for a long time), and other code in SquidLib had workarounds in use before the bug was fixed. The nature of the bug was simply thatUP
was returned whenDOWN
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.- Parameters:
x
- the x position relative to the origin (0,0)y
- the y position relative to the origin (0,0)- Returns:
- the closest matching cardinal Direction enum, which may also be
NONE
-
toGoTo
- Parameters:
from
- The starting point.to
- The desired point to reach.- Returns:
- The direction to follow to go from
from
toto
. It can be cardinal or diagonal.
-
clockwise
Returns the Direction one step clockwise including diagonals. If considering only Cardinal directions, calling this twice will get the next clockwise cardinal direction.- Returns:
-
counterClockwise
Returns the Direction one step counterclockwise including diagonals. If considering only Cardinal directions, calling this twice will get the next counterclockwise cardinal direction.- Returns:
-
opposite
Returns the direction directly opposite of this one.- Returns:
-
isDiagonal
- Returns:
- Whether this is a diagonal move.
-
isCardinal
- Returns:
- Whether this is a cardinal-direction move.
-
hasUp
- Returns:
true
ifthis
has an upward component.
-
hasDown
- Returns:
true
ifthis
has a downward component.
-
hasLeft
- Returns:
true
ifthis
has a left component.
-
hasRight
- Returns:
true
ifthis
has a right component.
-