Add utils for hexagonal grild #50

Merged
tipragot merged 23 commits from hex-utils into main 2024-02-14 17:49:08 +00:00
3 changed files with 11 additions and 18 deletions
Showing only changes of commit f95f63ffd1 - Show all commits

7
Cargo.lock generated
View file

@ -1317,6 +1317,7 @@ dependencies = [
"bevy",
"bevy_egui",
"num",
"partial-min-max",
]
[[package]]
@ -3328,6 +3329,12 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "partial-min-max"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6448add382c60bbbc64f9dab41309a12ec530c05191601042f911356ac09758c"
[[package]]
name = "paste"
version = "1.0.14"

View file

@ -14,3 +14,4 @@ workspace = true
bevy = "0.12.1"
bevy_egui = "0.24.0"
num = "0.4.1"
partial-min-max = "0.4.0"

View file

@ -5,25 +5,10 @@ use std::hash::Hash;
use std::ops;
use num::Signed;
use partial_min_max::{max, min};
/// Represents a number that can be used in a hexagonal grid.
pub trait HexNumber: Signed + PartialEq + Copy + PartialOrd {
/// Returns the minimum value between `self` and `other`.
fn min(&self, other: Self) -> Self {
if *self < other {
return *self;
}
other
}
/// Returns the maximum value between `self` and `other`.
fn max(&self, other: Self) -> Self {
if *self > other {
return *self;
}
other
}
}
pub trait HexNumber: Signed + PartialEq + Copy + PartialOrd {}
impl<T: Signed + PartialEq + Copy + PartialOrd> HexNumber for T {}
/// Represents a position in a hexagonal grid.
@ -99,7 +84,7 @@ impl<T: HexNumber + Eq + Hash + std::cmp::PartialOrd + num::ToPrimitive> HexPosi
pub fn range(&self, range: T) -> HashSet<Self> {
let mut result_positions = HashSet::new();
for q in num::range_inclusive(-range, range) {
for r in num::range_inclusive((-range).max(-q - range), range.min(-q + range)) {
for r in num::range_inclusive(max(-range, -q - range), min(range, -q + range)) {
result_positions.insert(Self { q, r });
}
}