1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Shapes for Graphics
//!
//! Provides shapes for simple graphics
//!
//! ```
//! # use graphics_shapes::coord::Coord;
//! # use graphics_shapes::rect::Rect;
//! # use graphics_shapes::{coord, Shape};
//! # use graphics_shapes::triangle::Triangle;
//! let rect = Rect::new((10,10),(20,20));
//! assert!(rect.contains(coord!(15,15)));
//! let triangle = Triangle::new((34,5),(12,30),(9,10));
//! let rotated = triangle.rotate(45);
//!
//! let start = coord!(20,130);
//! let dist = start.distance((30,130));
//!```

#![forbid(unsafe_code)]

use crate::coord::Coord;
use crate::general_math::{rotate_points, scale_points};
use crate::prelude::*;
use crate::shape_box::ShapeBox;
use fnv::FnvHashSet;
use std::any::Any;

pub mod circle;
#[macro_use]
pub mod coord;
pub mod contains;
pub mod ellipse;
pub mod general_math;
pub mod intersection;
pub mod lerp;
pub mod line;
pub mod polygon;
pub mod rect;
pub mod shape_box;
pub mod triangle;

pub mod prelude {
    pub use crate::circle::*;
    pub use crate::contains::ContainsShape;
    pub use crate::coord;
    pub use crate::coord::*;
    pub use crate::ellipse::*;
    pub use crate::intersection::IntersectsShape;
    pub use crate::lerp::*;
    pub use crate::line::*;
    pub use crate::polygon::*;
    pub use crate::rect::*;
    pub use crate::triangle::*;
    pub use crate::IntersectsContains;
    pub use crate::Shape;
}

pub trait AnyToAny: 'static {
    fn as_any(&self) -> &dyn Any;
}

impl<T: 'static> AnyToAny for T {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

pub trait Shape: AnyToAny {
    /// create this shape from a list of points (corners of a shape or tips of a line)
    #[must_use]
    fn from_points(points: &[Coord]) -> Self
    where
        Self: Sized;

    #[must_use]
    fn rebuild(&self, points: &[Coord]) -> Self
    where
        Self: Sized;

    /// change every point by +`delta`
    #[must_use]
    fn translate_by(&self, delta: Coord) -> Self
    where
        Self: Sized,
    {
        let points: Vec<Coord> = self.points().iter().map(|p| *p + delta).collect();
        self.rebuild(&points)
    }

    /// moves the shapes first point to `point`
    /// (and changes every other point to match their original distance and angle)
    ///
    /// As this moves self.points()[0] the result might be unexpected if the shape was created
    /// right to left and/or bottom to top
    #[must_use]
    fn move_to(&self, point: Coord) -> Self
    where
        Self: Sized,
    {
        let diff = (point) - self.points()[0];
        self.translate_by(diff)
    }

    /// Moves the shapes center to `point`
    /// (and changes every other point to match their original distance and angle)
    ///
    /// As this moves relative to self.points()[0] the result might be unexpected if the shape was created
    /// right to left and/or bottom to top
    #[must_use]
    fn move_center_to(&self, point: Coord) -> Self
    where
        Self: Sized,
    {
        let diff = point - self.center();
        self.translate_by(diff)
    }

    /// Returns true if the shape contains point
    #[must_use]
    fn contains(&self, point: Coord) -> bool;

    /// Points(corners/ends) the shape is made of
    #[must_use]
    fn points(&self) -> Vec<Coord>;

    /// Rotate shape around it's center
    #[must_use]
    fn rotate(&self, degrees: isize) -> Self
    where
        Self: Sized,
    {
        self.rotate_around(degrees, self.center())
    }

    /// Rotate shape around a point
    #[must_use]
    fn rotate_around(&self, degrees: isize, point: Coord) -> Self
    where
        Self: Sized,
    {
        let points = rotate_points(point, &self.points(), degrees);
        self.rebuild(&points)
    }

    /// Center of shape
    #[must_use]
    fn center(&self) -> Coord;

    /// x of the left most point
    #[must_use]
    fn left(&self) -> isize {
        self.points().iter().map(|p| p.x).min().unwrap()
    }

    /// x of the right most point
    #[must_use]
    fn right(&self) -> isize {
        self.points().iter().map(|p| p.x).max().unwrap()
    }

    /// y of the top most point
    #[must_use]
    fn top(&self) -> isize {
        self.points().iter().map(|p| p.y).min().unwrap()
    }

    /// y of the bottom most point
    #[must_use]
    fn bottom(&self) -> isize {
        self.points().iter().map(|p| p.y).max().unwrap()
    }

    #[must_use]
    fn top_left(&self) -> Coord {
        coord!(self.left(), self.top())
    }

    #[must_use]
    fn top_right(&self) -> Coord {
        coord!(self.right(), self.top())
    }

    #[must_use]
    fn bottom_left(&self) -> Coord {
        coord!(self.left(), self.bottom())
    }

    #[must_use]
    fn bottom_right(&self) -> Coord {
        coord!(self.right(), self.bottom())
    }

    /// Scale the shape by factor (around the center, so the change will be uniform)
    #[must_use]
    fn scale(&self, factor: f32) -> Self
    where
        Self: Sized,
    {
        self.scale_around(factor, self.center())
    }

    /// Scale the shape by factor around point
    #[must_use]
    fn scale_around(&self, factor: f32, point: Coord) -> Self
    where
        Self: Sized,
    {
        let points = scale_points(point, &self.points(), factor);
        self.rebuild(&points)
    }

