generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -4,31 +4,43 @@ use std::collections::HashSet;
|
||||||
use std::ops;
|
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(pub HashSet<HexPosition>);
|
||||||
|
|
||||||
impl GroupHexPosition {
|
impl GroupHexPosition {
|
||||||
|
/// TODO
|
||||||
|
pub fn new(hexes: HashSet<HexPosition>) -> Self {
|
||||||
|
GroupHexPosition(hexes)
|
||||||
|
}
|
||||||
|
|
||||||
/// Return an intersection of two ``GroupHexPosition``
|
/// Return an intersection of two ``GroupHexPosition``
|
||||||
///
|
///
|
||||||
/// ## example
|
/// ## example
|
||||||
/// ```
|
/// ```
|
||||||
/// use border_wars::hex::GroupHexPosition;
|
/// use std::collections::HashSet;
|
||||||
|
///
|
||||||
|
/// use border_wars::hex::{GroupHexPosition, HexPosition};
|
||||||
|
///
|
||||||
|
/// let vec1 = GroupHexPosition::new(HashSet::from([
|
||||||
|
/// HexPosition { q: 0, r: 0 },
|
||||||
|
/// HexPosition { q: 1, r: -1 },
|
||||||
|
/// ]));
|
||||||
|
///
|
||||||
|
/// let vec2 = GroupHexPosition::new(HashSet::from([
|
||||||
|
/// HexPosition { q: 0, r: 0 },
|
||||||
|
/// HexPosition { q: 1, r: 1 },
|
||||||
|
/// ]));
|
||||||
///
|
///
|
||||||
/// let vec1 = GroupHexPosition(
|
|
||||||
/// vec![HexPosition { q: 0, r: 0 }, HexPosition { q: 1, r: -1 }]
|
|
||||||
/// );
|
|
||||||
/// let vec2 = GroupHexPosition(
|
|
||||||
/// vec![HexPosition { q: 0, r: 0 }, HexPosition { q: 0, r: -1 }]
|
|
||||||
/// );
|
|
||||||
/// let iter = vec1.iter_with(&vec2);
|
/// let iter = vec1.iter_with(&vec2);
|
||||||
/// assert_eq!(iter, vec![HexPosition { q: 0, r: 0 }]);
|
/// assert!(iter.0.contains(&HexPosition { q: 0, r: 0 }));
|
||||||
pub fn iter_with(&self, other: &GroupHexPosition) -> Vec<HexPosition> {
|
/// ```
|
||||||
let mut result = Vec::new();
|
pub fn iter_with(&self, other: &GroupHexPosition) -> GroupHexPosition {
|
||||||
|
let mut result = HashSet::new();
|
||||||
for pos_1 in self.0.iter() {
|
for pos_1 in self.0.iter() {
|
||||||
if other.0.contains(pos_1) {
|
if other.0.contains(pos_1) {
|
||||||
result.push(*pos_1);
|
result.insert(*pos_1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
GroupHexPosition::new(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Removes the origin (``HexPosition { q: 0, r: 0 }``) of a
|
/// Removes the origin (``HexPosition { q: 0, r: 0 }``) of a
|
||||||
|
@ -36,14 +48,16 @@ impl GroupHexPosition {
|
||||||
///
|
///
|
||||||
/// ## example
|
/// ## example
|
||||||
/// ```
|
/// ```
|
||||||
/// use border_wars::hex::GroupHexPosition;
|
/// use std::collections::HashSet;
|
||||||
///
|
///
|
||||||
/// let mut vec = GroupHexPosition(vec![
|
/// use border_wars::hex::{GroupHexPosition, HexPosition};
|
||||||
|
///
|
||||||
|
/// let mut vec = GroupHexPosition::new(HashSet::from([
|
||||||
/// HexPosition { q: 0, r: 0 },
|
/// HexPosition { q: 0, r: 0 },
|
||||||
/// HexPosition { q: 1, r: -1 },
|
/// HexPosition { q: 1, r: -1 },
|
||||||
/// ]);
|
/// ]));
|
||||||
/// vec.remove_origin();
|
/// vec.remove_origin();
|
||||||
/// assert_eq!(vec.0, vec![HexPosition { q: 1, r: -1 }]);
|
/// assert_eq!(vec.0.len(), 1);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn remove_origin(&mut self) -> &mut Self {
|
pub fn remove_origin(&mut self) -> &mut Self {
|
||||||
self.0.remove(&HexPosition { q: 0, r: 0 });
|
self.0.remove(&HexPosition { q: 0, r: 0 });
|
||||||
|
@ -68,12 +82,14 @@ impl HexPosition {
|
||||||
///
|
///
|
||||||
/// ## example
|
/// ## example
|
||||||
/// ```
|
/// ```
|
||||||
|
/// use std::collections::HashSet;
|
||||||
|
///
|
||||||
/// use border_wars::hex::HexPosition;
|
/// use border_wars::hex::HexPosition;
|
||||||
///
|
///
|
||||||
/// let hex1 = HexPosition { q: 0, r: 0 };
|
/// let hex1 = HexPosition { q: 0, r: 0 };
|
||||||
/// let hex2 = HexPosition { q: 1, r: 1 };
|
/// let hex2 = HexPosition { q: 1, r: 1 };
|
||||||
/// let distance = hex1.distance_to(&hex2);
|
/// let distance = hex1.distance_to(&hex2);
|
||||||
/// assert_eq!(distance, 2);
|
/// assert_eq!(distance, 2.0);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn distance_to(&self, other: &HexPosition) -> f32 {
|
pub fn distance_to(&self, other: &HexPosition) -> f32 {
|
||||||
(((self.q - other.q).abs()
|
(((self.q - other.q).abs()
|
||||||
|
@ -90,7 +106,7 @@ impl HexPosition {
|
||||||
///
|
///
|
||||||
/// let hex = HexPosition { q: 0, r: 0 };
|
/// let hex = HexPosition { q: 0, r: 0 };
|
||||||
/// let positions = hex.range(1);
|
/// let positions = hex.range(1);
|
||||||
/// assert_eq!(positions.len(), 6);
|
/// assert_eq!(positions.0.len(), 6);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn range(&self, range: i32) -> GroupHexPosition {
|
pub fn range(&self, range: i32) -> GroupHexPosition {
|
||||||
let mut positions = HashSet::new();
|
let mut positions = HashSet::new();
|
||||||
|
|
Loading…
Reference in a new issue