Table of Contents

Class Polygon2

Namespace
Thunder.UnitsNET.Vectors.Geometry
Assembly
Thunder.UnitsNET.Vectors.Geometry.dll

An arbitrary polygon in 2D space, defined by an ordered list of vertices.

A polygon is a closed shape formed by connecting each vertex to the next with a straight line, and connecting the last vertex back to the first. The vertices can form any shape: convex (like a square or regular hexagon) or concave (like an L-shape or star).

All geometric properties — Area, Perimeter, Centroid, and IsConvex — are computed once during construction and cached.

Polygon2 is an immutable class (not a struct) because it stores a variable-length vertex list.

public sealed class Polygon2
Inheritance
Polygon2
Inherited Members
Extension Methods

Examples

// Unit square
var square = new Polygon2(new[]
{
    LengthPoint2.FromMeters(0, 0),
    LengthPoint2.FromMeters(1, 0),
    LengthPoint2.FromMeters(1, 1),
    LengthPoint2.FromMeters(0, 1),
});

Area   area     = square.Area;      // 1 m²
Length perim    = square.Perimeter; // 4 m
bool   isConvex = square.IsConvex;  // true
bool   inside   = square.Contains(LengthPoint2.FromMeters(0.5, 0.5)); // true

// Regular hexagon with circumradius 1 m
var hex = Polygon2.CreateRegular(LengthPoint2.Origin, Length.FromMeters(1), 6);

Constructors

Polygon2(IEnumerable<LengthPoint2>)

Constructs a Polygon2 from an ordered sequence of vertices.

public Polygon2(IEnumerable<LengthPoint2> vertices)

Parameters

vertices IEnumerable<LengthPoint2>

The ordered vertices of the polygon. The polygon is closed automatically (last vertex → first vertex). Must contain at least 3 vertices.

Examples

var triangle = new Polygon2(new[]
{
    LengthPoint2.FromMeters(0, 0),
    LengthPoint2.FromMeters(3, 0),
    LengthPoint2.FromMeters(1.5, 2.6),
});

Exceptions

ArgumentException

Thrown when fewer than 3 vertices are provided.

Properties

Area

