pub enum Direction {
    Up,
    Right,
    Down,
    Left,
}
Expand description

The four cardinal directions: Up, Down, Left, and Right. Direction implements a number of simple helper methods. It also implements VectorLike, which allows it to be used in contexts where a Vector can be used as a unit vector in the given direction (for example, with Vector arithmetic).

Variants§

§

Up

The negative row direction

§

Right

The positive column direction

§

Down

The positive row direction

§

Left

The negative column direction

Implementations§

source§

impl Direction

source

pub fn from_name(name: &str) -> Option<Self>

Parse a direction name into a direction. Currently supported names are (case insensitive):

  • Up: Up, North, U, N
  • Down: Down, South, D, S
  • Left: Left, West, L, W
  • Right: Right, East, R, E
Example
use gridly::prelude::*;

assert_eq!(Direction::from_name("up"), Some(Up));
assert_eq!(Direction::from_name("West"), Some(Left));
assert_eq!(Direction::from_name("Foo"), None);
source

pub fn sized_vec(self, length: isize) -> Vector

Return a vector with the given length in this direction

Example:
use gridly::prelude::*;

assert_eq!(Up.sized_vec(2), Vector::new(-2, 0));
assert_eq!(Down.sized_vec(3), Vector::new(3, 0));
assert_eq!(Left.sized_vec(1), Vector::new(0, -1));
assert_eq!(Right.sized_vec(5), Vector::new(0, 5));
source

pub fn unit_vec(self) -> Vector

Return the unit vector in the given direction.

Example:
use gridly::prelude::*;

assert_eq!(Up.unit_vec(), Vector::new(-1, 0));
assert_eq!(Down.unit_vec(), Vector::new(1, 0));
assert_eq!(Left.unit_vec(), Vector::new(0, -1));
assert_eq!(Right.unit_vec(), Vector::new(0, 1));
source

pub fn is_vertical(self) -> bool

True if this is Up or Down

Example:
use gridly::direction::*;

assert!(Up.is_vertical());
assert!(Down.is_vertical());
assert!(!Left.is_vertical());
assert!(!Right.is_vertical());
source

pub fn is_horizontal(self) -> bool

True if this is Left or Right

Example:
use gridly::direction::*;

assert!(!Up.is_horizontal());
assert!(!Down.is_horizontal());
assert!(Left.is_horizontal());
assert!(Right.is_horizontal());
source

pub fn reverse(self) -> Direction

Reverse this direction (UpDown, etc)

use gridly::direction::*;

assert_eq!(Up.reverse(), Down);
assert_eq!(Down.reverse(), Up);
assert_eq!(Left.reverse(), Right);
assert_eq!(Right.reverse(), Left);
source

pub fn clockwise(self) -> Direction

Rotate this direction clockwise

Example:
use gridly::direction::*;

assert_eq!(Up.clockwise(), Right);
assert_eq!(Down.clockwise(), Left);
assert_eq!(Left.clockwise(), Up);
assert_eq!(Right.clockwise(), Down);
source

pub fn anticlockwise(self) -> Direction

Rotate this direction counterclockwise

Example:
use gridly::direction::*;

assert_eq!(Up.anticlockwise(), Left);
assert_eq!(Down.anticlockwise(), Right);
assert_eq!(Left.anticlockwise(), Down);
assert_eq!(Right.anticlockwise(), Up);
source

pub fn rotate(self, rotation: Rotation) -> Direction

Rotate this direction by the given rotation.

Example
use gridly::rotation::*;
use gridly::direction::*;

assert_eq!(Up.rotate(Clockwise), Right);
assert_eq!(Down.rotate(Rotation::None), Down);
assert_eq!(Left.rotate(Anticlockwise), Down);
assert_eq!(Right.rotate(Rotation::Flip), Left);
source

pub fn rotation_to(self, target: Direction) -> Rotation

Given a target direction, get the rotation that rotates this direction to that one.

Example
use gridly::direction::*;
use gridly::rotation::*;

assert_eq!(Up.rotation_to(Right), Clockwise);
assert_eq!(Down.rotation_to(Up), Rotation::Flip);
assert_eq!(Left.rotation_to(Down), Anticlockwise);
assert_eq!(Up.rotation_to(Up), Rotation::None);

Trait Implementations§

source§

