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 b04ed1acf4 - Show all commits

View file

@ -2,9 +2,9 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::hash::Hash; use std::hash::Hash;
use std::ops; use std::ops::{Add, AddAssign, Sub, SubAssign};
use num::Signed; use num::{FromPrimitive, Signed};
use partial_min_max::{max, min}; use partial_min_max::{max, min};
/// Represents a number that can be used in a hexagonal grid. /// Represents a number that can be used in a hexagonal grid.
@ -88,7 +88,7 @@ impl<T: HexNumber + Eq + Hash + std::cmp::PartialOrd + num::ToPrimitive> HexPosi
} }
} }
impl<T: HexNumber> ops::Add<Self> for HexPosition<T> { impl<T: HexNumber> Add<Self> for HexPosition<T> {
type Output = Self; type Output = Self;
fn add(self, other: Self) -> Self::Output { fn add(self, other: Self) -> Self::Output {
@ -99,13 +99,14 @@ impl<T: HexNumber> ops::Add<Self> for HexPosition<T> {
} }
} }
impl<T: HexNumber> ops::AddAssign<Self> for HexPosition<T> { impl<T: HexNumber + AddAssign> AddAssign<Self> for HexPosition<T> {
fn add_assign(&mut self, other: Self) { fn add_assign(&mut self, other: Self) {
*self = *self + other; self.q += other.q;
self.r += other.r;
} }
} }
impl<T: HexNumber> ops::Sub<Self> for HexPosition<T> { impl<T: HexNumber> Sub<Self> for HexPosition<T> {
type Output = Self; type Output = Self;
fn sub(self, other: Self) -> Self { fn sub(self, other: Self) -> Self {
@ -116,8 +117,9 @@ impl<T: HexNumber> ops::Sub<Self> for HexPosition<T> {
} }
} }
impl<T: HexNumber> ops::SubAssign<Self> for HexPosition<T> { impl<T: HexNumber + SubAssign> SubAssign<Self> for HexPosition<T> {
fn sub_assign(&mut self, other: Self) { fn sub_assign(&mut self, other: Self) {
*self = *self - other; self.q -= other.q;
self.r -= other.r;
} }
} }