Skip to content

Commit

Permalink
Axis pair unit vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Apr 15, 2024
1 parent f1099e5 commit 1c6dc49
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
* `Hex::NEG_Z`
* `Hex::AXES`
* `Hex::AXES`
* Added axis pair consts as:
* `Hex::INCR_X`
* `Hex::INCR_Y
* `Hex::INCR_Z`
* `Hex::DECR_X`
* `Hex::DECR_Y
* `Hex::DECR_Z`

## 0.16.1

Expand Down
31 changes: 31 additions & 0 deletions src/hex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ impl Hex {
/// -Y (-R) (0, -1)
pub const NEG_Y: Self = Self::new(0, -1);

/// Unit vectors that increase the X axis in clockwise order
pub const INCR_X: [Self; 2] = [Self::new(1, 0), Self::new(1, -1)];
/// Unit vectors that increase the Y axis in clockwise order
pub const INCR_Y: [Self; 2] = [Self::new(0, 1), Self::new(-1, 1)];
/// Unit vectors that increase the Z axis in clockwise order
pub const INCR_Z: [Self; 2] = [Self::new(-1, 0), Self::new(0, -1)];

/// Unit vectors that decrease the X axis in clockwise order
pub const DECR_X: [Self; 2] = [Self::new(-1, 0), Self::new(-1, 1)];
/// Unit vectors that decrease the Y axis in clockwise order
pub const DECR_Y: [Self; 2] = [Self::new(0, -1), Self::new(1, -1)];
/// Unit vectors that decrease the Z axis in clockwise order
pub const DECR_Z: [Self; 2] = [Self::new(1, 0), Self::new(0, 1)];

/// Hexagon edge neighbor coordinates array, following [`EdgeDirection`]
/// order
///
Expand All @@ -118,6 +132,23 @@ impl Hex {
/// \___/
/// Y -Z
/// ```
///
/// Cubic coordinates:
///
/// ```txt
/// (0, -1, 1)
/// ___
/// / \
/// (-1, 0, 1) +--+ 4 +--+ (1, -1, 0)
/// / 3 \___/ 5 \
/// \ / \ /
/// +--+ +--+
/// / \___/ \
/// (-1, 1, 0) \ 2 / \ 0 / (1, 0, -1)
/// +--+ 1 +--+
/// \___/
/// (0, 1, -1)
/// ```
pub const NEIGHBORS_COORDS: [Self; 6] = [
Self::new(1, 0),
Self::new(0, 1),
Expand Down
16 changes: 16 additions & 0 deletions src/hex/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,19 @@ fn resolutions() {
}
}
}

#[test]
fn axis_pairs() {
for [[a, b], [na, nb]] in [
[Hex::INCR_X, Hex::DECR_X],
[Hex::INCR_Y, Hex::DECR_Y],
[Hex::INCR_Z, Hex::DECR_Z],
] {
assert_eq!(a.length(), 1);
assert_eq!(b.length(), 1);
assert_eq!(na.length(), 1);
assert_eq!(nb.length(), 1);
assert_eq!(a.const_neg(), na);
assert_eq!(b.const_neg(), nb);
}
}

0 comments on commit 1c6dc49

Please sign in to comment.