generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -16,10 +16,10 @@ impl<T: Signed + PartialEq + Copy + PartialOrd> HexNumber for T {}
|
|||
/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates).
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct HexPosition<T: HexNumber> {
|
||||
/// Q coordinate
|
||||
/// Q coordinate.
|
||||
pub q: T,
|
||||
|
||||
/// R coordinate
|
||||
/// R coordinate.
|
||||
pub r: T,
|
||||
}
|
||||
|
||||
|
@ -46,13 +46,9 @@ impl<T: HexNumber> HexPosition<T> {
|
|||
/// assert_eq!(a.distance_to(&b), 2);
|
||||
/// ```
|
||||
pub fn distance_to(&self, other: &Self) -> T {
|
||||
// dx = |x1 - x2| where x = q
|
||||
// Calculate the difference between the q and r coordinates.
|
||||
let dq = self.q - other.q;
|
||||
|
||||
// dy = |y1 - y2| where y = r
|
||||
let dr = self.r - other.r;
|
||||
|
||||
// dz = |z1 - z2| where z = -q - r
|
||||
let dz = self.q + self.r - other.q - other.r;
|
||||
|
||||
// Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2
|
||||
|
|
Loading…
Reference in a new issue
I think we should be more generic over the type used. You can use the num crate to do so. And you could still make a specific type using the
type
keyword.