Add utils for hexagonal grild #50

Merged
tipragot merged 23 commits from hex-utils into main 2024-02-14 17:49:08 +00:00
Showing only changes of commit 9af2d354e7 - Show all commits

View file

@ -1,6 +1,7 @@
//! This file contains utilities for working with hex coordinates. //! This file contains utilities for working with hex coordinates.
use std::collections::HashSet; use std::collections::HashSet;
use std::ops;
/// Contains all ``HexPosition`` neighbors of a ``HexPosition`` /// Contains all ``HexPosition`` neighbors of a ``HexPosition``
pub struct GroupHexPosition(HashSet<HexPosition>); pub struct GroupHexPosition(HashSet<HexPosition>);
@ -97,3 +98,71 @@ impl HexPosition {
GroupHexPosition(positions) GroupHexPosition(positions)
} }
} }
impl ops::Add<HexPosition> for HexPosition {
type Output = HexPosition;
fn add(self, other: HexPosition) -> HexPosition {
HexPosition {
q: self.q + other.q,
r: self.r + other.r,
}
}
}
impl ops::AddAssign<HexPosition> for HexPosition {
fn add_assign(&mut self, other: HexPosition) {
*self = *self + other;
}
}
impl ops::Sub<HexPosition> for HexPosition {
type Output = HexPosition;
fn sub(self, other: HexPosition) -> HexPosition {
HexPosition {
q: self.q - other.q,
r: self.r - other.r,
}
}
}
impl ops::SubAssign<HexPosition> for HexPosition {
fn sub_assign(&mut self, other: HexPosition) {
*self = *self - other;
}
}
impl ops::Mul<i32> for HexPosition {
type Output = HexPosition;
fn mul(self, other: i32) -> HexPosition {
HexPosition {
q: self.q * other,
r: self.r * other,
}
}
}
impl ops::MulAssign<i32> for HexPosition {
fn mul_assign(&mut self, other: i32) {
*self = *self * other;
}
}
impl ops::Div<i32> for HexPosition {
type Output = HexPosition;
fn div(self, other: i32) -> HexPosition {
HexPosition {
q: self.q / other,
r: self.r / other,
}
}
}
impl ops::DivAssign<i32> for HexPosition {
fn div_assign(&mut self, other: i32) {
*self = *self / other;
}
}