From 8d7010c0c8760779da6f3b65d43f8d63546394d2 Mon Sep 17 00:00:00 2001 From: Corentin Date: Fri, 16 Feb 2024 09:10:04 +0000 Subject: [PATCH] Delete crates/border-wars/src/map/hex_save.rs --- crates/border-wars/src/map/hex_save.rs | 155 ------------------------- 1 file changed, 155 deletions(-) delete mode 100644 crates/border-wars/src/map/hex_save.rs diff --git a/crates/border-wars/src/map/hex_save.rs b/crates/border-wars/src/map/hex_save.rs deleted file mode 100644 index 74db219..0000000 --- a/crates/border-wars/src/map/hex_save.rs +++ /dev/null @@ -1,155 +0,0 @@ -//! All functions related to calculations in a hexagonal grid. - -use std::collections::HashSet; -use std::hash::Hash; -use std::ops::{Add, AddAssign, Sub, SubAssign}; - -use num::cast::AsPrimitive; -use num::{FromPrimitive, Signed}; -use partial_min_max::{max, min}; - -/// Represents a number that can be used in a hexagonal grid. -pub trait HexNumber: Signed + PartialEq + Copy + PartialOrd + FromPrimitive {} - -impl HexNumber for T {} - -/// Represents a position in a hexagonal grid. -/// We use the axial coordinate system explained in this -/// [documentation](https://www.redblobgames.com/grids/hexagons/#coordinates). -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct HexPosition { - /// Q coordinate. - pub q: T, - - /// R coordinate. - pub r: T, -} - -impl> HexPosition { - /// Returns the distance between two [HexPosition]s. - /// - /// # How it works - /// - /// In the hexagonal grid, using the - /// [cube coordinate system](https://www.redblobgames.com/grids/hexagons/#coordinates), - /// it's akin to a cube in 3D space. - /// The Manhattan distance between two positions is equal to half of - /// the sum of abs(dx) + abs(dy) + abs(dz). - /// However, in hexagonal grids, z is defined as -q - r. - /// - /// # Example - /// - /// ```no_run - /// use border_wars::map::hex::HexPosition; - /// - /// let a = HexPosition { q: 0, r: 0 }; - /// let b = HexPosition { q: 1, r: 1 }; - /// - /// assert_eq!(a.distance_to(&b), 2); - /// ``` - pub fn distance_to(&self, other: &Self) -> T { - // Calculate the difference between the q and r coordinates. - let dq = (self.q - other.q).abs(); - let dr = (self.r - other.r).abs(); - let ds = dq + dr; - - // Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2 - (dq + dr + ds) / (T::one() + T::one()) - } - - /// Converts the current [HexPosition] into a pixel coordinate. - /// Input: The size of the hexagon in pixels (witdh, height). - /// - /// If you want to learn more about pixel coordinates conversion, - /// you can check the - /// [documentation](https://www.redblobgames.com/grids/hexagons/#hex-to-pixel). - /// - /// # Example - /// - /// ```no_run - /// use border_wars::map::hex::HexPosition; - /// - /// let position = HexPosition { q: 1, r: 0 }; - /// assert_eq!( - /// position.to_pixel_coordinates((1.0, 1.0)), - /// (3f32.sqrt(), 0.0) - /// ); - /// ``` - pub fn to_pixel_coordinates(&self, size: (f32, f32)) -> (f32, f32) { - ( - size.0 - * 3f32 - .sqrt() - .mul_add(self.q.as_(), 3f32.sqrt() / 2.0 * (self.r.as_())), - size.1 * (3.0 / 2.0 * self.r.as_()), - ) - } -} - -impl HexPosition { - /// Returns all positions within a given `range` from the current - /// `HexPosition`. - /// - /// This function iterates over the possible q and r values within the - /// specified range. - /// Note that the original position is also returned. - /// - /// For more details, refer to: https://www.redblobgames.com/grids/hexagons/#range - /// - /// # Example - /// - /// ``` - /// use border_wars::map::hex::HexPosition; - /// - /// let position = HexPosition { q: 0, r: 0 }; - /// - /// let positions = position.range(1); - /// - /// assert_eq!(positions.len(), 7); - /// ``` - pub fn range(&self, range: T) -> HashSet { - let mut result_positions = HashSet::new(); - for q in num::range_inclusive(-range, range) { - for r in num::range_inclusive(max(-range, -q - range), min(range, -q + range)) { - result_positions.insert(Self { q, r }); - } - } - result_positions - } -} - -impl Add for HexPosition { - type Output = Self; - - fn add(self, other: Self) -> Self::Output { - Self { - q: self.q + other.q, - r: self.r + other.r, - } - } -} - -impl AddAssign for HexPosition { - fn add_assign(&mut self, other: Self) { - self.q += other.q; - self.r += other.r; - } -} - -impl Sub for HexPosition { - type Output = Self; - - fn sub(self, other: Self) -> Self { - Self { - q: self.q - other.q, - r: self.r - other.r, - } - } -} - -impl SubAssign for HexPosition { - fn sub_assign(&mut self, other: Self) { - self.q -= other.q; - self.r -= other.r; - } -}