1use crate::{Circle, PathEl, Point, Rect, Shape, Vec2};
6
7use core::cmp::*;
8use core::f64::consts::FRAC_PI_4;
9use core::ops::{Add, Sub};
10
11#[cfg(not(feature = "std"))]
12use crate::common::FloatFuncs;
13
14#[derive(Clone, Copy, PartialEq, Debug)]
22#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
23#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
24pub struct Triangle {
25 pub a: Point,
27 pub b: Point,
29 pub c: Point,
31}
32
33impl Triangle {
34 pub const ZERO: Self = Self::from_coords((0., 0.), (0., 0.), (0., 0.));
36
37 pub const EQUILATERAL: Self = Self::from_coords(
39 (
40 1.0 / 2.0,
41 1.732050807568877293527446341505872367_f64 / 2.0, ),
43 (0.0, 0.0),
44 (1.0, 0.0),
45 );
46
47 #[inline(always)]
49 pub fn new(a: impl Into<Point>, b: impl Into<Point>, c: impl Into<Point>) -> Self {
50 Self {
51 a: a.into(),
52 b: b.into(),
53 c: c.into(),
54 }
55 }
56
57 #[inline(always)]
61 pub const fn from_coords(a: (f64, f64), b: (f64, f64), c: (f64, f64)) -> Self {
62 Self {
63 a: Point::new(a.0, a.1),
64 b: Point::new(b.0, b.1),
65 c: Point::new(c.0, c.1),
66 }
67 }
68
69 #[inline]
71 pub fn centroid(&self) -> Point {
72 (1.0 / 3.0 * (self.a.to_vec2() + self.b.to_vec2() + self.c.to_vec2())).to_point()
73 }
74
75 #[inline]
77 pub fn offsets(&self) -> [Vec2; 3] {
78 let centroid = self.centroid().to_vec2();
79
80 [
81 (self.a.to_vec2() - centroid),
82 (self.b.to_vec2() - centroid),
83 (self.c.to_vec2() - centroid),
84 ]
85 }
86
87 #[inline]
89 pub fn area(&self) -> f64 {
90 0.5 * (self.b - self.a).cross(self.c - self.a)
91 }
92
93 #[doc(alias = "is_empty")]
95 #[inline]
96 pub fn is_zero_area(&self) -> bool {
97 self.area() == 0.0
98 }
99
100 #[doc(alias = "incircle")]
104 #[inline]
105 pub fn inscribed_circle(&self) -> Circle {
106 let ab = self.a.distance(self.b);
107 let bc = self.b.distance(self.c);
108 let ac = self.a.distance(self.c);
109
110 let perimeter_recip = 1. / (ab + bc + ac);
113 let incenter = (self.a.to_vec2() * bc + self.b.to_vec2() * ac + self.c.to_vec2() * ab)
114 * perimeter_recip;
115
116 Circle::new(incenter.to_point(), 2.0 * self.area() * perimeter_recip)
117 }
118
119 #[doc(alias = "circumcircle")]
123 #[inline]
124 pub fn circumscribed_circle(&self) -> Circle {
125 let b = self.b - self.a;
126 let c = self.c - self.a;
127 let b_len2 = b.hypot2();
128 let c_len2 = c.hypot2();
129 let d_recip = 0.5 / b.cross(c);
130
131 let x = (c.y * b_len2 - b.y * c_len2) * d_recip;
132 let y = (b.x * c_len2 - c.x * b_len2) * d_recip;
133 let r = (b_len2 * c_len2).sqrt() * (c - b).hypot() * d_recip;
134
135 Circle::new(self.a + Vec2::new(x, y), r)
136 }
137
138 #[doc(alias = "offset")]
140 pub fn inflate(&self, scalar: f64) -> Self {
141 let centroid = self.centroid();
142
143 Self::new(
144 centroid + (0.0, scalar),
145 centroid + scalar * Vec2::from_angle(5.0 * FRAC_PI_4),
146 centroid + scalar * Vec2::from_angle(7.0 * FRAC_PI_4),
147 )
148 }
149
150 #[inline]
154 pub const fn is_finite(&self) -> bool {
155 self.a.is_finite() && self.b.is_finite() && self.c.is_finite()
156 }
157
158 #[inline]
162 pub const fn is_nan(&self) -> bool {
163 self.a.is_nan() || self.b.is_nan() || self.c.is_nan()
164 }
165}
166
167impl From<(Point, Point, Point)> for Triangle {
168 fn from(points: (Point, Point, Point)) -> Triangle {
169 Triangle::new(points.0, points.1, points.2)
170 }
171}
172
173impl Add<Vec2> for Triangle {
174 type Output = Triangle;
175
176 #[inline]
177 fn add(self, v: Vec2) -> Triangle {
178 Triangle::new(self.a + v, self.b + v, self.c + v)
179 }
180}
181
182impl Sub<Vec2> for Triangle {
183 type Output = Triangle;
184
185 #[inline]
186 fn sub(self, v: Vec2) -> Triangle {
187 Triangle::new(self.a - v, self.b - v, self.c - v)
188 }
189}
190
191#[doc(hidden)]
192pub struct TrianglePathIter {
193 triangle: Triangle,
194 ix: usize,
195}
196
197impl Shape for Triangle {
198 type PathElementsIter<'iter> = TrianglePathIter;
199
200 fn path_elements(&self, _tolerance: f64) -> TrianglePathIter {
201 TrianglePathIter {
202 triangle: *self,
203 ix: 0,
204 }
205 }
206
207 #[inline]
208 fn area(&self) -> f64 {
209 Triangle::area(self)
210 }
211
212 #[inline]
213 fn perimeter(&self, _accuracy: f64) -> f64 {
214 self.a.distance(self.b) + self.b.distance(self.c) + self.c.distance(self.a)
215 }
216
217 #[inline]
218 fn winding(&self, pt: Point) -> i32 {
219 let s0 = (self.b - self.a).cross(pt - self.a).signum();
220 let s1 = (self.c - self.b).cross(pt - self.b).signum();
221 let s2 = (self.a - self.c).cross(pt - self.c).signum();
222
223 if s0 == s1 && s1 == s2 { s0 as i32 } else { 0 }
224 }
225
226 #[inline]
227 fn bounding_box(&self) -> Rect {
228 Rect::new(
229 self.a.x.min(self.b.x.min(self.c.x)),
230 self.a.y.min(self.b.y.min(self.c.y)),
231 self.a.x.max(self.b.x.max(self.c.x)),
232 self.a.y.max(self.b.y.max(self.c.y)),
233 )
234 }
235}
236
237impl Iterator for TrianglePathIter {
240 type Item = PathEl;
241
242 fn next(&mut self) -> Option<PathEl> {
243 self.ix += 1;
244 match self.ix {
245 1 => Some(PathEl::MoveTo(self.triangle.a)),
246 2 => Some(PathEl::LineTo(self.triangle.b)),
247 3 => Some(PathEl::LineTo(self.triangle.c)),
248 4 => Some(PathEl::ClosePath),
249 _ => None,
250 }
251 }
252}
253
254#[cfg(test)]
256mod tests {
257 use crate::{Point, Triangle, Vec2};
258
259 fn assert_approx_eq(x: f64, y: f64, max_relative_error: f64) {
260 assert!(
261 (x - y).abs() <= f64::max(x.abs(), y.abs()) * max_relative_error,
262 "{x} != {y}"
263 );
264 }
265
266 fn assert_approx_eq_point(x: Point, y: Point, max_relative_error: f64) {
267 assert_approx_eq(x.x, y.x, max_relative_error);
268 assert_approx_eq(x.y, y.y, max_relative_error);
269 }
270
271 #[test]
272 fn centroid() {
273 let test = Triangle::from_coords((-90.02, 3.5), (7.2, -9.3), (8.0, 9.1)).centroid();
274 let expected = Point::new(-24.94, 1.1);
275
276 assert_approx_eq_point(test, expected, f64::EPSILON * 100.);
277 }
278
279 #[test]
280 fn offsets() {
281 let test = Triangle::from_coords((-20.0, 180.2), (1.2, 0.0), (290.0, 100.0)).offsets();
282 let expected = [
283 Vec2::new(-110.4, 86.8),
284 Vec2::new(-89.2, -93.4),
285 Vec2::new(199.6, 6.6),
286 ];
287
288 test.iter().zip(expected.iter()).for_each(|(t, e)| {
289 assert_approx_eq_point(t.to_point(), e.to_point(), f64::EPSILON * 100.);
290 });
291 }
292
293 #[test]
294 fn area() {
295 let test = Triangle::new(
296 (12123.423, 2382.7834),
297 (7892.729, 238.459),
298 (7820.2, 712.23),
299 );
300 let expected = 1079952.9157407999;
301
302 assert_approx_eq(test.area(), -expected, f64::EPSILON * 100.);
304 let test = Triangle::new(test.b, test.a, test.c);
306 assert_approx_eq(test.area(), expected, f64::EPSILON * 100.);
307 }
308
309 #[test]
310 fn circumcenter() {
311 let test = Triangle::EQUILATERAL.circumscribed_circle().center;
312 let expected = Point::new(0.5, 0.28867513459481288);
313
314 assert_approx_eq_point(test, expected, f64::EPSILON * 100.);
315 }
316
317 #[test]
318 fn inradius() {
319 let test = Triangle::EQUILATERAL.inscribed_circle().radius;
320 let expected = 0.28867513459481287;
321
322 assert_approx_eq(test, expected, f64::EPSILON * 100.);
323 }
324
325 #[test]
326 fn circumradius() {
327 let test = Triangle::EQUILATERAL;
328 let expected = 0.57735026918962576;
329
330 assert_approx_eq(
331 test.circumscribed_circle().radius,
332 expected,
333 f64::EPSILON * 100.,
334 );
335 let test = Triangle::new(test.b, test.a, test.c);
337 assert_approx_eq(
338 test.circumscribed_circle().radius,
339 -expected,
340 f64::EPSILON * 100.,
341 );
342 }
343
344 #[test]
345 fn inscribed_circle() {
346 let test = Triangle::new((-4., 1.), (-4., -1.), (10., 3.));
347
348 let inscribed = test.inscribed_circle();
349 assert_approx_eq_point(
350 inscribed.center,
351 (-3.0880178529263671, 0.20904207741504303).into(),
352 f64::EPSILON * 100.,
353 );
354 assert_approx_eq(inscribed.radius, 0.91198214707363295, f64::EPSILON * 100.);
355 }
356
357 #[test]
358 fn circumscribed_circle() {
359 let test = Triangle::new((-4., 1.), (-4., -1.), (10., 3.));
360
361 let circumscribed = test.circumscribed_circle();
362 assert_approx_eq_point(
363 circumscribed.center,
364 (3.2857142857142857, 0.).into(),
365 f64::EPSILON * 100.,
366 );
367 assert_approx_eq(
368 circumscribed.radius,
369 7.3540215292764288,
370 f64::EPSILON * 100.,
371 );
372 }
373}