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

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(...); // unit square
square.Contains(LengthPoint2.FromMeters(0.5, 0.5)); // true  — interior
square.Contains(LengthPoint2.FromMeters(2.0, 2.0)); // false — exterior

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 shortest distance from point to the polygon's boundary. Returns UnitsNet.Length.Zero if the point is inside or on the polygon.

public Length DistanceTo(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space query point.

Returns

Length

Distance to the nearest edge, or zero if the point is already inside.

Examples

var square = new Polygon2(new[]
{
    LengthPoint2.FromMeters(0, 0), LengthPoint2.FromMeters(4, 0),
    LengthPoint2.FromMeters(4, 4), LengthPoint2.FromMeters(0, 4),
});
square.DistanceTo(LengthPoint2.FromMeters(2, -3)); // 3 m
square.DistanceTo(LengthPoint2.FromMeters(2,  2)); // 0 m  (inside)

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)

MoveCentroidTo(LengthPoint2)

Returns a new Polygon2 whose centroid is at newCentroid. All vertices shift by the same offset.

public Polygon2 MoveCentroidTo(LengthPoint2 newCentroid)

Parameters

newCentroid LengthPoint2

The desired centroid position.

Returns

Polygon2

A translated Polygon2 with centroid at newCentroid.

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.

v0.4.0-preview5 ▼