#[repr(C)]pub struct Triangle {
pub a: Point3,
pub b: Point3,
pub c: Point3,
}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: Point3First vertex of the triangle
b: Point3Second vertex of the triangle
c: Point3Third vertex of the triangle
Implementations§
Source§impl Triangle
impl Triangle
Sourcepub fn new(
a: impl Into<[Real; 3]>,
b: impl Into<[Real; 3]>,
c: impl Into<[Real; 3]>,
) -> Self
pub fn new( a: impl Into<[Real; 3]>, b: impl Into<[Real; 3]>, c: impl Into<[Real; 3]>, ) -> Self
Creates a new triangle from three vertices.
§Arguments
a- First vertexb- Second vertexc- 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),
);Sourcepub fn center(&self) -> Point3
pub fn center(&self) -> Point3
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));Sourcepub fn normal(&self) -> UnitVec3
pub fn normal(&self) -> UnitVec3
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 vectorSourcepub fn area(&self) -> Real
pub fn area(&self) -> Real
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 2Sourcepub fn is_degenerate(&self) -> bool
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 ComputeAabb3 for Triangle
impl ComputeAabb3 for Triangle
Source§fn compute_aabb(&self) -> Aabb3
fn compute_aabb(&self) -> Aabb3
Source§impl<'de> Deserialize<'de> for Triangle
impl<'de> Deserialize<'de> for Triangle
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Copy for Triangle
impl StructuralPartialEq for Triangle
Auto Trait Implementations§
impl Freeze for Triangle
impl RefUnwindSafe for Triangle
impl Send for Triangle
impl Sync for Triangle
impl Unpin for Triangle
impl UnwindSafe for Triangle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.