generated from tipragot/rust
Remake utils for hexagon grids #55
|
@ -309,37 +309,6 @@ impl<T: Number> HexPosition<T> {
|
|||
x.abs() + y.abs() + (x + y).abs() / T::TWO
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// Note that the original position is also returned.
|
||||
///
|
||||
/// For more details, refer to: https://www.redblobgames.com/grids/hexagons/#range
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// use border_wars::map::hex::HexPosition;
|
||||
///
|
||||
/// let position = HexPosition(0, 0);
|
||||
/// let range = 1;
|
||||
///
|
||||
/// let positions = position.range(range);
|
||||
///
|
||||
/// assert_eq!(positions.len(), 7);
|
||||
/// ```
|
||||
pub fn range(&self, range: isize) -> Vec<Self> {
|
||||
let mut result_positions = Vec::new();
|
||||
for q in -range..=range {
|
||||
for r in Number::min(-range, -q - range)..=Number::min(range, -q + range) {
|
||||
result_positions.push(Self(T::from_isize(q), T::from_isize(r)));
|
||||
}
|
||||
}
|
||||
result_positions
|
||||
}
|
||||
|
||||
/// Returns the hexagonal ring of the given radius.
|
||||
/// If you want to learn more about hexagonal grids, check the
|
||||
/// [documentation](https://www.redblobgames.com/grids/hexagons/#rings)
|
||||
|
|
Loading…
Reference in a new issue