Remake utils for hexagon grids #55

Merged
CoCo_Sol merged 10 commits from remake-utile into main 2024-02-16 21:17:15 +00:00
Showing only changes of commit 37bb4ac564 - Show all commits

View file

@ -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)