impl<T: VectorLike> Add<T> for Direction

Adding a Vector to a Direction is equivelent to adding it to a unit vector in the given direction. Note that, because Direction itself implements VectorLike, this means you can add together a sequence of directions to get a Vector.

Example:

use gridly::vector::Vector;
use gridly::direction::*;

let base = Vector::new(3, 4);

assert_eq!(Up + base, Vector::new(2, 4));
assert_eq!(Down + base, Vector::new(4, 4));
assert_eq!(Right + base, Vector::new(3, 5));
assert_eq!(Left + base, Vector::new(3, 3));

assert_eq!(Up + Right + Up + Up, Vector::new(-3, 1));
§

type Output = Vector

The resulting type after applying the + operator.
source§

fn add(self, rhs: T) -> Vector

Performs the + operation. Read more
source§

impl Clone for Direction

source§

fn clone(&self) -> Direction

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Direction

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Direction

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Mul<isize> for Direction

Multiplying a Direction by an isize produces a Vector of the given length in the given direction

Example:

use gridly::direction::*;
use gridly::vector::Vector;

assert_eq!(Up * 5, Vector::new(-5, 0));
assert_eq!(Down * 3, Vector::new(3, 0));
assert_eq!(Left * 2, Vector::new(0, -2));
assert_eq!(Right * 4, Vector::new(0, 4));
§

type Output = Vector

The resulting type after applying the * operator.
source§

fn mul(self, amount: isize) -> Vector

Performs the * operation. Read more
source§

impl Neg for Direction

Negating a Direction reverses it

Example:

use gridly::direction::*;

assert_eq!(-Up, Down);
assert_eq!(-Down, Up);
assert_eq!(-Left, Right);
assert_eq!(-Right, Left);
§

type Output = Direction

The resulting type after applying the - operator.
source§

fn neg(self) -> Direction

Performs the unary - operation. Read more
source§

impl PartialEq for Direction

source§

fn eq(&self, other: &Direction) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: VectorLike> Sub<T> for Direction

Subtracting a Vector from a Direction is equivelent to subtracing it from a unit vector in the given direction

Example:

use gridly::vector::Vector;
use gridly::direction::*;

let base = Vector::new(3, 3);

assert_eq!(Up - base, Vector::new(-4, -3));
assert_eq!(Down - base, Vector::new(-2, -3));
assert_eq!(Right - base, Vector::new(-3, -2));
assert_eq!(Left - base, Vector::new(-3, -4));
§

type Output = Vector

The resulting type after applying the - operator.
source§

fn sub(self, rhs: T) -> Vector

Performs the - operation. Read more
source§

impl VectorLike for Direction

A Direction acts like a unit vector in the given direction. This allows it to be used in things like Vector arithmetic.

Example:

use gridly::prelude::*;

assert_eq!(Vector::new(1, 1) + Up, Vector::new(0, 1));
assert_eq!(Location::new(3, 4) - Left, Location::new(3, 5));
source§

fn rows(&self) -> Rows

source§

fn columns(&self) -> Columns

source§

fn as_vector(&self) -> Vector

source§

fn manhattan_length(&self) -> isize

Return the manhattan length of the vector. The manhattan length of a vector is the sum of the absolute values of its components. Read more
source§

fn checked_manhattan_length(&self) -> Option<isize>

Return the manhattan length of the vector, or None if there are any overflows. Read more
source§

fn clockwise(&self) -> Vector

Return a new vector, rotated 90 degrees clockwise. Read more
source§

fn anticlockwise(&self) -> Vector

Return a new vector, rotated 90 degrees counterclockwise. Read more
source§

fn reverse(&self) -> Vector

Return a new vector, facing the opposite direction of this one Read more
source§

fn transpose(&self) -> Vector

Swap the rows and columns of this Vector Read more
source§

fn direction(&self) -> Option<Direction>

If the vector is pointing in an orthogonal direction, return that direction Read more
source§

fn rotate(&self, rotation: Rotation) -> Vector

Return a new vector, rotated by a given rotation Read more
source§

fn get_component<T: Component>(&self) -> T

Generically get either the Rows or Columns of a vector Read more
source§

impl Copy for Direction

source§

impl Eq for Direction

source§

impl StructuralEq for Direction

source§

impl StructuralPartialEq for Direction

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.