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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::Balance;
impl Balance {
pub const EAST: f64 = 0.0;
pub const NORTH_EAST: f64 = 45.0;
pub const NORTH: f64 = 90.0;
pub const NORTH_WEST: f64 = 135.0;
pub const WEST: f64 = 180.0;
pub const SOUTH_EAST: f64 = -45.0;
pub const SOUTH: f64 = -90.0;
pub const SOUTH_WEST: f64 = -135.0;
/// Returns a unique integer value associated with each `Balance` variant.
///
/// This mapping assigns a unique value to each position in the 3x3 grid,
/// which could be useful for serialization, indexing, or logical calculations
/// that depend on the position.
///
/// # Returns
///
/// An `i8` integer representing the `Balance` variant.
///
/// # Mapping
///
/// - `Balance::TopLeft` => `-4`
/// - `Balance::Top` => `-3`
/// - `Balance::TopRight` => `-2`
/// - `Balance::Left` => `-1`
/// - `Balance::Center` => `0`
/// - `Balance::Right` => `1`
/// - `Balance::BottomLeft` => `2`
/// - `Balance::Bottom` => `3`
/// - `Balance::BottomRight` => `4`
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let position = Balance::Center;
/// assert_eq!(position.to_value(), 0);
///
/// let position = Balance::TopRight;
/// assert_eq!(position.to_value(), -2);
/// ```
pub const fn to_value(self) -> i8 {
match self {
Balance::TopLeft => -4,
Balance::Top => -3,
Balance::TopRight => -2,
Balance::Left => -1,
Balance::Center => 0,
Balance::Right => 1,
Balance::BottomLeft => 2,
Balance::Bottom => 3,
Balance::BottomRight => 4,
}
}
/// Constructs a `Balance` variant from a given `i8` value.
///
/// This method maps an integer value to a specific `Balance` variant.
/// Values outside the valid range will cause a panic.
///
/// # Arguments
///
/// - `value` - An `i8` integer value corresponding to a `Balance` variant.
///
/// # Returns
///
/// A `Balance` instance mapped from the provided integer.
///
/// # Panics
///
/// This function will panic if the input value is not in the range `-4..=4`.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let position = Balance::from_value(-4);
/// assert_eq!(position, Balance::TopLeft);
///
/// let position = Balance::from_value(0);
/// assert_eq!(position, Balance::Center);
///
/// // This will panic:
/// // let invalid = Balance::from_value(5);
/// ```
pub const fn from_value(value: i8) -> Self {
match value {
-4 => Balance::TopLeft,
-3 => Balance::Top,
-2 => Balance::TopRight,
-1 => Balance::Left,
0 => Balance::Center,
1 => Balance::Right,
2 => Balance::BottomLeft,
3 => Balance::Bottom,
4 => Balance::BottomRight,
_ => panic!("Invalid value"),
}
}
/// Calculates the scalar magnitude squared for the vector representation
/// of the current `Balance` position within the grid.
///
/// The scalar magnitude squared is defined as `x^2 + y^2`, where `(x, y)`
/// are the coordinates of the position.
///
/// # Returns
///
/// An `i8` value representing the scalar magnitude squared of the position.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let position = Balance::TopLeft;
/// assert_eq!(position.to_scalar(), 2);
///
/// let center = Balance::Center;
/// assert_eq!(center.to_scalar(), 0);
/// ```
pub const fn to_scalar(self) -> i8 {
let (x, y) = self.to_vector();
x * x + y * y
}
/// Calculates the Euclidean (or absolute) magnitude of the vector representation
/// of the current `Balance` position.
///
/// The magnitude is defined as the square root of the scalar magnitude squared (`√(x² + y²)`),
/// where `(x, y)` are the coordinates of the position.
///
/// # Returns
///
/// A `f64` value representing the Euclidean magnitude of the position with low precision (but fast) calculus.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let position = Balance::TopLeft;
/// assert_eq!(position.to_magnitude(), 2.0f64.sqrt());
///
/// let center = Balance::Center;
/// assert_eq!(center.to_magnitude(), 0.0);
/// ```
pub const fn to_magnitude(self) -> f64 {
if self.is_corner() {
core::f64::consts::SQRT_2
} else if self.is_edge() {
1.0
} else {
0.0
}
}
/// Converts the current `Balance` position into its corresponding
/// angle in degrees in a Cartesian coordinate system.
///
/// The angle is returned in the range `[-180.0, 180.0]` degrees.
///
/// # Returns
///
/// A `f64` value representing the angle in degrees.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let position = Balance::Top;
/// assert_eq!(position.to_angle(), 90.0);
/// ```
pub const fn to_angle(self) -> f64 {
match self {
Balance::TopLeft => Self::NORTH_WEST,
Balance::Top => Self::NORTH,
Balance::TopRight => Self::NORTH_EAST,
Balance::Left => Self::WEST,
Balance::Right => Self::EAST,
Balance::BottomLeft => Self::SOUTH_WEST,
Balance::Bottom => Self::SOUTH,
Balance::BottomRight => Self::SOUTH_EAST,
_ => panic!("Invalid value: cannot convert Balance::Center to an angle."),
}
}
/// Constructs a `Balance` enum variant based on the given angle in degrees.
///
/// # Parameters
///
/// - `angle`: A `f64` value representing the angle in degrees.
///
/// # Returns
///
/// A `Balance` enum variant corresponding to the direction indicated by the angle.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let balance = Balance::from_angle(45.0);
/// assert_eq!(balance, Balance::TopRight);
///
/// let balance = Balance::from_angle(-135.0);
/// assert_eq!(balance, Balance::BottomLeft);
///
/// let balance = Balance::from_angle(270.0);
/// assert_eq!(balance, Balance::Bottom);
/// ```
pub const fn from_angle(angle: f64) -> Self {
let mut angle = angle % 360.0;
if angle > 180.0 {
angle = -(360.0 - angle);
}
match angle {
Self::EAST => Balance::Right,
Self::NORTH_EAST => Balance::TopRight,
Self::NORTH => Balance::Top,
Self::NORTH_WEST => Balance::TopLeft,
Self::WEST => Balance::Left,
Self::SOUTH_WEST => Balance::BottomLeft,
Self::SOUTH => Balance::Bottom,
Self::SOUTH_EAST => Balance::BottomRight,
_ => panic!("Invalid angle. Cannot construct a Balance from an approximate angle."),
}
}
/// Converts the current `Balance` variant into a 2D vector `(i8, i8)` representing its coordinates.
///
/// # Returns
///
/// A tuple `(i8, i8)` representing the position in the 3x3 grid.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let position = Balance::TopLeft;
/// assert_eq!(position.to_vector(), (-1, -1));
///
/// let center = Balance::Center;
/// assert_eq!(center.to_vector(), (0, 0));
/// ```
pub const fn to_vector(self) -> (i8, i8) {
(self.x(), self.y())
}
/// Converts a pair of integers `(a, b)` into the corresponding `Balance` variant.
///
/// # Parameters
///
/// - `a`: The x-coordinate in the 2D grid, expected to be in the range `-1..=1`.
/// - `b`: The y-coordinate in the 2D grid, expected to be in the range `-1..=1`.
///
/// # Returns
///
/// The `Balance` variant that corresponds to the provided `(a, b)` coordinates.
///
/// # Panics
///
/// Panics if the provided `(a, b)` pair does not correspond to a valid `Balance` variant.
///
/// # Examples
///
/// ```
/// use balanced_direction::Balance;
///
/// let balance = Balance::from_vector(-1, -1);
/// assert_eq!(balance, Balance::TopLeft);
///
/// let balance = Balance::from_vector(0, 1);
/// assert_eq!(balance, Balance::Bottom);
/// ```
pub const fn from_vector(a: i8, b: i8) -> Self {
match (a, b) {
(-1, -1) => Balance::TopLeft,
(0, -1) => Balance::Top,
(1, -1) => Balance::TopRight,
(-1, 0) => Balance::Left,
(0, 0) => Balance::Center,
(1, 0) => Balance::Right,
(-1, 1) => Balance::BottomLeft,
(0, 1) => Balance::Bottom,
(1, 1) => Balance::BottomRight,
_ => panic!("Invalid vector"),
}
}
}