    /// The coords for drawing the shape outline, the points may be in any order
    /// This should be cached rather than called per frame
    #[must_use]
    fn outline_pixels(&self) -> Vec<Coord>;

    /// The coords for drawing the filled shape, the points may be in any order
    /// This should be cached rather than called per frame
    #[must_use]
    fn filled_pixels(&self) -> Vec<Coord>;

    #[must_use]
    fn to_shape_box(&self) -> ShapeBox;
}

//Separate so `Shape`s don't have to implement Contains and Intersects
pub trait IntersectsContains: Shape + ContainsShape + IntersectsShape + Sized {
    /// Returns
    /// * Some(true) if `self` contains `other`
    /// * Some(false) if `self` does not contain `other`
    /// * None if `other` isn't a supported `Shape`
    #[must_use]
    fn contains_shape(&self, other: &dyn Shape) -> Option<bool> {
        if let Some(line) = other.as_any().downcast_ref::<Line>() {
            return Some(self.contains_line(line));
        }
        if let Some(rect) = other.as_any().downcast_ref::<Rect>() {
            return Some(self.contains_rect(rect));
        }
        if let Some(triangle) = other.as_any().downcast_ref::<Triangle>() {
            return Some(self.contains_triangle(triangle));
        }
        if let Some(polygon) = other.as_any().downcast_ref::<Polygon>() {
            return Some(self.contains_polygon(polygon));
        }
        if let Some(circle) = other.as_any().downcast_ref::<Circle>() {
            return Some(self.contains_circle(circle));
        }
        if let Some(ellipse) = other.as_any().downcast_ref::<Ellipse>() {
            return Some(self.contains_ellipse(ellipse));
        }
        if let Some(shapebox) = other.as_any().downcast_ref::<ShapeBox>() {
            return Some(match shapebox {
                ShapeBox::Line(line) => self.contains_line(line),
                ShapeBox::Rect(rect) => self.contains_rect(rect),
                ShapeBox::Triangle(triangle) => self.contains_triangle(triangle),
                ShapeBox::Circle(circle) => self.contains_circle(circle),
                ShapeBox::Ellipse(ellipse) => self.contains_ellipse(ellipse),
                ShapeBox::Polygon(polygon) => self.contains_polygon(polygon),
            });
        }
        None
    }

    /// Returns
    /// * Some(true) if `self` intersects `other`
    /// * Some(false) if `self` does not intersects `other`
    /// * None if `other` isn't a supported `Shape`
    #[must_use]
    fn intersects_shape(&self, other: &dyn Shape) -> Option<bool> {
        if let Some(line) = other.as_any().downcast_ref::<Line>() {
            return Some(self.intersects_line(line));
        }
        if let Some(rect) = other.as_any().downcast_ref::<Rect>() {
            return Some(self.intersects_rect(rect));
        }
        if let Some(triangle) = other.as_any().downcast_ref::<Triangle>() {
            return Some(self.intersects_triangle(triangle));
        }
        if let Some(polygon) = other.as_any().downcast_ref::<Polygon>() {
            return Some(self.intersects_polygon(polygon));
        }
        if let Some(circle) = other.as_any().downcast_ref::<Circle>() {
            return Some(self.intersects_circle(circle));
        }
        if let Some(ellipse) = other.as_any().downcast_ref::<Ellipse>() {
            return Some(self.intersects_ellipse(ellipse));
        }
        if let Some(shapebox) = other.as_any().downcast_ref::<ShapeBox>() {
            return Some(match shapebox {
                ShapeBox::Line(line) => self.intersects_line(line),
                ShapeBox::Rect(rect) => self.intersects_rect(rect),
                ShapeBox::Triangle(triangle) => self.intersects_triangle(triangle),
                ShapeBox::Circle(circle) => self.intersects_circle(circle),
                ShapeBox::Ellipse(ellipse) => self.intersects_ellipse(ellipse),
                ShapeBox::Polygon(polygon) => self.intersects_polygon(polygon),
            });
        }
        None
    }
}

fn new_hash_set() -> FnvHashSet<Coord> {
    FnvHashSet::default()
}

#[cfg(test)]
mod test {
    use crate::prelude::*;

    pub fn check_points(expected: &[(isize, isize)], actual: &[Coord]) {
        let mut expected: Vec<Coord> = expected.iter().map(|(x, y)| coord!(*x, *y)).collect();
        let mut unexpected = vec![];
        for point in actual {
            if let Some(i) = expected.iter().position(|p| p == point) {
                expected.remove(i);
            } else {
                unexpected.push(point);
            }
        }
        let mut message = String::new();
        if !expected.is_empty() {
            message.push_str(&format!("Points not found: {:?}", expected));
        }
        if !unexpected.is_empty() {
            message.push_str(&format!("Points unexpectedly found: {:?}", unexpected));
        }
        if !message.is_empty() {
            panic!("{message}");
        }
    }

    #[test]
    fn generic_contains() {
        let outer = Rect::new((0, 0), (10, 10));
        let inner = Line::new((2, 2), (4, 4));

        assert!(outer.contains_line(&inner));
        assert_eq!(outer.contains_shape(&inner), Some(true));

        let outside = Line::new((-3, 200), (-1, -1));
        assert!(!outer.contains_line(&outside));
        assert_eq!(outer.contains_shape(&outside), Some(false));
    }

    #[test]
    fn shapebox_intersects() {
        let line = Line::new((10, 10), (20, 20));
        let rect = Rect::new((5, 5), (15, 15));
        let shape_box = rect.to_shape_box();
        assert_eq!(line.intersects_shape(&rect), Some(true));
        assert_eq!(line.intersects_shape(&shape_box), Some(true));
    }
}