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
use super::*;
use crate::utils::{max3, min3};
use std::vec::IntoIter;

impl<T, P> TryFrom<(P, P, P)> for Triangle<T>
where
    Point<T>: From<P>,
    T: Num + Clone + PartialOrd,
{
    type Error = ();

    fn try_from(value: (P, P, P)) -> Result<Self, Self::Error> {
        let tri = Self { a: value.0.into(), b: value.1.into(), c: value.2.into() };
        match tri.is_valid() {
            true => Ok(tri),
            false => Err(()),
        }
    }
}

impl<T> TryFrom<&Polygon<T>> for Triangle<T>
where
    T: Clone,
{
    type Error = ();

    fn try_from(value: &Polygon<T>) -> Result<Self, Self::Error> {
        match value.points_set.points.as_slice() {
            [a, b, c] => Ok(Triangle { a: a.clone(), b: b.clone(), c: c.clone() }),
            _ => Err(()),
        }
    }
}

impl<T> Shape2D for Triangle<T>
where
    T: Clone + PartialOrd + Num,
{
    type Value = T;
    type VertexIterator<'a>

    = IntoIter<Point<T>>where
        T: 'a;
    type LineIterator<'a>

    = IntoIter<Line<T>>where
        T: 'a;

    fn is_valid(&self) -> bool {
        let a = Vector::from_2_points(self.a.clone(), self.b.clone());
        let b = Vector::from_2_points(self.b.clone(), self.c.clone());
        a.dx * b.dy < a.dy * b.dx
    }

    fn boundary(&self) -> Rectangle<Self::Value> {
        let min_x = min3(&self.a.x, &self.b.x, &self.c.x).clone();
        let min_y = min3(&self.a.y, &self.b.y, &self.c.y).clone();
        let max_x = max3(&self.a.x, &self.b.x, &self.c.x).clone();
        let max_y = max3(&self.a.y, &self.b.y, &self.c.y).clone();
        Rectangle::from_min_max(Point::new(min_x, min_y), Point::new(max_x, max_y))
    }

    fn vertices<'a>(&'a self, _: usize) -> Self::VertexIterator<'a> {
        vec![self.a.clone(), self.b.clone(), self.c.clone()].into_iter()
    }

    fn edges<'a>(&'a self, _: usize) -> Self::LineIterator<'a> {
        vec![
            Line::new(self.a.clone(), self.b.clone()),
            Line::new(self.b.clone(), self.c.clone()),
            Line::new(self.c.clone(), self.a.clone()),
        ]
        .into_iter()
    }
}