graphics_shapes/contains/
mod.rs

1pub mod circle;
2pub mod ellipse;
3pub mod line;
4pub mod polygon;
5pub mod rect;
6pub mod triangle;
7
8use crate::prelude::*;
9
10/// A shape counts as contained if it is fully inside
11pub trait ContainsShape {
12    /// Returns true if `self` contains `rect`
13    #[must_use]
14    fn contains_rect(&self, rect: &Rect) -> bool
15    where
16        Self: Shape + Sized,
17    {
18        contains_points(self, rect)
19    }
20
21    /// Returns true if `self` contains `circle`
22    #[must_use]
23    fn contains_circle(&self, circle: &Circle) -> bool
24    where
25        Self: Shape + Sized,
26    {
27        contains_points(self, circle)
28    }
29
30    /// Returns true if `self` contains `line`
31    #[must_use]
32    fn contains_line(&self, line: &Line) -> bool
33    where
34        Self: Shape + Sized,
35    {
36        contains_points(self, line)
37    }
38
39    /// Returns true if `self` contains `triangle`
40    #[must_use]
41    fn contains_triangle(&self, triangle: &Triangle) -> bool
42    where
43        Self: Shape + Sized,
44    {
45        contains_points(self, triangle)
46    }
47
48    /// Returns true if `self` contains `ellipse`
49    #[must_use]
50    fn contains_ellipse(&self, ellipse: &Ellipse) -> bool
51    where
52        Self: Shape + Sized,
53    {
54        contains_points(self, ellipse)
55    }
56
57    /// Returns true if `self` contains `polygon`
58    #[must_use]
59    fn contains_polygon(&self, polygon: &Polygon) -> bool
60    where
61        Self: Shape + Sized,
62    {
63        contains_points(self, polygon)
64    }
65}
66
67#[inline]
68fn contains_points(shape: &dyn Shape, other: &dyn Shape) -> bool {
69    for point in other.points() {
70        if !shape.contains(point) {
71            return false;
72        }
73    }
74    true
75}