generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -16,21 +16,22 @@ pub struct HexPosition {
|
||||||
|
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
impl HexPosition {
|
impl HexPosition {
|
||||||
/// Returns the distance between two [HexPosition]
|
/// Returns the distance between two [HexPosition]
|
||||||
///
|
///
|
||||||
/// # How does it work ?
|
/// # How does it work ?
|
||||||
///
|
///
|
||||||
/// Hexagonal grid using the [cube](https://www.redblobgames.com/grids/hexagons/#coordinates) coordinate system,
|
/// Hexagonal grid using the [cube](https://www.redblobgames.com/grids/hexagons/#coordinates) coordinate system,
|
||||||
/// is like a cube in 2D space.
|
/// is like a cube in 2D space.
|
||||||
/// The Manhattan distance between two positions is equal to: the half of the sum of abs(dx) + abs(dy) + abs(dz)
|
/// The Manhattan distance between two positions is equal to: the half of
|
||||||
///
|
/// the sum of abs(dx) + abs(dy) + abs(dz)
|
||||||
|
///
|
||||||
/// # Example:
|
/// # Example:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use border_wars::hex::HexPosition;
|
/// use border_wars::hex::HexPosition;
|
||||||
///
|
///
|
||||||
/// let a = HexPosition { q: 0, r: 0 };
|
/// let a = HexPosition { q: 0, r: 0 };
|
||||||
/// let b = HexPosition { q: 1, r: 1 };
|
/// let b = HexPosition { q: 1, r: 1 };
|
||||||
///
|
///
|
||||||
/// assert_eq!(a.distance_to(&b), 2.0);
|
/// assert_eq!(a.distance_to(&b), 2.0);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn distance_to(&self, other: &HexPosition) -> f32 {
|
pub fn distance_to(&self, other: &HexPosition) -> f32 {
|
||||||
|
@ -43,27 +44,29 @@ impl HexPosition {
|
||||||
// dz = |z1 - z2| and z = -q - r
|
// dz = |z1 - z2| and z = -q - r
|
||||||
let ds = self.q + self.r - other.q - other.r;
|
let ds = 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
|
||||||
(dq.abs() + dr.abs() + ds.abs()) as f32 / 2.
|
(dq.abs() + dr.abs() + ds.abs()) as f32 / 2.
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all positions within a given `range` from the current HexPosition.
|
/// Returns all positions within a given `range` from the current
|
||||||
|
/// HexPosition.
|
||||||
|
///
|
||||||
|
/// This function iterates over the possible q and r values within the
|
||||||
|
/// specified range and inserts HexPositions into a HashSet, ensuring
|
||||||
|
/// that each generated position is within the given range from the
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
dz = -dq - dr dz = -dq - dr
|
|||||||
|
/// current position.
|
||||||
///
|
///
|
||||||
/// This function iterates over the possible q and r values within the specified range
|
|
||||||
/// and inserts HexPositions into a HashSet, ensuring that each generated position
|
|
||||||
/// is within the given range from the current position.
|
|
||||||
///
|
|
||||||
/// for more details: https://www.redblobgames.com/grids/hexagons/#range
|
/// for more details: https://www.redblobgames.com/grids/hexagons/#range
|
||||||
///
|
///
|
||||||
/// # Example:
|
/// # Example:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use border_wars::hex::HexPosition;
|
/// use border_wars::hex::HexPosition;
|
||||||
///
|
///
|
||||||
/// let position = HexPosition { q: 0, r: 0 };
|
/// let position = HexPosition { q: 0, r: 0 };
|
||||||
///
|
///
|
||||||
/// let positions = position.range(1);
|
/// let positions = position.range(1);
|
||||||
///
|
///
|
||||||
/// assert_eq!(positions.len(), 7);
|
/// assert_eq!(positions.len(), 7);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn range(&self, range: i32) -> HashSet<HexPosition> {
|
pub fn range(&self, range: i32) -> HashSet<HexPosition> {
|
||||||
|
@ -75,9 +78,8 @@ impl HexPosition {
|
||||||
}
|
}
|
||||||
result_positions
|
result_positions
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ops::Add<HexPosition> for HexPosition {
|
impl ops::Add<HexPosition> for HexPosition {
|
||||||
type Output = HexPosition;
|
type Output = HexPosition;
|
||||||
|
|
||||||
|
|
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.