From 94fdc2fd55c2acc77f77dc90226f9a4548a641fc Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 21 Apr 2020 07:39:23 +0200 Subject: [PATCH] Expand and improve math code --- emigui/src/math.rs | 89 +++++++++++++++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 28 deletions(-) diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 6eb7b262..965300a9 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -1,24 +1,21 @@ -#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Copy, Default, Deserialize, Serialize)] pub struct Vec2 { pub x: f32, pub y: f32, } -impl PartialEq for Vec2 { - fn eq(&self, other: &Self) -> bool { - self.x == other.x && self.y == other.y - } +pub fn vec2(x: f32, y: f32) -> Vec2 { + Vec2 { x, y } } -impl Eq for Vec2 {} impl Vec2 { - pub fn splat(v: impl Into) -> Vec2 { + pub fn splat(v: impl Into) -> Self { let v: f32 = v.into(); - Vec2 { x: v, y: v } + Self { x: v, y: v } } #[must_use] - pub fn normalized(self) -> Vec2 { + pub fn normalized(self) -> Self { let len = self.length(); if len <= 0.0 { self @@ -27,7 +24,7 @@ impl Vec2 { } } - pub fn rot90(self) -> Vec2 { + pub fn rot90(self) -> Self { vec2(self.y, -self.x) } @@ -39,15 +36,15 @@ impl Vec2 { self.x * self.x + self.y * self.y } - pub fn dist(a: Vec2, b: Vec2) -> f32 { + pub fn dist(a: Self, b: Self) -> f32 { (a - b).length() } - pub fn dist_sq(a: Vec2, b: Vec2) -> f32 { + pub fn dist_sq(a: Self, b: Self) -> f32 { (a - b).length_sq() } - pub fn angled(angle: f32) -> Vec2 { + pub fn angled(angle: f32) -> Self { vec2(angle.cos(), angle.sin()) } @@ -67,15 +64,22 @@ impl Vec2 { self.x.is_finite() && self.y.is_finite() } - pub fn min(self, other: Vec2) -> Self { + pub fn min(self, other: Self) -> Self { vec2(self.x.min(other.x), self.y.min(other.y)) } - pub fn max(self, other: Vec2) -> Self { + pub fn max(self, other: Self) -> Self { vec2(self.x.max(other.x), self.y.max(other.y)) } } +impl PartialEq for Vec2 { + fn eq(&self, other: &Self) -> bool { + self.x == other.x && self.y == other.y + } +} +impl Eq for Vec2 {} + impl std::ops::Neg for Vec2 { type Output = Vec2; @@ -150,33 +154,32 @@ impl std::ops::Div for Vec2 { } } -pub fn vec2(x: f32, y: f32) -> Vec2 { - Vec2 { x, y } +impl std::fmt::Debug for Vec2 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[{} {}]", self.x, self.y) + } } // ---------------------------------------------------------------------------- /// Sometimes called a Point. I prefer the shorter Pos2 so it is equal length to Vec2 -#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Copy, Default, Deserialize, Serialize)] pub struct Pos2 { pub x: f32, pub y: f32, // implicit w = 1 } -impl PartialEq for Pos2 { - fn eq(&self, other: &Self) -> bool { - self.x == other.x && self.y == other.y - } +pub fn pos2(x: f32, y: f32) -> Pos2 { + Pos2 { x, y } } -impl Eq for Pos2 {} impl Pos2 { - pub fn dist(self: Pos2, other: Pos2) -> f32 { + pub fn dist(self: Self, other: Self) -> f32 { (self - other).length() } - pub fn dist_sq(self: Pos2, other: Pos2) -> f32 { + pub fn dist_sq(self: Self, other: Self) -> f32 { (self - other).length_sq() } @@ -203,8 +206,23 @@ impl Pos2 { pub fn is_finite(&self) -> bool { self.x.is_finite() && self.y.is_finite() } + + pub fn min(self, other: Self) -> Self { + pos2(self.x.min(other.x), self.y.min(other.y)) + } + + pub fn max(self, other: Self) -> Self { + pos2(self.x.max(other.x), self.y.max(other.y)) + } } +impl PartialEq for Pos2 { + fn eq(&self, other: &Self) -> bool { + self.x == other.x && self.y == other.y + } +} +impl Eq for Pos2 {} + impl std::ops::AddAssign for Pos2 { fn add_assign(&mut self, rhs: Vec2) { *self = Pos2 { @@ -254,13 +272,15 @@ impl std::ops::Sub for Pos2 { } } -pub fn pos2(x: f32, y: f32) -> Pos2 { - Pos2 { x, y } +impl std::fmt::Debug for Pos2 { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[{} {}]", self.x, self.y) + } } // ---------------------------------------------------------------------------- -#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Deserialize, Serialize)] +#[derive(Clone, Copy, Default, Eq, PartialEq, Deserialize, Serialize)] pub struct Rect { min: Pos2, max: Pos2, @@ -311,6 +331,13 @@ impl Rect { Rect::from_min_size(self.min() + amnt, self.size()) } + pub fn intersect(self, other: Rect) -> Self { + Self { + min: self.min.max(other.min), + max: self.max.min(other.max), + } + } + // keep min pub fn set_width(&mut self, w: f32) { self.max.x = self.min.x + w; @@ -386,6 +413,12 @@ impl Rect { } } +impl std::fmt::Debug for Rect { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "[{:?} - {:?}]", self.min, self.max) + } +} + // ---------------------------------------------------------------------------- pub fn lerp(min: T, max: T, t: f32) -> T