Add utils for hexagonal grild #50

Merged
tipragot merged 23 commits from hex-utils into main 2024-02-14 17:49:08 +00:00
Showing only changes of commit 648bbf9c61 - Show all commits

View file

@ -47,12 +47,12 @@ impl<T: HexNumber> HexPosition<T> {
/// ``` /// ```
pub fn distance_to(&self, other: &Self) -> T { pub fn distance_to(&self, other: &Self) -> T {
// Calculate the difference between the q and r coordinates. // Calculate the difference between the q and r coordinates.
let dq = self.q - other.q; let dq = (self.q - other.q).abs();
let dr = self.r - other.r; let dr = (self.r - other.r).abs();
let dz = self.q + self.r - other.q - other.r; let ds = dq + dr;
// Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2 // Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2
(dq.abs() + dr.abs() + dz.abs()) / (T::one() + T::one()) (dq + dr + ds) / (T::one() + T::one())
} }
} }