Skip to main content

Triangle

Struct Triangle 

Source
#[repr(C)]
pub struct Triangle { pub a: OPoint<f64, Const<3>>, pub b: OPoint<f64, Const<3>>, pub c: OPoint<f64, Const<3>>, }
Expand description

A triangle defined by three vertices in 3D space.

A triangle is the simplest polygon in 3D space, consisting of three vertices connected by three edges. It’s a fundamental building block in computer graphics, computational geometry, and physics simulations.

The triangle is defined by three vertices A, B, and C. The orientation and properties are computed based on the relative positions of these vertices.

§Examples

use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

// Create a triangle
let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);

§Mathematical Properties

  • Area = 0.5 * |(B-A) × (C-A)|
  • Normal = normalize((B-A) × (C-A))
  • Center = (A + B + C) / 3

Fields§

§a: OPoint<f64, Const<3>>

First vertex of the triangle

§b: OPoint<f64, Const<3>>

Second vertex of the triangle

§c: OPoint<f64, Const<3>>

Third vertex of the triangle

Implementations§

Source§

impl Triangle

Source

pub fn new( a: impl Into<[f64; 3]>, b: impl Into<[f64; 3]>, c: impl Into<[f64; 3]>, ) -> Triangle

Creates a new triangle from three vertices.

§Arguments
  • a - First vertex
  • b - Second vertex
  • c - Third vertex
§Returns

A new Triangle instance.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);
Source

pub fn center(&self) -> OPoint<f64, Const<3>>

Computes the geometric center (centroid) of the triangle.

The centroid is calculated as the average of all three vertices: C = (A + B + C) / 3

§Returns

The centroid coordinates as a Point3.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(2.0, 0.0, 0.0),
    Point3::new(0.0, 2.0, 0.0),
);

let center = triangle.center();
assert_eq!(center, Point3::new(2.0/3.0, 2.0/3.0, 0.0));
Source

pub fn normal( &self, ) -> Unit<Matrix<f64, Const<3>, Const<1>, ArrayStorage<f64, 3, 1>>>

Computes the normal vector of the triangle.

The normal is calculated using the cross product of two edges: N = normalize((B-A) × (C-A))

The direction follows the right-hand rule based on the vertex order (A, B, C).

§Returns

The unit normal vector as a UnitVec3.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::{Point3, UnitVec3};

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);

let normal = triangle.normal();
// Triangle lies in the XY plane, normal points in +Z direction
assert!(normal.z > 0.9); // Close to 1.0 for unit vector
Source

pub fn area(&self) -> f64

Computes the area of the triangle.

The area is calculated using half the magnitude of the cross product: Area = 0.5 * |(B-A) × (C-A)|

§Returns

The area as a Real value.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

let triangle = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(2.0, 0.0, 0.0),
    Point3::new(0.0, 2.0, 0.0),
);

let area = triangle.area();
assert_eq!(area, 2.0); // Right triangle with legs of length 2
Source

pub fn is_degenerate(&self) -> bool

Checks if the triangle is degenerate (has zero area).

A triangle is degenerate if all three vertices are collinear, which means the cross product of two edges has zero (or near-zero) magnitude.

§Returns

true if the triangle is degenerate, false otherwise.

§Examples
use phys_geom::shape::Triangle;
use phys_geom::math::Point3;

// Non-degenerate triangle
let triangle1 = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(0.0, 1.0, 0.0),
);
assert!(!triangle1.is_degenerate());

// Degenerate triangle (all points on a line)
let triangle2 = Triangle::new(
    Point3::new(0.0, 0.0, 0.0),
    Point3::new(1.0, 0.0, 0.0),
    Point3::new(2.0, 0.0, 0.0),
);
assert!(triangle2.is_degenerate());

Trait Implementations§

Source§

impl Clone for Triangle

Source§

fn clone(&self) -> Triangle

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl ComputeAabb3 for Triangle

Source§

fn compute_aabb(&self) -> Aabb3

Compute the AABB in local space.
Source§

impl Copy for Triangle

Source§

impl Debug for Triangle

Source§

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

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

impl Default for Triangle

Source§

fn default() -> Triangle

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Triangle

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.