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