pub enum Direction4 {
North,
East,
South,
West,
}
Expand description
Four-way directions.
These start at North and increment counter-clockwise,
so you can convert them to integers with as
and use them
in rotational calculations if you need.
Variants§
Implementations§
Source§impl Direction4
impl Direction4
Sourcepub const DIRECTIONS: [Direction4; 4]
pub const DIRECTIONS: [Direction4; 4]
All the directions in order. This is used internally for rotations and flips. I made it public just in case it’s helpful for you the programmer.
Sourcepub fn rotate(self, rot: Rotation) -> Self
pub fn rotate(self, rot: Rotation) -> Self
Rotate this by the given amount.
use Direction4::*;
use Rotation::*;
assert_eq!(North.rotate(Clockwise), East);
assert_eq!(North.rotate(CounterClockwise), West);
Sourcepub fn rotate_by(self, steps_clockwise: isize) -> Self
pub fn rotate_by(self, steps_clockwise: isize) -> Self
Get this direction, rotated by this many steps clockwise. Negative numbers go counter-clockwise.
use Direction4::*;
assert_eq!(North.rotate_by(1), East);
assert_eq!(North.rotate_by(2), South);
assert_eq!(North.rotate_by(-1), West);
assert_eq!(North.rotate_by(5).rotate_by(-11), South);
Sourcepub fn flip(self) -> Self
pub fn flip(self) -> Self
Flip this direction.
use Direction4::*;
assert_eq!(North.flip(), South);
assert_eq!(West.flip(), East);
assert_eq!(East.flip().flip(), East);
Sourcepub fn radians(self) -> f32
pub fn radians(self) -> f32
Get this direction in radians.
This uses trigonometric + graphical standard, where:
- 0 radians is to the right
- Positive radians increment clockwise. NOTE: this is opposite from normal trig, but makes sense in computer graphics where +Y is downwards.
If you need it in degrees just call .to_degrees
on the result.
use Direction4::*;
use std::f32::consts::TAU;
let north_radians = North.radians();
assert!((north_radians - (TAU / 4.0 * 3.0)).abs() < 1e-10);
let west_radians = West.radians();
assert!((west_radians - (TAU / 4.0 * 2.0)).abs() < 1e-10);
Sourcepub fn deltas(self) -> ICoord
pub fn deltas(self) -> ICoord
Get the deltas a step in this direction would result in, as a ICoord.
use Direction4::*;
assert_eq!(North.deltas(), ICoord {x: 0, y: -1});
assert_eq!(West.deltas(), ICoord {x: -1, y: 0});
Sourcepub fn is_horizontal(self) -> bool
pub fn is_horizontal(self) -> bool
See if this direction points horizontally (ie, is East
or West
).
use Direction4::*;
assert!(East.is_horizontal());
assert!(!South.is_horizontal());
Sourcepub fn is_vertical(self) -> bool
pub fn is_vertical(self) -> bool
See if this direction points vertically (ie, is North
or South
).
use Direction4::*;
assert!(North.is_vertical());
assert!(!West.is_vertical());
Trait Implementations§
Source§impl Add<Direction4> for ICoord
impl Add<Direction4> for ICoord
Source§impl AddAssign<Direction4> for ICoord
impl AddAssign<Direction4> for ICoord
Source§fn add_assign(&mut self, rhs: Direction4)
fn add_assign(&mut self, rhs: Direction4)
+=
operation. Read moreSource§impl Clone for Direction4
impl Clone for Direction4
Source§fn clone(&self) -> Direction4
fn clone(&self) -> Direction4
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Direction4
impl Debug for Direction4
Source§impl<V> Enum<V> for Direction4
impl<V> Enum<V> for Direction4
Source§impl Hash for Direction4
impl Hash for Direction4
Source§impl Ord for Direction4
impl Ord for Direction4
Source§fn cmp(&self, other: &Direction4) -> Ordering
fn cmp(&self, other: &Direction4) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for Direction4
impl PartialEq for Direction4
Source§impl PartialOrd for Direction4
impl PartialOrd for Direction4
impl Copy for Direction4
impl Eq for Direction4
impl StructuralPartialEq for Direction4
Auto Trait Implementations§
impl Freeze for Direction4
impl RefUnwindSafe for Direction4
impl Send for Direction4
impl Sync for Direction4
impl Unpin for Direction4
impl UnwindSafe for Direction4
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CallHasher for T
impl<T> CallHasher for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more