diff options
Diffstat (limited to 'src/math')
| -rw-r--r-- | src/math/polygon/mod.rs | 10 | ||||
| -rw-r--r-- | src/math/rect.rs | 17 |
2 files changed, 24 insertions, 3 deletions
diff --git a/src/math/polygon/mod.rs b/src/math/polygon/mod.rs index bc145ed..02d8abb 100644 --- a/src/math/polygon/mod.rs +++ b/src/math/polygon/mod.rs @@ -58,13 +58,11 @@ impl<T: Scalar + Copy> Polygon<T> { } /// Like new, but does not perform any validity checks, so be careful when using this function. - pub fn new_unchecked<M>(corners: Vec<Vec2<T>>, t_margin: M) -> Self + pub(crate) fn new_unchecked<M>(corners: Vec<Vec2<T>>, t_margin: M) -> Self where T: RealField + ApproxEq<Margin = M>, M: Copy, { - assert!(Polygon::check_validity(&corners, t_margin).is_ok()); - let corners = if combined_angle(&corners, t_margin) > T::zero() { corners } else { @@ -74,6 +72,12 @@ impl<T: Scalar + Copy> Polygon<T> { Self { corners } } + /// Create a polygon from the sorted vertices. This will create an invalid polygon if the + /// vertices are not sorted so that the edges turn counterclockwise, so use with caution. + pub(crate) fn from_vertices(corners: Vec<Vec2<T>>) -> Self { + Self { corners } + } + /// Checks if a set of corners can be made into a polygon or not. Returns Ok if they can, or /// the reason they cannot in form of a PolygonError. pub fn check_validity<M>(corners: &[Vec2<T>], t_margin: M) -> Result<(), PolygonError<T>> diff --git a/src/math/rect.rs b/src/math/rect.rs index b019ad5..adb608b 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -229,6 +229,23 @@ impl<T: Scalar + Copy + ToPrimitive> Into<raylib::ffi::Rectangle> for Rect<T> { } } +/* Convert the rectangle into a polygon. This is the same as creating a convex hull from the corner + * points, but is a specific case. + */ +impl<T: Scalar + Copy + ToPrimitive, P: Scalar + Copy> Into<Polygon<P>> for Rect<T> +where + T: Into<P> + Add<Output = T>, +{ + fn into(self) -> Polygon<P> { + Polygon::from_vertices(vec![ + Vec2::new(self.x.into(), self.y.into()), + Vec2::new(self.x.into(), (self.y + self.h).into()), + Vec2::new((self.x + self.w).into(), (self.y + self.h).into()), + Vec2::new((self.x + self.w).into(), self.y.into()), + ]) + } +} + #[cfg(test)] mod test { use super::*; |
