pub enum Direction8 {
North,
NorthEast,
East,
SouthEast,
South,
SouthWest,
West,
NorthWest,
}
Expand description
Eight-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 Direction8
impl Direction8
Sourcepub const DIRECTIONS: [Direction8; 8]
pub const DIRECTIONS: [Direction8; 8]
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 Direction8::*;
use Rotation::*;
assert_eq!(NorthEast.rotate(Clockwise), East);
assert_eq!(South.rotate(CounterClockwise), SouthEast);
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 Direction8::*;
let north = North;
assert_eq!(north.rotate_by(1), NorthEast);
assert_eq!(north.rotate_by(2), East);
assert_eq!(north.rotate_by(-1), NorthWest);
assert_eq!(north.rotate_by(4), South);
assert_eq!(north.rotate_by(5).rotate_by(-11), East);
Sourcepub fn flip(self) -> Self
pub fn flip(self) -> Self
Flip this direction.
use Direction8::*;
assert_eq!(North.flip(), South);
assert_eq!(West.flip(), East);
assert_eq!(SouthWest.flip(), NorthEast);
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 Direction8::*;
use std::f32::consts::TAU;
let north_radians = North.radians();
assert!((north_radians - (TAU / 8.0 * 6.0)).abs() < 1e-10);
let west_radians = West.radians();
assert!((west_radians - (TAU / 8.0 * 4.0)).abs() < 1e-10);
let southeast_radians = SouthEast.radians();
assert!((southeast_radians - (TAU / 8.0)).abs() < 1e-10);
Trait Implementations§
Source§impl Add<Direction8> for ICoord
impl Add<Direction8> for ICoord
Source§impl AddAssign<Direction8> for ICoord
impl AddAssign<Direction8> for ICoord
Source§fn add_assign(&mut self, rhs: Direction8)
fn add_assign(&mut self, rhs: Direction8)
Performs the
+=
operation. Read moreSource§impl Clone for Direction8
impl Clone for Direction8
Source§fn clone(&self) -> Direction8
fn clone(&self) -> Direction8
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for Direction8
impl Debug for Direction8
Source§impl<V> Enum<V> for Direction8
impl<V> Enum<V> for Direction8
Source§impl Hash for Direction8
impl Hash for Direction8
Source§impl Ord for Direction8
impl Ord for Direction8
Source§fn cmp(&self, other: &Direction8) -> Ordering
fn cmp(&self, other: &Direction8) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for Direction8
impl PartialEq for Direction8
Source§impl PartialOrd for Direction8
impl PartialOrd for Direction8
impl Copy for Direction8
impl Eq for Direction8
impl StructuralPartialEq for Direction8
Auto Trait Implementations§
impl Freeze for Direction8
impl RefUnwindSafe for Direction8
impl Send for Direction8
impl Sync for Direction8
impl Unpin for Direction8
impl UnwindSafe for Direction8
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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