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
136
137
138
139
140
141
142
143
144
145
use crate::{polygon, CoordNum, Coordinate, Line, Polygon};
#[cfg(any(feature = "approx", test))]
use approx::{AbsDiffEq, RelativeEq};
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Triangle<T: CoordNum>(pub Coordinate<T>, pub Coordinate<T>, pub Coordinate<T>);
impl<T: CoordNum> Triangle<T> {
pub fn to_array(&self) -> [Coordinate<T>; 3] {
[self.0, self.1, self.2]
}
pub fn to_lines(&self) -> [Line<T>; 3] {
[
Line::new(self.0, self.1),
Line::new(self.1, self.2),
Line::new(self.2, self.0),
]
}
pub fn to_polygon(self) -> Polygon<T> {
polygon![self.0, self.1, self.2, self.0]
}
}
impl<IC: Into<Coordinate<T>> + Copy, T: CoordNum> From<[IC; 3]> for Triangle<T> {
fn from(array: [IC; 3]) -> Triangle<T> {
Triangle(array[0].into(), array[1].into(), array[2].into())
}
}
#[cfg(any(feature = "approx", test))]
impl<T> RelativeEq for Triangle<T>
where
T: AbsDiffEq<Epsilon = T> + CoordNum + RelativeEq,
{
#[inline]
fn default_max_relative() -> Self::Epsilon {
T::default_max_relative()
}
#[inline]
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
if !self.0.relative_eq(&other.0, epsilon, max_relative) {
return false;
}
if !self.1.relative_eq(&other.1, epsilon, max_relative) {
return false;
}
if !self.2.relative_eq(&other.2, epsilon, max_relative) {
return false;
}
true
}
}
#[cfg(any(feature = "approx", test))]
impl<T> AbsDiffEq for Triangle<T>
where
T: AbsDiffEq<Epsilon = T> + CoordNum,
T::Epsilon: Copy,
{
type Epsilon = T;
#[inline]
fn default_epsilon() -> Self::Epsilon {
T::default_epsilon()
}
#[inline]
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
if !self.0.abs_diff_eq(&other.0, epsilon) {
return false;
}
if !self.1.abs_diff_eq(&other.1, epsilon) {
return false;
}
if !self.2.abs_diff_eq(&other.2, epsilon) {
return false;
}
true
}
}