From 44a05e245feca2bf7d171eb1f45f49b2ca89bd68 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 14 Feb 2024 22:12:28 +0100 Subject: [PATCH 1/3] save --- crates/border-wars/src/map/hex.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 1aeaa14..7374783 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -4,6 +4,7 @@ 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}; @@ -24,7 +25,7 @@ pub struct HexPosition { pub r: T, } -impl HexPosition { +impl> HexPosition { /// Returns the distance between two [HexPosition]s. /// /// # How it works @@ -55,6 +56,31 @@ impl HexPosition { // 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 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 HexPosition { -- 2.43.4 From e572f95f9969872ea1475820b3bb7f363461e6b0 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 14 Feb 2024 22:20:05 +0100 Subject: [PATCH 2/3] Fix cargo clippy --- crates/border-wars/src/map/hex.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index 7374783..cd94a46 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -77,7 +77,10 @@ impl> HexPosition { /// ``` 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.0 + * 3f32 + .sqrt() + .mul_add(self.q.as_(), 3f32.sqrt() / 2.0 * (self.r.as_())), size.1 * (3.0 / 2.0 * self.r.as_()), ) } -- 2.43.4 From 078326a7d1d69e068e43f25c369dbfbed58d04a9 Mon Sep 17 00:00:00 2001 From: CoCoSol007 Date: Wed, 14 Feb 2024 22:23:42 +0100 Subject: [PATCH 3/3] change doc --- crates/border-wars/src/map/hex.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/border-wars/src/map/hex.rs b/crates/border-wars/src/map/hex.rs index cd94a46..74db219 100644 --- a/crates/border-wars/src/map/hex.rs +++ b/crates/border-wars/src/map/hex.rs @@ -58,7 +58,7 @@ impl> HexPosition { } /// Converts the current [HexPosition] into a pixel coordinate. - /// Input: The size of a single hexagon in pixels (witdh, height). + /// Input: The size of the hexagon in pixels (witdh, height). /// /// If you want to learn more about pixel coordinates conversion, /// you can check the -- 2.43.4