From 53d376eaeef991850d35318b147f75c8f103319d Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Sun, 27 Dec 2020 21:54:31 +0100 Subject: Change to polygongraph instead of polygon in roomtool The polygon room tool used a convoluted process for determining what the user actually wants to draw. I have changed to the polygon graph instead, which makes the checks easier and restricts the user a bit less. In the process however I found a serious problem with my handling float, so everything needed to change to margin compares (which I of course should have done in the beginning. Guys, take the warning seriously and don't ignore it for ten years like I did. It will come back to haunt you.. apparently) instead of direct equality. --- src/math/surface.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'src/math/surface.rs') diff --git a/src/math/surface.rs b/src/math/surface.rs index ab1c703..088ac47 100644 --- a/src/math/surface.rs +++ b/src/math/surface.rs @@ -1,10 +1,34 @@ //! Surfaces, which are areas at a certain position in a vector space. use super::{LineSegment, Polygon, Rect, Vec2}; -use nalgebra::Scalar; +use float_cmp::ApproxEq; +use nalgebra::RealField; -/// Trait that describes an area in the vector space on the field of T -pub trait Surface { +/// Trait that describes an area in the vector space on the field of T, with T unable to be +/// used without rounding. +pub trait Surface +where + T: ApproxEq, +{ + /// Checks if a point lies on this surface. + fn contains_point(&self, point: &Vec2, margin: M) -> bool; + + /// Checks if a line segment is entirely contained by this surface. + fn contains_line_segment(&self, line_segment: &LineSegment, margin: M) -> bool; + + /// Checks if a rectangle is entirely contained inside this surface. + fn contains_rect(&self, rect: &Rect, margin: M) -> bool; + + /// Checks if a polygon is contained wholly by this surface. + fn contains_polygon(&self, polygon: &Polygon, margin: M) -> bool; + + /// Checks if this surface is contained by the rect in it's entirety. Think of it as the reverse + /// operation for contains_... on a rectangle. + fn is_inside_rect(&self, rect: &Rect) -> bool; +} + +/// The same as Surface, but the vector space will be assumed to be perfectly divideable or checkable. +pub trait ExactSurface { /// Checks if a point lies on this surface. fn contains_point(&self, point: &Vec2) -> bool; @@ -21,3 +45,28 @@ pub trait Surface { /// operation for contains_... on a rectangle. fn is_inside_rect(&self, rect: &Rect) -> bool; } + +/* +// Every exact surface must also be an approximate surface, with margin 0 to be exact. +impl Surface for S where S: ExactSurface { + fn contains_point(&self, point: &Vec2, _margin: M) -> bool { + ExactSurface::contains_point(&self, point) + } + + fn contains_line_segment(&self, line_segment: &LineSegment, margin: M) -> bool { + ExactSurface::contains_line_segment(&self, line_segment) + } + + fn contains_rect(&self, rect: &Rect, margin: M) -> bool { + ExactSurface::contains_rect(&self, rect) + } + + fn contains_polygon(&self, polygon: &Polygon, margin: M) -> bool { + ExactSurface::contains_polygon(&self, polygon) + } + + fn is_inside_rect(&self, rect: &Rect) -> bool { + ExactSurface::is_inside_rect(&self, rect) + } +} +*/ -- cgit v1.2.3-70-g09d2