Struct Triangle
pub struct Triangle { /* private fields */ }Expand description
A constructed 2D triangle represented by three vertices.
Use Triangle::try_new when the vertices come from user input, files,
or other external sources. Use Triangle::new when the points are
already trusted.
§Examples
use use_geometry::{
Orientation2, Point2, Triangle, triangle_area,
triangle_twice_signed_area,
};
let a = Point2::try_new(0.0, 0.0)?;
let b = Point2::try_new(4.0, 0.0)?;
let c = Point2::try_new(0.0, 3.0)?;
let triangle = Triangle::try_new(a, b, c)?;
assert_eq!(triangle.orientation(), Orientation2::CounterClockwise);
assert_eq!(triangle.twice_signed_area(), triangle_twice_signed_area(a, b, c));
assert_eq!(triangle.area(), triangle_area(a, b, c));
assert_eq!(triangle.sides(), [4.0, 5.0, 3.0]);Implementations§
§impl Triangle
impl Triangle
pub fn try_new(
a: Point2,
b: Point2,
c: Point2,
) -> Result<Triangle, GeometryError>
pub fn try_new( a: Point2, b: Point2, c: Point2, ) -> Result<Triangle, GeometryError>
Creates a triangle from three points with finite coordinates.
Use this constructor at API boundaries where coordinates may still need
validation. Triangle::new remains available for already-validated
points.
§Errors
Returns GeometryError::NonFiniteComponent when any vertex contains a
non-finite coordinate.
§Examples
use use_geometry::{Point2, Triangle};
let triangle = Triangle::try_new(
Point2::try_new(0.0, 0.0)?,
Point2::try_new(4.0, 0.0)?,
Point2::try_new(0.0, 3.0)?,
)?;
assert_eq!(triangle.area(), 6.0);pub fn twice_signed_area(self) -> f64
pub fn twice_signed_area(self) -> f64
Returns twice the signed area of the triangle.
The sign depends on the vertex winding order.
pub fn twice_area(self) -> f64
pub fn twice_area(self) -> f64
Returns twice the unsigned area of the triangle.
pub fn orientation(self) -> Orientation2
pub fn orientation(self) -> Orientation2
Returns the triangle orientation implied by the vertex winding order.
pub fn is_degenerate(self) -> bool
pub fn is_degenerate(self) -> bool
Returns true when the triangle is exactly degenerate.
Exact degeneracy means the signed twice-area is exactly zero, which in turn means the vertices are collinear.
pub fn is_degenerate_with_tolerance(
self,
tolerance: f64,
) -> Result<bool, GeometryError>
pub fn is_degenerate_with_tolerance( self, tolerance: f64, ) -> Result<bool, GeometryError>
Returns true when the triangle’s unsigned twice-area is within tolerance of zero.
Use this when you care about practical collapse in measured or generated geometry rather than exact arithmetic collapse.
§Errors
Returns GeometryError::NonFiniteTolerance when tolerance is NaN
or infinite.
Returns GeometryError::NegativeTolerance when tolerance is negative.