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).
|
/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates).
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct HexPosition<T: HexNumber> {
|
pub struct HexPosition<T: HexNumber> {
|
||||||
/// Q coordinate
|
/// Q coordinate.
|
||||||
pub q: T,
|
pub q: T,
|
||||||
|
|
||||||
/// R coordinate
|
/// R coordinate.
|
||||||
pub r: T,
|
pub r: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,13 +46,9 @@ impl<T: HexNumber> HexPosition<T> {
|
||||||
/// assert_eq!(a.distance_to(&b), 2);
|
/// assert_eq!(a.distance_to(&b), 2);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn distance_to(&self, other: &Self) -> T {
|
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;
|
let dq = self.q - other.q;
|
||||||
|
|
||||||
// dy = |y1 - y2| where y = r
|
|
||||||
let dr = self.r - other.r;
|
let dr = self.r - other.r;
|
||||||
|
|
||||||
// dz = |z1 - z2| where z = -q - r
|
|
||||||
let dz = self.q + self.r - other.q - other.r;
|
let dz = self.q + self.r - other.q - other.r;
|
||||||
|
|
||||||
// Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2
|
// Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2
|
||||||
|
|
Loading…
Reference in a new issue