diff options
| author | Arne Dußin | 2020-11-23 23:38:52 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-23 23:38:52 +0100 |
| commit | e62275d90d3ebf379e8ab268cb77d8eaf6d1cf07 (patch) | |
| tree | 5f8aee175d048e40b8b496157816177e0597e0f9 /src/math/rect.rs | |
| parent | bff1955c38480f2dffd0a10c16ef46dc11320752 (diff) | |
| parent | 3b0c99351da92410bbfaba233e40376b767cb64e (diff) | |
| download | graf_karto-e62275d90d3ebf379e8ab268cb77d8eaf6d1cf07.tar.gz graf_karto-e62275d90d3ebf379e8ab268cb77d8eaf6d1cf07.zip | |
Merge branch 'triangulation' into polygon-rooms
Diffstat (limited to 'src/math/rect.rs')
| -rw-r--r-- | src/math/rect.rs | 112 |
1 files changed, 57 insertions, 55 deletions
diff --git a/src/math/rect.rs b/src/math/rect.rs index 6988b2c..876e728 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -1,6 +1,6 @@ -use super::Vec2; +use super::{LineSegment, Surface, Vec2}; //use alga::general::{Additive, Identity}; -use nalgebra::{RealField, Scalar}; +use nalgebra::{ClosedAdd, RealField, Scalar}; use num_traits::identities::Zero; use serde::{Deserialize, Serialize}; use std::ops::{Add, AddAssign, Sub}; @@ -18,48 +18,6 @@ pub struct Rect<T: Scalar + Copy> { pub h: T, } -// This is sad, but also sadly necessary :/ -impl<T: Into<f32> + Scalar + Copy> Into<raylib::ffi::Rectangle> for Rect<T> { - fn into(self) -> raylib::ffi::Rectangle { - raylib::ffi::Rectangle { - x: self.x.into(), - y: self.y.into(), - width: self.w.into(), - height: self.h.into(), - } - } -} -impl<T: From<f32> + Scalar + Copy> From<raylib::ffi::Rectangle> for Rect<T> { - fn from(r: raylib::ffi::Rectangle) -> Self { - Self { - x: T::from(r.x), - y: T::from(r.y), - w: T::from(r.width), - h: T::from(r.height), - } - } -} -impl<T: Into<f32> + Scalar + Copy> Into<raylib::math::Rectangle> for Rect<T> { - fn into(self) -> raylib::math::Rectangle { - raylib::math::Rectangle { - x: self.x.into(), - y: self.y.into(), - width: self.w.into(), - height: self.h.into(), - } - } -} -impl<T: From<f32> + Scalar + Copy> From<raylib::math::Rectangle> for Rect<T> { - fn from(r: raylib::math::Rectangle) -> Self { - Self { - x: T::from(r.x), - y: T::from(r.y), - w: T::from(r.width), - h: T::from(r.height), - } - } -} - impl<T: Scalar + Copy> Rect<T> { pub fn new(x: T, y: T, w: T, h: T) -> Self { Self { x, y, w, h } @@ -105,17 +63,6 @@ impl<T: Scalar + Copy> Rect<T> { || this.y + this.h < other.y) } - /// Check if the point is inside this Rect and return true if so. - pub fn contains(&self, point: Vec2<T>) -> bool - where - T: PartialOrd + Add<Output = T>, - { - point.x >= self.x - && point.x <= self.x + self.w - && point.y >= self.y - && point.y <= self.y + self.h - } - /// Returns true if the entire rect is contained inside this rectangle. pub fn contains_rect(&self, rect: Rect<T>) -> bool where @@ -181,6 +128,61 @@ impl<T: Scalar + Copy> Rect<T> { } } +impl<T: Scalar + Copy + PartialOrd + ClosedAdd> Surface<T> for Rect<T> { + fn contains_point(&self, point: &Vec2<T>) -> bool { + point.x >= self.x + && point.x <= self.x + self.w + && point.y >= self.y + && point.y <= self.y + self.h + } + + fn contains_line_segment(&self, line_segment: &LineSegment<T>) -> bool { + self.contains_point(&line_segment.start) && self.contains_point(&line_segment.end) + } +} + +// This is sad, but also sadly necessary :/ +impl<T: Into<f32> + Scalar + Copy> Into<raylib::ffi::Rectangle> for Rect<T> { + fn into(self) -> raylib::ffi::Rectangle { + raylib::ffi::Rectangle { + x: self.x.into(), + y: self.y.into(), + width: self.w.into(), + height: self.h.into(), + } + } +} +impl<T: From<f32> + Scalar + Copy> From<raylib::ffi::Rectangle> for Rect<T> { + fn from(r: raylib::ffi::Rectangle) -> Self { + Self { + x: T::from(r.x), + y: T::from(r.y), + w: T::from(r.width), + h: T::from(r.height), + } + } +} +impl<T: Into<f32> + Scalar + Copy> Into<raylib::math::Rectangle> for Rect<T> { + fn into(self) -> raylib::math::Rectangle { + raylib::math::Rectangle { + x: self.x.into(), + y: self.y.into(), + width: self.w.into(), + height: self.h.into(), + } + } +} +impl<T: From<f32> + Scalar + Copy> From<raylib::math::Rectangle> for Rect<T> { + fn from(r: raylib::math::Rectangle) -> Self { + Self { + x: T::from(r.x), + y: T::from(r.y), + w: T::from(r.width), + h: T::from(r.height), + } + } +} + #[cfg(test)] mod test { use super::*; |
