generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue