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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use serde::{Deserialize, Serialize};
use super::Rect;
/// Represents the four different possibilities of rotation.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Rotation {
/// A rotation of 0° (ie. no rotation).
/// This is the default value.
///
/// ```txt
/// North
/// +---------+
/// | ^ |
/// | |
/// | |
/// +---------+
/// 0°
/// ```
#[default]
North,
/// A rotation of 90° clockwise.
///
/// ```txt
/// East
/// +---------+
/// | |
/// | >|
/// | |
/// +---------+
/// 90°
/// ```
East,
/// A rotation of 180°.
///
/// ```txt
/// South
/// +---------+
/// | |
/// | |
/// | v |
/// +---------+
/// 180°
/// ```
South,
/// A rotation of 270° clockwise.
///
/// ```txt
/// West
/// +---------+
/// | |
/// |< |
/// | |
/// +---------+
/// 270°
/// ```
West,
}
impl Rotation {
/// Returns whether the aspect ratio of the provided
/// Rect changes with the given rotation.
pub fn aspect_ratio_changes(&self, rect: &Rect) -> bool {
// if the rect is not a square, and the rotation is
// 90° or 270°, then the aspect ratio changes
rect.h != rect.w && matches!(self, Self::West | Self::East)
}
/// Returns the (x, y) coordinate of the point which will be
/// the Rect's anchor after it is rotated.
///
/// ## Explanation
/// The anchor point of a [`Rect`] is usually the top-left (x,y).
/// When a [`Rect`] is rotated inside a layout, then another corner
/// of the [`Rect`] will become the new anchor point after the rotation.
/// This method returns the current position of that corner.
pub fn next_anchor(&self, rect: &Rect) -> (i32, i32) {
match self {
// top-left
Self::North => (rect.x, rect.y),
// bottom-left
Self::East => (rect.x, rect.y + rect.h as i32),
// bottom-right
Self::South => (rect.x + rect.w as i32, rect.y + rect.h as i32),
// top-right
Self::West => (rect.x + rect.w as i32, rect.y),
}
}
/// Get the next rotation variant when rotating clockwise
#[must_use]
pub fn clockwise(&self) -> Self {
match self {
Rotation::North => Rotation::East,
Rotation::East => Rotation::South,
Rotation::South => Rotation::West,
Rotation::West => Rotation::North,
}
}
/// Get the next rotation variant when rotating counter clockwise
#[must_use]
pub fn counter_clockwise(&self) -> Self {
match self {
Rotation::North => Rotation::West,
Rotation::West => Rotation::South,
Rotation::South => Rotation::East,
Rotation::East => Rotation::North,
}
}
}
#[cfg(test)]
mod tests {
use super::Rotation;
use crate::geometry::Rect;
const SQUARE: Rect = Rect {
x: 0,
y: 0,
w: 200,
h: 200,
};
const RECTANGLE: Rect = Rect {
x: 0,
y: 0,
w: 400,
h: 200,
};
#[test]
fn square_never_changes_aspect_ratio() {
let rotations = vec![
Rotation::North,
Rotation::East,
Rotation::South,
Rotation::West,
];
for rotation in rotations {
assert!(!rotation.aspect_ratio_changes(&SQUARE));
}
}
#[test]
fn non_square_changes_aspect_ratio_east_west() {
assert!(Rotation::East.aspect_ratio_changes(&RECTANGLE));
assert!(Rotation::West.aspect_ratio_changes(&RECTANGLE));
}
#[test]
fn non_square_doesnt_change_aspect_ratio_north_south() {
assert!(!Rotation::North.aspect_ratio_changes(&RECTANGLE));
assert!(!Rotation::South.aspect_ratio_changes(&RECTANGLE));
}
#[test]
fn calc_anchor_north() {
let rect = Rect::new(0, 0, 1920, 1080);
let anchor = Rotation::North.next_anchor(&rect);
assert_eq!(anchor, (0, 0));
}
#[test]
fn calc_anchor_east() {
let rect = Rect::new(0, 0, 1920, 1080);
let anchor = Rotation::East.next_anchor(&rect);
assert_eq!(anchor, (0, 1080));
}
#[test]
fn calc_anchor_south() {
let rect = Rect::new(0, 0, 1920, 1080);
let anchor = Rotation::South.next_anchor(&rect);
assert_eq!(anchor, (1920, 1080));
}
#[test]
fn calc_anchor_west() {
let rect = Rect::new(0, 0, 1920, 1080);
let anchor = Rotation::West.next_anchor(&rect);
assert_eq!(anchor, (1920, 0));
}
}