generated from tipragot/rust
Add utils for hexagonal grild #50
|
@ -3,7 +3,7 @@
|
|||
use std::collections::HashSet;
|
||||
use std::ops;
|
||||
|
||||
/// Contains all ``HexPosition`` neighbors of a ``HexPosition``
|
||||
/// Contains all "HexPosition" neighbors of "HexPosition"
|
||||
pub struct GroupHexPosition(pub HashSet<HexPosition>);
|
||||
|
||||
impl GroupHexPosition {
|
||||
|
@ -12,53 +12,8 @@ impl GroupHexPosition {
|
|||
GroupHexPosition(hexes)
|
||||
}
|
||||
|
||||
/// Return an intersection of two ``GroupHexPosition``
|
||||
///
|
||||
/// ## example
|
||||
/// ```
|
||||
/// 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 iter = vec1.iter_with(&vec2);
|
||||
/// assert!(iter.0.contains(&HexPosition { q: 0, r: 0 }));
|
||||
/// ```
|
||||
pub fn iter_with(&self, other: &GroupHexPosition) -> GroupHexPosition {
|
||||
let mut result = HashSet::new();
|
||||
for pos_1 in self.0.iter() {
|
||||
if other.0.contains(pos_1) {
|
||||
result.insert(*pos_1);
|
||||
}
|
||||
}
|
||||
GroupHexPosition::new(result)
|
||||
}
|
||||
|
||||
/// Removes the origin (``HexPosition { q: 0, r: 0 }``) of a
|
||||
/// ``GroupHexPosition``
|
||||
///
|
||||
/// ## example
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// use border_wars::hex::{GroupHexPosition, HexPosition};
|
||||
///
|
||||
/// let mut vec = GroupHexPosition::new(HashSet::from([
|
||||
/// HexPosition { q: 0, r: 0 },
|
||||
/// HexPosition { q: 1, r: -1 },
|
||||
/// ]));
|
||||
/// vec.remove_origin();
|
||||
/// assert_eq!(vec.0.len(), 1);
|
||||
/// ```
|
||||
/// Removes the origin (`HexPosition { q: 0, r: 0 }`) of a
|
||||
/// "GroupHexPosition"
|
||||
CoCo_Sol marked this conversation as resolved
Outdated
|
||||
pub fn remove_origin(&mut self) -> &mut Self {
|
||||
self.0.remove(&HexPosition { q: 0, r: 0 });
|
||||
self
|
||||
|
@ -78,36 +33,13 @@ pub struct HexPosition {
|
|||
}
|
||||
|
||||
impl HexPosition {
|
||||
/// Returns the distance between two ``HexPosition``
|
||||
///
|
||||
/// ## example
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
///
|
||||
/// use border_wars::hex::HexPosition;
|
||||
///
|
||||
/// let hex1 = HexPosition { q: 0, r: 0 };
|
||||
/// let hex2 = HexPosition { q: 1, r: 1 };
|
||||
/// let distance = hex1.distance_to(&hex2);
|
||||
/// assert_eq!(distance, 2.0);
|
||||
/// ```
|
||||
/// Returns the distance between two "HexPosition"
|
||||
pub fn distance_to(&self, other: &HexPosition) -> f32 {
|
||||
(((self.q - other.q).abs()
|
||||
+ (self.r - other.r).abs()
|
||||
+ (self.q + self.r - other.q - other.r).abs())
|
||||
/ 2) as f32
|
||||
|
||||
1.
|
||||
}
|
||||
|
||||
/// Returns all positions in a given range
|
||||
///
|
||||
/// ## example
|
||||
/// ```
|
||||
/// use border_wars::hex::HexPosition;
|
||||
///
|
||||
/// let hex = HexPosition { q: 0, r: 0 };
|
||||
/// let positions = hex.range(1);
|
||||
/// assert_eq!(positions.0.len(), 7);
|
||||
/// ```
|
||||
pub fn range(&self, range: i32) -> GroupHexPosition {
|
||||
let mut positions = HashSet::new();
|
||||
for q in (-range)..=range {
|
||||
|
|
Loading…
Reference in a new issue
I think we should be more generic over the type used. You can use the num crate to do so. And you could still make a specific type using the
type
keyword.