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
mod convert;

use super::*;

#[derive(Debug, Clone)]
pub struct Triangle<T> {
    pub vertex: [Point<T>; 3],
}

impl<T> Triangle<T> {
    pub fn new<P>(a: P, b: P, c: P) -> Self
    where
        Point<T>: From<P>,
    {
        Self { vertex: [a.into(), b.into(), c.into()] }
    }
}

impl<T> Triangle<T>
where
    T: Clone + Real,
{
    pub fn is_valid(&self) -> bool {
        let (ab, ac, _) = self.edges();
        ab.is_parallel(&ac)
    }
    pub fn is_congruent(&self) -> bool {
        true
    }
    pub fn is_isosceles(&self) -> bool {
        true
    }
    pub fn perimeter(&self) -> T {
        todo!()
    }

    /// Returns the area of the triangle.
    pub fn area(&self) -> T {
        // Det[{{x0, y0, 1}, {x1, y1, 1}, {x2, y2, 1}}] / 2
        // x0 y1 - x1 y0
        let det1 = self.vertex[0].x.clone() * self.vertex[1].y.clone() - self.vertex[1].x.clone() * self.vertex[0].y.clone();
        // x1 y2 - x2 y1
        let det2 = self.vertex[1].x.clone() * self.vertex[2].y.clone() - self.vertex[2].x.clone() * self.vertex[1].y.clone();
        // x2 y0 - x0 y2
        let det3 = self.vertex[2].x.clone() * self.vertex[0].y.clone() - self.vertex[0].x.clone() * self.vertex[2].y.clone();
        (det1 + det2 + det3) / two()
    }
    /// Get the inscribed circle of the triangle
    pub fn inscribed_circle(&self) -> Circle<T> {
        todo!()
    }
    /// Get the circumscribed circle of the triangle.
    pub fn circumscribed_circle(&self) -> Circle<T> {
        Circle::from_3_points(&self.vertex[0], &self.vertex[1], &self.vertex[2])
    }
    pub fn edges(&self) -> (Line<T>, Line<T>, Line<T>) {
        let ab = Line::new(&self.vertex[0], &self.vertex[1]);
        let ac = Line::new(&self.vertex[0], &self.vertex[2]);
        let bc = Line::new(&self.vertex[1], &self.vertex[2]);
        (ab, ac, bc)
    }
}