mod convert;
mod display;
mod indexes;
use super::*;
#[derive(Debug, Clone)]
pub struct Triangle<T> {
    pub a: Point<T>,
    pub b: Point<T>,
    pub c: Point<T>,
}
#[derive(Copy, Clone)]
pub struct TriangleIndex {
    pub a: usize,
    pub b: usize,
    pub c: usize,
}
impl<T> Triangle<T> {
    pub fn new<P>(a: P, b: P, c: P) -> Self
    where
        Point<T>: From<P>,
    {
        Self { a: a.into(), b: b.into(), c: c.into() }
    }
    pub fn from_mesh(vertexes: &[Point<T>], index: TriangleIndex) -> Self
    where
        T: Clone,
    {
        debug_assert!(index.max() < vertexes.len(), "triangle index {index} out of range, must less than {}", vertexes.len());
        unsafe {
            Self {
                a: vertexes.get_unchecked(index.a).clone(),
                b: vertexes.get_unchecked(index.b).clone(),
                c: vertexes.get_unchecked(index.c).clone(),
            }
        }
    }
}
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!()
    }
    pub fn area(&self) -> T {
        let det1 = self.a.x.clone() * self.b.y.clone() - self.b.x.clone() * self.a.y.clone();
        let det2 = self.b.x.clone() * self.c.y.clone() - self.c.x.clone() * self.b.y.clone();
        let det3 = self.c.x.clone() * self.a.y.clone() - self.a.x.clone() * self.c.y.clone();
        (det1 + det2 + det3) / two()
    }
    pub fn inscribed_circle(&self) -> Circle<T> {
        todo!()
    }
    pub fn circumscribed_circle(&self) -> Circle<T> {
        Circle::from_3_points(&self.a, &self.b, &self.c)
    }
    pub fn edges(&self) -> (Line<T>, Line<T>, Line<T>) {
        let ab = Line::new(&self.a, &self.b);
        let ac = Line::new(&self.a, &self.c);
        let bc = Line::new(&self.b, &self.c);
        (ab, ac, bc)
    }
}