save
Some checks failed
Rust Checks / checks (push) Failing after 2m1s
Rust Checks / checks (pull_request) Failing after 1m25s

This commit is contained in:
CoCo_Sol 2024-02-14 22:12:28 +01:00
parent 663a4160a2
commit 44a05e245f

View file

@ -4,6 +4,7 @@ use std::collections::HashSet;
use std::hash::Hash; use std::hash::Hash;
use std::ops::{Add, AddAssign, Sub, SubAssign}; use std::ops::{Add, AddAssign, Sub, SubAssign};
use num::cast::AsPrimitive;
use num::{FromPrimitive, Signed}; use num::{FromPrimitive, Signed};
use partial_min_max::{max, min}; use partial_min_max::{max, min};
@ -24,7 +25,7 @@ pub struct HexPosition<T: HexNumber> {
pub r: T, pub r: T,
} }
impl<T: HexNumber> HexPosition<T> { impl<T: HexNumber + AsPrimitive<f32>> HexPosition<T> {
/// Returns the distance between two [HexPosition]s. /// Returns the distance between two [HexPosition]s.
/// ///
/// # How it works /// # How it works
@ -55,6 +56,31 @@ impl<T: HexNumber> HexPosition<T> {
// Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2 // Manhattan distance = (abs(dq) + abs(dr) + abs(ds)) / 2
(dq + dr + ds) / (T::one() + T::one()) (dq + dr + ds) / (T::one() + T::one())
} }
/// Converts the current [HexPosition] into a pixel coordinate.
/// Input: The size of a single 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() * (self.q.as_()) + 3f32.sqrt() / 2.0 * (self.r.as_())),
size.1 * (3.0 / 2.0 * self.r.as_()),
)
}
} }
impl<T: HexNumber + Eq + Hash + std::cmp::PartialOrd + num::ToPrimitive> HexPosition<T> { impl<T: HexNumber + Eq + Hash + std::cmp::PartialOrd + num::ToPrimitive> HexPosition<T> {