From 4c4b57dc24bc36b3091931c9dcc36f6b1894a017 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Fri, 27 Nov 2020 22:55:00 +0100 Subject: Change to f64 as the preferred floating point number --- src/transform.rs | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'src/transform.rs') diff --git a/src/transform.rs b/src/transform.rs index bd80bc1..7c1adbf 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -5,15 +5,15 @@ use crate::math::{Rect, Vec2}; -const STANDARD_PIXELS_PER_M: f32 = 64.; -const MIN_PIXELS_PER_M: f32 = 5.; -const MAX_PIXELS_PER_M: f32 = 10_000.; +const STANDARD_PIXELS_PER_M: f64 = 64.; +const MIN_PIXELS_PER_M: f64 = 5.; +const MAX_PIXELS_PER_M: f64 = 10_000.; pub struct Transform { /// The (not necessarily natural) number of pixels per m, i.e. the current scale of the map - pixels_per_m: f32, + pixels_per_m: f64, /// The vector the entire on-screen map is moved by in pixels - translation_px: Vec2, + translation_px: Vec2, } impl Transform { @@ -27,36 +27,36 @@ impl Transform { /// Convert a point that is given in meters into the corresponding point in pixels. #[inline] - pub fn point_m_to_px(&self, point: Vec2) -> Vec2 { + pub fn point_m_to_px(&self, point: &Vec2) -> Vec2 { // Start by converting the absolute position in meters into the absolute position in // pixels, then add the translation of the screen. - (point * self.pixels_per_m) + self.translation_px + (*point * self.pixels_per_m) + self.translation_px } /// Convert an on-screen point into an absolute point with values in meters. #[inline] - pub fn point_px_to_m(&self, point: Vec2) -> Vec2 { + pub fn point_px_to_m(&self, point: &Vec2) -> Vec2 { // Start by subtracting the pixel translation and afterwards convert these absolute pixel // measurements into meters. - (point - self.translation_px) / self.pixels_per_m + (*point - self.translation_px) / self.pixels_per_m } /// Convert a length given in meters into a length in pixels #[inline] - pub fn length_m_to_px(&self, length: f32) -> f32 { + pub fn length_m_to_px(&self, length: f64) -> f64 { length * self.pixels_per_m } /// Convert a length given in pixels into a length in meters #[inline] - pub fn length_px_to_m(&self, length: f32) -> f32 { + pub fn length_px_to_m(&self, length: f64) -> f64 { length / self.pixels_per_m } /// Convert a rectangle which has measurements in meters into one of pixels #[inline] - pub fn rect_m_to_px(&self, rect: Rect) -> Rect { - let left_upper = self.point_m_to_px(Vec2::new(rect.x, rect.y)); + pub fn rect_m_to_px(&self, rect: &Rect) -> Rect { + let left_upper = self.point_m_to_px(&Vec2::new(rect.x, rect.y)); Rect::new( left_upper.x, left_upper.y, @@ -67,8 +67,8 @@ impl Transform { /// Convert a rectangle which has measurements in pixels into one of meters #[inline] - pub fn rect_px_to_m(&self, rect: Rect) -> Rect { - let left_upper = self.point_px_to_m(Vec2::new(rect.x, rect.y)); + pub fn rect_px_to_m(&self, rect: &Rect) -> Rect { + let left_upper = self.point_px_to_m(&Vec2::new(rect.x, rect.y)); Rect::new( left_upper.x, left_upper.y, @@ -84,7 +84,7 @@ impl Transform { /// zoom with a negative factor you'll have to figure out yourself. /// `mouse_pos_px`: Position of the mouse cursor, this time not in meters, but in screen /// pixels. This will be used to tether zoom on that point. - pub fn try_zoom(&mut self, mouse_pos_px: Vec2, factor: f32) -> bool { + pub fn try_zoom(&mut self, mouse_pos_px: &Vec2, factor: f64) -> bool { // Abort zooming when the scale would not be in the min-max-bounds anymore. let desired_px_per_m = self.pixels_per_m * factor; if (factor < 1. && desired_px_per_m <= MIN_PIXELS_PER_M) @@ -94,36 +94,36 @@ impl Transform { } // Save the absolute mouse position in meters for tethering later - let mouse_pos_m = self.point_px_to_m(mouse_pos_px); + let mouse_pos_m = self.point_px_to_m(&mouse_pos_px); // Make sure the desired scale stays within the bounds and in whole numbers let desired_px_per_m = if desired_px_per_m < MIN_PIXELS_PER_M { - MIN_PIXELS_PER_M as u32 as f32 + MIN_PIXELS_PER_M as u32 as f64 } else if desired_px_per_m > MAX_PIXELS_PER_M { - MAX_PIXELS_PER_M as u32 as f32 + MAX_PIXELS_PER_M as u32 as f64 } else { - desired_px_per_m as u32 as f32 + desired_px_per_m as u32 as f64 }; /* Adjust to the desired scale and bring the map back to its desired position according to * the mouse pointer position. */ self.pixels_per_m = desired_px_per_m; - self.translation_px += mouse_pos_px - self.point_m_to_px(mouse_pos_m); + self.translation_px += *mouse_pos_px - self.point_m_to_px(&mouse_pos_m); true } /// Move the canvas by the vector in pixels. - pub fn move_by_px(&mut self, by: Vec2) { - self.translation_px += by; + pub fn move_by_px(&mut self, by: &Vec2) { + self.translation_px += *by; } - pub fn pixels_per_m(&self) -> f32 { + pub fn pixels_per_m(&self) -> f64 { self.pixels_per_m } - pub fn translation_px(&self) -> Vec2 { - self.translation_px + pub fn translation_px(&self) -> &Vec2 { + &self.translation_px } } -- cgit v1.2.3-70-g09d2