1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use core::ops::{Mul, Neg};
use crate::vector::Vector;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Direction {
Up,
Down,
Left,
Right,
}
pub use self::Direction::*;
impl Direction {
pub fn sized_vec(self, length: isize) -> Vector {
match self {
Up => Vector::upward(length),
Down => Vector::downward(length),
Left => Vector::leftward(length),
Right => Vector::rightward(length),
}
}
#[inline]
pub fn unit_vec(self) -> Vector {
self.sized_vec(1)
}
#[inline]
pub fn is_vertical(self) -> bool {
match self {
Up | Down => true,
Left | Right => false,
}
}
#[inline]
pub fn is_horizontal(self) -> bool {
!self.is_vertical()
}
#[inline]
pub fn reverse(self) -> Direction {
match self {
Up => Down,
Down => Up,
Left => Right,
Right => Left,
}
}
#[inline]
pub fn clockwise(self) -> Direction {
match self {
Up => Right,
Right => Down,
Down => Left,
Left => Up,
}
}
#[inline]
pub fn counter_clockwise(self) -> Direction {
match self {
Up => Left,
Left => Down,
Down => Right,
Right => Up,
}
}
}
impl Neg for Direction {
type Output = Direction;
fn neg(self) -> Direction {
self.reverse()
}
}
impl Mul<isize> for Direction {
type Output = Vector;
fn mul(self, amount: isize) -> Vector {
self.sized_vec(amount)
}
}