From f3f56ed5f183f8461cc91cb6430d97c725ba159c Mon Sep 17 00:00:00 2001 From: Julian T Date: Sun, 7 Feb 2021 00:37:58 +0100 Subject: Move to double to avoid rounding error This rounding errors seems to happen when adding floats very close to 1 to a relatively large number. 325.0 + 0.99999034 = 326.0 This is a problem as this sample should be counted at the 325 pixel. However this will be a lesser problem when filtering is implemented. --- src/core/vector2.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/core/vector2.rs') diff --git a/src/core/vector2.rs b/src/core/vector2.rs index 858068e..b3fa443 100644 --- a/src/core/vector2.rs +++ b/src/core/vector2.rs @@ -4,6 +4,7 @@ use crate::{Float, Number}; use std::ops::{Sub, Add}; use std::fmt; +use std::cmp::min; #[derive(Clone, Copy)] pub struct Vector2 { @@ -84,6 +85,15 @@ impl From for Vector2i { } } +impl Vector2i { + pub fn cap(&self, x: i32, y: i32) -> Self { + Self::new_xy( + min(self.x, x), + min(self.y, y), + ) + } +} + #[cfg(test)] mod tests { -- cgit v1.2.3