Struct Triangle2
A triangle in 2D space defined by three vertices A, B, and C.
Triangle2 is a readonly record struct: value-type, immutable,
and comparable by value. All geometric properties (area, perimeter, centroid, sides) are
computed from the three vertices.
Triangles are the fundamental building block of 2D geometry. You can use them to test point containment, compute signed areas, or approximate curved shapes as polygon fans. The vertex order determines orientation: counter-clockwise (CCW) is the positive convention, matching standard mathematical coordinates where Y increases upward.
public readonly record struct Triangle2 : IEquatable<Triangle2>
- Implements
- Inherited Members
- Extension Methods
Examples
var tri = new Triangle2(
LengthPoint2.FromMeters(0, 0),
LengthPoint2.FromMeters(6, 0),
LengthPoint2.FromMeters(3, 6));
var center = tri.Center; // centroid at (3, 2)
var area = tri.Area; // 18 m²
tri.Contains(LengthPoint2.FromMeters(3, 2)); // => true
Constructors
Triangle2(LengthPoint2, LengthPoint2, LengthPoint2)
A triangle in 2D space defined by three vertices A, B, and C.
Triangle2 is a readonly record struct: value-type, immutable,
and comparable by value. All geometric properties (area, perimeter, centroid, sides) are
computed from the three vertices.
Triangles are the fundamental building block of 2D geometry. You can use them to test point containment, compute signed areas, or approximate curved shapes as polygon fans. The vertex order determines orientation: counter-clockwise (CCW) is the positive convention, matching standard mathematical coordinates where Y increases upward.
public Triangle2(LengthPoint2 A, LengthPoint2 B, LengthPoint2 C)
Parameters
Examples
var tri = new Triangle2(
LengthPoint2.FromMeters(0, 0),
LengthPoint2.FromMeters(6, 0),
LengthPoint2.FromMeters(3, 6));
var center = tri.Center; // centroid at (3, 2)
var area = tri.Area; // 18 m²
tri.Contains(LengthPoint2.FromMeters(3, 2)); // => true
Properties
A
public LengthPoint2 A { get; init; }
Property Value
AB
The side from vertex A to vertex B.
public LengthSegment2 AB { get; }
Property Value
Area
The area of the triangle, computed as half the absolute value of the cross product of vectors AB and AC.
Formula: Area = ½ × |AB × AC|
This formula works for any triangle regardless of orientation. If the vertices are counter-clockwise (CCW), the cross product is positive; clockwise gives a negative cross product. The absolute value handles both cases.
public Area Area { get; }
Property Value
- Area
Examples
var t = new Triangle2(
LengthPoint2.FromMeters(0, 0),
LengthPoint2.FromMeters(6, 0),
LengthPoint2.FromMeters(3, 6));
t.Area.SquareMeters; // => 18.0
B
public LengthPoint2 B { get; init; }
Property Value
BC
The side from vertex B to vertex C.
public LengthSegment2 BC { get; }
Property Value
C
public LengthPoint2 C { get; init; }
Property Value
CA
The side from vertex C to vertex A.
public LengthSegment2 CA { get; }
Property Value
Center
The centroid (geometric centre) of the triangle: the arithmetic mean of the three vertices.
The centroid is the point where all three medians of a triangle intersect. It is always located inside the triangle (one-third of the way from each side to the opposite vertex).
public LengthPoint2 Center { get; }
Property Value
Examples
var t = new Triangle2(
LengthPoint2.FromMeters(0, 0),
LengthPoint2.FromMeters(6, 0),
LengthPoint2.FromMeters(3, 6));
var center = t.Center; // centroid (3, 2)
IsDegenerate
Returns true if the three vertices are collinear (area is zero). A degenerate triangle has no interior and represents a line segment, not a surface.
public bool IsDegenerate { get; }
Property Value
Perimeter
The perimeter of the triangle (sum of all three side lengths).
public Length Perimeter { get; }
Property Value
- Length
Methods
Contains(LengthPoint2)
Returns true if point lies inside or on the boundary
of this triangle.
Algorithm — sign-of-cross (same-side test): point P is inside triangle ABC if and only if it is on the same side of each directed edge as the opposite vertex. Equivalently, all three cross products (AB × AP), (BC × BP), and (CA × CP) must have the same sign (all non-negative or all non-positive).
Points on edges or at vertices satisfy at least one cross product equal to zero, which passes the test when all non-zero values share the same sign.
public bool Contains(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space point to test.
Returns
Examples
var t = new Triangle2(
LengthPoint2.FromMeters(0, 0),
LengthPoint2.FromMeters(6, 0),
LengthPoint2.FromMeters(3, 6));
t.Contains(LengthPoint2.FromMeters(3, 2)); // => true
t.Contains(LengthPoint2.FromMeters(0, 5)); // => false
t.Contains(LengthPoint2.FromMeters(3, 0)); // => true
DistanceTo(LengthPoint2)
Returns the distance from this shape to point.
Returns UnitsNet.Length.Zero if the point is inside or on the boundary.
public Length DistanceTo(LengthPoint2 point)
Parameters
pointLengthPoint2The point to measure distance to.
Returns
- Length
The shortest distance from the shape's boundary to
point, or UnitsNet.Length.Zero if the point is contained.
Examples
Length d = shape.DistanceTo(LengthPoint2.FromMeters(10, 0));
GetBoundingRectangle()
Returns the smallest axis-aligned bounding rectangle that completely contains this triangle.
public Rectangle2 GetBoundingRectangle()
Returns
- Rectangle2
An axis-aligned Rectangle2 bounding all three vertices.
Examples
var t = new Triangle2(
LengthPoint2.FromMeters(1, 1),
LengthPoint2.FromMeters(5, 1),
LengthPoint2.FromMeters(3, 4));
var bounds = t.GetBoundingRectangle();
// bounds.Center = (3, 2.5), bounds.Dimensions = (4, 3)
GetClosestPoint(LengthPoint2)
Returns the point on the triangle's boundary (or interior) nearest to point.
If the point is inside the triangle it is returned unchanged. Otherwise the closest
point on any of the three edges is returned.
public LengthPoint2 GetClosestPoint(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space query point.
Returns
- LengthPoint2
The nearest point on or inside the triangle.
Examples
var tri = new Triangle2(
LengthPoint2.FromMeters(0, 0),
LengthPoint2.FromMeters(6, 0),
LengthPoint2.FromMeters(3, 6));
LengthPoint2 p = tri.GetClosestPoint(LengthPoint2.FromMeters(3, -2)); // (3, 0)
MoveCenterTo(LengthPoint2)
Returns a new Triangle2 with its centre moved to
newCenter, preserving all other properties.
public Triangle2 MoveCenterTo(LengthPoint2 newCenter)
Parameters
newCenterLengthPoint2The new centre position.
Returns
Examples
var moved = shape.MoveCenterTo(LengthPoint2.FromMeters(5, 0));
ToString()
Returns the fully qualified type name of this instance.
public override string ToString()
Returns
- string
The fully qualified type name.
TryGetManifold(Polygon2, out CollisionManifold)
Tests whether this triangle overlaps other and, if so, returns the
collision manifold describing the penetration.
public bool TryGetManifold(Polygon2 other, out CollisionManifold manifold)
Parameters
otherPolygon2The polygon to test against.
manifoldCollisionManifoldWhen this method returns true, contains the collision data.
Returns
Exceptions
- InvalidOperationException
Thrown when
otheris not convex. Use IsConvex to check before calling.
TryGetManifold(Triangle2, out CollisionManifold)
Tests whether this triangle overlaps other and, if so, returns the
collision manifold describing the penetration.
public bool TryGetManifold(Triangle2 other, out CollisionManifold manifold)
Parameters
otherTriangle2The other triangle to test against.
manifoldCollisionManifoldWhen this method returns true, contains the collision data: penetration depth, contact normal (pointing from
othertoward this triangle), and the contact point. Otherwise the default value.
Returns
- bool
true if the triangles penetrate (overlap strictly greater than zero); false if they are separated or only touching.
Operators
operator +(Triangle2, LengthVector2)
Translates all three vertices by the given displacement vector.
public static Triangle2 operator +(Triangle2 triangle, LengthVector2 displacement)
Parameters
triangleTriangle2The triangle to translate.
displacementLengthVector2The displacement to add to each vertex.
Returns
operator +(LengthVector2, Triangle2)
Translates all vertices (commutative overload).
public static Triangle2 operator +(LengthVector2 displacement, Triangle2 triangle)
Parameters
displacementLengthVector2The displacement to add.
triangleTriangle2The triangle to translate.