The area enclosed by the polygon, computed using the Shoelace formula (also known as Gauss's area formula).

The shoelace formula works for any simple (non-self-intersecting) polygon, regardless of whether it is convex or concave: Area = ½ · |Σᵢ (xᵢ·yᵢ₊₁ − xᵢ₊₁·yᵢ)|

public Area Area { get; }

Property Value

Area

Center

The geometric centre of this polygon (alias for Centroid).

Exposes the centroid under the standardised Center name shared by all shape types, enabling uniform access and source-generator contracts.

public LengthPoint2 Center { get; }

Property Value

LengthPoint2

Examples

var poly = new Polygon2(new[] {
    LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(2, 0),
    LengthPoint2.FromMeters(2, 2), LengthPoint2.FromMeters(0, 2)
});
LengthPoint2 c = poly.Center; // (1, 1) — same as poly.Centroid

Centroid

The centroid (geometric centre of mass) of the polygon.

The centroid is computed from the polygon's area and vertices using the standard polygon centroid formula. For a convex polygon this is always inside the shape; for a concave polygon it may lie outside the boundary.

public LengthPoint2 Centroid { get; }

Property Value

LengthPoint2

IsConvex

true if the polygon is convex: every interior angle is ≤ 180° and no vertex "dents inward".

Determined by checking that the cross product of each consecutive pair of edges all have the same sign. If any cross product has the opposite sign, the polygon is concave.

Examples: squares, triangles, and regular polygons are always convex. L-shapes and stars are concave.

public bool IsConvex { get; }

Property Value

bool

Perimeter

The total length of the polygon's boundary: the sum of all side lengths.

public Length Perimeter { get; }

Property Value

Length

Vertices

The ordered list of vertices defining this polygon. The polygon is implicitly closed — the last vertex connects back to the first.

public IReadOnlyList<LengthPoint2> Vertices { get; }

Property Value

IReadOnlyList<LengthPoint2>

Methods

Contains(LengthPoint2)

Returns true if point lies inside or on the boundary of this polygon.

Uses the ray-casting algorithm (even-odd rule): a horizontal ray is cast from the point toward +∞. If the ray crosses an odd number of polygon edges, the point is inside; if it crosses an even number, it is outside.

This algorithm works correctly for both convex and concave (non-self-intersecting) polygons.

public bool Contains(LengthPoint2 point)

Parameters

point LengthPoint2

The point to test.

Returns

bool

true when point is inside or on the polygon.

Examples

var square = new Polygon2(new[]
{
    LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(1, 0),
    LengthPoint2.FromMeters(1, 1), LengthPoint2.FromMeters(0, 1),
});
square.Contains(LengthPoint2.FromMeters(0.5, 0.5)); // => true
square.Contains(LengthPoint2.FromMeters(2.0, 2.0)); // => false

CreateRegular(LengthPoint2, Length, int)

Creates a regular polygon (all sides and angles equal) with the given circumradius.

A regular polygon with sides sides and circumradius radius has each vertex at distance radius from center, equally spaced around a full circle.

Examples: 3 sides = equilateral triangle, 4 sides = square, 6 sides = hexagon.

public static Polygon2 CreateRegular(LengthPoint2 center, Length radius, int sides)

Parameters

center LengthPoint2

The centre of the polygon.

radius Length

The circumradius: the distance from the centre to each vertex.

sides int

The number of sides (and vertices). Must be ≥ 3.

Returns

Polygon2

A regular Polygon2.

Examples

var hexagon = Polygon2.CreateRegular(LengthPoint2.Origin, Length.FromMeters(1), 6);
// hexagon.Perimeter ≈ 6 m (each side ≈ 1 m for a regular hexagon)

Exceptions

ArgumentException

Thrown when sides is less than 3.

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

point LengthPoint2

The 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));

GetClosestPoint(LengthPoint2)

Returns the point on the polygon's boundary (or interior) nearest to point. If the point is inside the polygon it is returned unchanged. Otherwise the closest point on any of the polygon's edges is returned.

public LengthPoint2 GetClosestPoint(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space query point.

Returns

LengthPoint2

The nearest point on or inside the polygon.

Examples

var square = new Polygon2(new[]
{
    LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(4, 0),
    LengthPoint2.FromMeters(4, 4), LengthPoint2.FromMeters(0, 4),
});
LengthPoint2 p = square.GetClosestPoint(LengthPoint2.FromMeters(2, -2)); // (2, 0)

MoveCenterTo(LengthPoint2)

Returns a new Polygon2 with its centre moved to newCenter, preserving all other properties.

public Polygon2 MoveCenterTo(LengthPoint2 newCenter)

Parameters

newCenter LengthPoint2

The new centre position.

Returns

Polygon2

A translated Polygon2 centred at newCenter.

Examples

var moved = shape.MoveCenterTo(LengthPoint2.FromMeters(5, 0));

TryGetManifold(Polygon2, out CollisionManifold)

Tests whether this polygon overlaps other and, if so, returns the collision manifold describing the penetration.

public bool TryGetManifold(Polygon2 other, out CollisionManifold manifold)

Parameters

other Polygon2

The other polygon to test against.

manifold CollisionManifold

When this method returns true, contains the collision data: penetration depth, contact normal (pointing from other toward this polygon), and the contact point. Otherwise the default value.

Returns

bool

true if the polygons penetrate (overlap strictly greater than zero); false if they are separated or only touching.

Exceptions

InvalidOperationException

Thrown when either polygon is not convex. Use IsConvex to check before calling.

Operators

operator +(Polygon2, LengthVector2)

Returns a new Polygon2 translated by right. Every vertex shifts by right.

public static Polygon2 operator +(Polygon2 left, LengthVector2 right)

Parameters

left Polygon2

The polygon to translate.

right LengthVector2

The displacement vector.

Returns

Polygon2

A new translated Polygon2.

latest ▼