Skip to main content

Triangle

Struct Triangle 

Source
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§

Source§

impl Triangle

Source

pub const fn new(a: Point2, b: Point2, c: Point2) -> Self

Creates a triangle from three points.

Source

pub fn try_new(a: Point2, b: Point2, c: Point2) -> Result<Self, 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);
Source

pub const fn a(self) -> Point2

Returns the first vertex.

Source

pub const fn b(self) -> Point2

Returns the second vertex.

Source

pub const fn c(self) -> Point2

Returns the third vertex.

Source

pub const fn vertices(self) -> [Point2; 3]

Returns the triangle vertices in [a, b, c] order.

Source

pub fn twice_signed_area(self) -> f64

Returns twice the signed area of the triangle.

The sign depends on the vertex winding order.

Source

pub fn twice_area(self) -> f64

Returns twice the unsigned area of the triangle.

Source

pub fn orientation(self) -> Orientation2

Returns the triangle orientation implied by the vertex winding order.

Source

pub fn area(self) -> f64

Returns the triangle area.

Source

pub fn sides(self) -> [f64; 3]

Returns the triangle side lengths in [ab, bc, ca] order.

Source

pub fn perimeter(self) -> f64

Returns the triangle perimeter.

Source

pub fn centroid(self) -> Point2

Returns the triangle centroid.

Source

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.

Source

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.

Source

pub const fn aabb(self) -> Aabb2

Returns the triangle bounding box.

Trait Implementations§

Source§

impl Clone for Triangle

Source§

fn clone(&self) -> Triangle

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Triangle

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Triangle

Source§

fn eq(&self, other: &Triangle) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Triangle

Source§

impl StructuralPartialEq for Triangle

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.