save
Some checks failed
Rust Checks / checks (push) Failing after 7s

This commit is contained in:
CoCo_Sol 2024-02-16 01:06:40 +01:00
parent 008a8f73e6
commit b7f8dfb064

View file

@ -118,8 +118,8 @@ impl HexDirection {
pub struct HexRing<T: Number> {
current: HexPosition<T>,
direction: HexDirection,
radius: usize,
index: usize,
radius: T,
index: T,
}
impl<T: Number> Iterator for HexRing<T> {
@ -160,16 +160,16 @@ impl<T: Number> Iterator for HexRing<T> {
pub struct HexSpiral<T: Number> {
origin: HexPosition<T>,
current: HexRing<T>,
radius: usize,
index: usize,
radius: T,
index: T,
}
impl<T: Number + AsPrimitive<usize> + FromPrimitive> Iterator for HexSpiral<T> {
impl<T: Number> Iterator for HexSpiral<T> {
type Item = HexPosition<T>;
fn next(&mut self) -> Option<Self::Item> {
if self.index == 0 {
self.index += 1;
if self.index == T::TWO {
self.index += T::ONE;
return Some(self.origin);
}
if self.index > self.radius {
@ -177,15 +177,15 @@ impl<T: Number + AsPrimitive<usize> + FromPrimitive> Iterator for HexSpiral<T> {
}
let mut result = self.current.next();
if result.is_none() && self.index < self.radius {
self.index += 1;
self.current = self.origin.ring(T::from_usize(self.index).unwrap());
self.index += T::ONE;
self.current = self.origin.ring(self.index);
result = self.current.next();
}
result
}
}
impl<T: Number + AsPrimitive<usize> + FromPrimitive> HexPosition<T> {
impl<T: Number> HexPosition<T> {
/// Creates a new hexagonal position.
pub fn new(x: T, y: T) -> Self {
Self(x, y)
@ -193,29 +193,27 @@ impl<T: Number + AsPrimitive<usize> + FromPrimitive> HexPosition<T> {
/// Returns the distance between two hexagonal positions.
pub fn distance(self, other: Self) -> T {
let HexPosition(x, y) = self - other;
let Self(x, y) = self - other;
x.abs() + y.abs() + (x + y).abs() / T::TWO
}
/// Returns the hexagonal ring of the given radius.
pub fn ring(self, radius: T) -> HexRing<T> {
let usize_radius = radius.as_();
HexRing {
current: self + HexDirection::DownLeft.to_vector() * radius,
direction: HexDirection::Right,
radius: usize_radius,
index: 0,
radius,
index: T::ZERO,
}
}
/// Returns the hexagonal spiral of the given radius.
pub fn spiral(self, radius: T) -> HexSpiral<T> {
let usize_radius = radius.as_();
HexSpiral {
origin: self,
current: self.ring(T::ONE),
radius: usize_radius,
index: 0,
radius,
index: T::ZERO,
}
}
}