Enum Direction

java.lang.Object
java.lang.Enum<Direction>
squidpony.squidgrid.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
  • Enum Constant Details

  • Field Details

  • Method Details

    • values

      public static Direction[] 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

      public static Direction valueOf​(String name)
      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 name
      NullPointerException - if the argument is null
    • getDirection

      public static Direction getDirection​(int x, int y)
      Returns the direction that most closely matches the input. This can be any direction, including cardinal and diagonal directions as well as NONE 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

      public 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. If x and y are both between -1 and 1 inclusive, this will always be accurate, and should be faster than 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.
      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

      public static Direction getCardinalDirection​(int x, int y)
      Returns the direction that most closely matches the input. This can be any of the four cardinal directions as well as NONE 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 that 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.
      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

      public static Direction toGoTo​(Coord from, Coord to)
      Parameters:
      from - The starting point.
      to - The desired point to reach.
      Returns:
      The direction to follow to go from from to to. It can be cardinal or diagonal.
    • clockwise

      public Direction 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

      public Direction opposite()
      Returns the direction directly opposite of this one.
      Returns:
    • isDiagonal

      public boolean isDiagonal()
      Returns:
      Whether this is a diagonal move.
    • isCardinal

      public boolean isCardinal()
      Returns:
      Whether this is a cardinal-direction move.
    • hasUp

      public boolean hasUp()
      Returns:
      true if this has an upward component.
    • hasDown

      public boolean hasDown()
      Returns:
      true if this has a downward component.
    • hasLeft

      public boolean hasLeft()
      Returns:
      true if this has a left component.
    • hasRight

      public boolean hasRight()
      Returns:
      true if this has a right component.