From 3ea35f18d6dbc354a6fead0de96c0e389f157fd6 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Thu, 17 Mar 2022 15:17:29 +0100 Subject: [PATCH] Add `Triangle::from_points` --- fj-math/src/triangle.rs | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/fj-math/src/triangle.rs b/fj-math/src/triangle.rs index 9805453ee..8024c6b9d 100644 --- a/fj-math/src/triangle.rs +++ b/fj-math/src/triangle.rs @@ -11,6 +11,28 @@ pub struct Triangle { } impl Triangle { + /// Construct a triangle from three points + /// + /// # Panics + /// + /// Panics, if the points don't form a triangle. + pub fn from_points(points: [Point; 3]) -> Self { + let area = { + let [a, b, c] = points.map(Point::to_xyz); + (b - a).cross(&(c - a)).magnitude() + }; + + // A triangle is not valid if it doesn't span any area + if area != Scalar::from(0.0) { + Self { + points, + color: [255, 0, 0, 255], + } + } else { + panic!("Invalid Triangle specified"); + } + } + /// Access the triangle's points pub fn points(&self) -> [Point; 3] { self.points @@ -36,20 +58,7 @@ impl Triangle<3> { impl From<[Point; 3]> for Triangle { fn from(points: [Point; 3]) -> Self { - let area = { - let [a, b, c] = points.map(Point::to_xyz); - (b - a).cross(&(c - a)).magnitude() - }; - - // A triangle is not valid if it doesn't span any area - if area != Scalar::from(0.0) { - Self { - points, - color: [255, 0, 0, 255], - } - } else { - panic!("Invalid Triangle specified"); - } + Self::from_points(points) } }