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
use crate::rect::Rect;
use crate::{Coord, Shape};
#[cfg(feature = "serde_derive")]
use serde::{Deserialize, Serialize};

#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)]
pub enum TriangleAngleType {
    Acute,
    Right,
    Obtuse,
    Equiangular,
    Other,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)]
pub enum TriangleSideType {
    Isosceles,
    Scalene,
    Equilateral,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Triangle {
    points: [Coord; 3],
    angles: [isize; 3],
    angle_type: TriangleAngleType,
    side_type: TriangleSideType,
    center: Coord,
}

impl Triangle {
    pub fn new<P1: Into<Coord>, P2: Into<Coord>, P3: Into<Coord>>(
        point1: P1,
        point2: P2,
        point3: P3,
    ) -> Self {
        let points = [point1.into(), point2.into(), point3.into()];
        let angles = [
            points[0].angle_to(points[1]),
            points[1].angle_to(points[2]),
            points[2].angle_to(points[0]),
        ];
        let angle_type = if angles.iter().any(|a| a.abs() == 90) {
            TriangleAngleType::Right
        } else if angles.iter().all(|a| a.abs() < 90) {
            TriangleAngleType::Acute
        } else if angles[0].abs() == angles[1].abs() && angles[1].abs() == angles[2].abs() {
            TriangleAngleType::Equiangular
        } else if angles.iter().any(|a| a.abs() > 90) {
            TriangleAngleType::Obtuse
        } else {
            TriangleAngleType::Other
        };
        let side_type = if points[0].distance(points[1]) == points[1].distance(points[2])
            && points[2].distance(points[0]) == points[1].distance(points[2])
        {
            TriangleSideType::Equilateral
        } else if points[0].distance(points[1]) == points[1].distance(points[2])
            || points[2].distance(points[0]) == points[1].distance(points[2])
            || points[2].distance(points[0]) == points[0].distance(points[1])
        {
            TriangleSideType::Isosceles
        } else {
            TriangleSideType::Scalene
        };
        let mut triangle = Self {
            points,
            angles,
            angle_type,
            side_type,
            center: Coord::new(0, 0),
        };
        triangle.center = Coord::new(triangle.left(), triangle.top())
            .mid_point(Coord::new(triangle.right(), triangle.bottom()));
        triangle
    }
}

impl Triangle {
    #[inline]
    pub fn angles(&self) -> [isize; 3] {
        self.angles
    }
    #[inline]
    pub fn angle_type(&self) -> &TriangleAngleType {
        &self.angle_type
    }
    #[inline]
    pub fn side_type(&self) -> &TriangleSideType {
        &self.side_type
    }
}

impl Shape for Triangle {
    fn from_points(points: Vec<Coord>) -> Self
    where
        Self: Sized,
    {
        Triangle::new(points[0], points[1], points[2])
    }

    fn contains<P: Into<Coord>>(&self, point: P) -> bool {
        let point = point.into();
        let p1 = Coord::new(
            self.points[1].x - self.points[0].x,
            self.points[1].y - self.points[0].y,
        );
        let p2 = Coord::new(
            self.points[2].x - self.points[0].x,
            self.points[2].y - self.points[0].y,
        );
        let q = Coord::new(point.x - self.points[0].x, point.y - self.points[0].y);

        let s = q.cross_product(p2) as f32 / p1.cross_product(p2) as f32;
        let t = p1.cross_product(q) as f32 / p1.cross_product(p2) as f32;

        s >= 0.0 && t >= 0.0 && (s + t) <= 1.0
    }

    fn points(&self) -> Vec<Coord> {
        self.points.to_vec()
    }

    #[inline]
    fn center(&self) -> Coord {
        self.center
    }
}

impl Triangle {
    pub fn as_rect(&self) -> Rect {
        Rect::new((self.left(), self.top()), (self.right(), self.bottom()))
    }
}