diff options
| author | Arne Dußin | 2020-12-27 21:54:31 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-12-27 21:54:31 +0100 |
| commit | 53d376eaeef991850d35318b147f75c8f103319d (patch) | |
| tree | 7e95a1666818dc7a804b145f263bdb4b76fef83a /src/math/surface.rs | |
| parent | 2d2f45df9d47db25ac5a91c8f926a025c3a5dc7a (diff) | |
| download | graf_karto-53d376eaeef991850d35318b147f75c8f103319d.tar.gz graf_karto-53d376eaeef991850d35318b147f75c8f103319d.zip | |
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.
Diffstat (limited to 'src/math/surface.rs')
| -rw-r--r-- | src/math/surface.rs | 55 |
1 files changed, 52 insertions, 3 deletions
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<T: Scalar + Copy> { +/// 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<T: RealField, M> +where + T: ApproxEq<Margin = M>, +{ + /// Checks if a point lies on this surface. + fn contains_point(&self, point: &Vec2<T>, margin: M) -> bool; + + /// Checks if a line segment is entirely contained by this surface. + fn contains_line_segment(&self, line_segment: &LineSegment<T>, margin: M) -> bool; + + /// Checks if a rectangle is entirely contained inside this surface. + fn contains_rect(&self, rect: &Rect<T>, margin: M) -> bool; + + /// Checks if a polygon is contained wholly by this surface. + fn contains_polygon(&self, polygon: &Polygon<T>, 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<T>) -> bool; +} + +/// The same as Surface, but the vector space will be assumed to be perfectly divideable or checkable. +pub trait ExactSurface<T: RealField> { /// Checks if a point lies on this surface. fn contains_point(&self, point: &Vec2<T>) -> bool; @@ -21,3 +45,28 @@ pub trait Surface<T: Scalar + Copy> { /// operation for contains_... on a rectangle. fn is_inside_rect(&self, rect: &Rect<T>) -> bool; } + +/* +// Every exact surface must also be an approximate surface, with margin 0 to be exact. +impl<T, S> Surface<T> for S where S: ExactSurface<T> { + fn contains_point<M>(&self, point: &Vec2<T>, _margin: M) -> bool { + ExactSurface::contains_point(&self, point) + } + + fn contains_line_segment<M>(&self, line_segment: &LineSegment<T>, margin: M) -> bool { + ExactSurface::contains_line_segment(&self, line_segment) + } + + fn contains_rect<M>(&self, rect: &Rect<T>, margin: M) -> bool { + ExactSurface::contains_rect(&self, rect) + } + + fn contains_polygon<M>(&self, polygon: &Polygon<T>, margin: M) -> bool { + ExactSurface::contains_polygon(&self, polygon) + } + + fn is_inside_rect(&self, rect: &Rect<T>) -> bool { + ExactSurface::is_inside_rect(&self, rect) + } +} +*/ |
