generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -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,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
|
|||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
CoCo_Sol marked this conversation as resolved
Outdated
tipragot
commented
Don't make a new allocation. Use the += operator Don't make a new allocation. Use the += operator
|
|||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue
Don't make a new allocation. Use the += operator