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
//! Shapes for Graphics
//!
//! Provides shapes for simple graphics
//!
//! ```
//! # use graphics_shapes::coord::Coord;
//! # use graphics_shapes::rect::Rect;
//! # use graphics_shapes::Shape;
//! # use graphics_shapes::triangle::Triangle;
//! let rect = Rect::new((10,10),(20,20));
//! assert!(rect.contains((15,15)));
//! let triangle = Triangle::new((34,5),(12,30),(9,10));
//! let rotated = triangle.rotate(45);
//!
//! let start = Coord::new(20,130);
//! let dist = start.distance((30,130));
//!```

#![deny(clippy::all)]
#![forbid(unsafe_code)]

use crate::coord::Coord;
use crate::general_math::{rotate_points, scale_points};
use fnv::FnvHashSet;

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

pub mod prelude {
    pub use crate::circle::Circle;
    pub use crate::coord::*;
    pub use crate::ellipse::Ellipse;
    pub use crate::line::Line;
    pub use crate::polygon::Polygon;
    pub use crate::rect::Rect;
    pub use crate::triangle::Triangle;
    pub use crate::Shape;
}

pub trait Shape {
    /// 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;

    /// change every point by +`delta`
    #[must_use]
    fn translate_by<P: Into<Coord>>(&self, delta: P) -> Self
    where
        Self: Sized,
    {
        let delta = delta.into();
        let points: Vec<Coord> = self.points().iter().map(|p| *p + delta).collect();
        Self::from_points(&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<P: Into<Coord>>(&self, point: P) -> Self
    where
        Self: Sized,
    {
        let diff = (point.into()) - 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<P: Into<Coord>>(&self, point: P) -> Self
    where
        Self: Sized,
    {
        let diff = (point.into()) - (self.center() - self.points()[0]);
        self.translate_by(diff)
    }

    /// returns true if the shape contains point
    #[must_use]
    fn contains<P: Into<Coord>>(&self, point: P) -> 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<P: Into<Coord>>(&self, degrees: isize, point: P) -> Self
    where
        Self: Sized,
    {
        let points = rotate_points(point.into(), &self.points(), degrees);
        Self::from_points(&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()
    }

    /// 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<P: Into<Coord>>(&self, factor: f32, point: P) -> Self
    where
        Self: Sized,
    {
        let points = scale_points(point.into(), &self.points(), factor);
        Self::from_points(&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>;
}

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

// pub trait Intersects<T> {
//     /// returns true if `shape` intersects this shape
//     #[must_use]
//     fn intersects(&self, shape: T) -> bool;
// }

#[cfg(test)]
mod test {
    use crate::coord::Coord;

    pub fn check_points(expected: &[(isize, isize)], actual: &[Coord]) {
        let mut expected: Vec<Coord> = expected
            .iter()
            .map(|(x, y)| Coord::new(*x as isize, *y as isize))
            .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}");
        }
    }
}