shape_core/elements/triangles/mod.rs
1mod convert;
2mod display;
3mod indexes;
4
5use super::*;
6
7/// A triangles is a polygon with three edges and three vertices. It is one of the basic shapes in geometry.
8///
9/// # Arguments
10///
11/// * `a`:
12/// * `b`:
13/// * `c`:
14///
15/// returns: Triangle<T>
16///
17/// # Examples
18///
19/// ```
20/// # use shape_core::Triangle;
21/// ```
22#[derive(Debug, Clone)]
23pub struct Triangle<T> {
24 /// The 1st vertex of the triangle.
25 pub a: Point<T>,
26 /// The 2nd vertex of the triangle.
27 pub b: Point<T>,
28 /// The 3rd vertex of the triangle.
29 pub c: Point<T>,
30}
31
32/// Clockwise means the front side, and counterclockwise means the back side. When rendering, only the front side is rendered by default, and the back side is invisible.
33///
34/// If you need double-sided display, you need to draw the reverse side at the same time, you can call !self to get the reverse side
35#[derive(Copy, Clone)]
36pub struct TriangleIndex {
37 /// The 1st vertex in the triangle index.
38 pub a: usize,
39 /// The 2nd vertex in the triangle index.
40 pub b: usize,
41 /// The 3rd vertex in the triangle index.
42 pub c: usize,
43}
44
45impl<T> Triangle<T> {
46 /// Create a new triangle from three points.
47 pub fn new<P>(a: P, b: P, c: P) -> Self
48 where
49 P: Into<Point<T>>,
50 {
51 Self { a: a.into(), b: b.into(), c: c.into() }
52 }
53 /// Create a triangle from a mesh and a triangles index.
54 pub fn from_mesh(vertexes: &[Point<T>], index: TriangleIndex) -> Self
55 where
56 T: Clone,
57 {
58 debug_assert!(index.max() < vertexes.len(), "triangles index {index} out of range, must less than {}", vertexes.len());
59 // SAFETY: the debug_assert! above ensures that the index is in range
60 unsafe {
61 Self {
62 a: vertexes.get_unchecked(index.a).clone(),
63 b: vertexes.get_unchecked(index.b).clone(),
64 c: vertexes.get_unchecked(index.c).clone(),
65 }
66 }
67 }
68}
69
70impl<T> Triangle<T>
71where
72 T: Clone + AddAssign + Real,
73{
74 /// Returns true if the triangle is equilateral.
75 pub fn is_congruent(&self) -> bool {
76 true
77 }
78 /// Returns true if the triangle is equilateral.
79 pub fn is_isosceles(&self) -> bool {
80 true
81 }
82
83 /// Returns the perimeter of the triangle.
84 pub fn perimeter(&self) -> T {
85 let mut out = T::zero();
86 for edge in self.edges(3) {
87 out += edge.length();
88 }
89 out
90 }
91
92 /// Returns the area of the triangles.
93 pub fn area(&self) -> T {
94 // Det[{{x0, y0, 1}, {x1, y1, 1}, {x2, y2, 1}}] / 2
95 // x0 y1 - x1 y0
96 let det1 = self.a.x.clone() * self.b.y.clone() - self.b.x.clone() * self.a.y.clone();
97 // x1 y2 - x2 y1
98 let det2 = self.b.x.clone() * self.c.y.clone() - self.c.x.clone() * self.b.y.clone();
99 // x2 y0 - x0 y2
100 let det3 = self.c.x.clone() * self.a.y.clone() - self.a.x.clone() * self.c.y.clone();
101 (det1 + det2 + det3) / two()
102 }
103 /// Get the inscribed circle of the triangles
104 pub fn inscribed_circle(&self) -> Circle<T> {
105 todo!()
106 }
107 /// Get the circumscribed circle of the triangles.
108 pub fn circumscribed_circle(&self) -> Circle<T> {
109 Circle::from_3_points(&self.a, &self.b, &self.c)
110 }
111}