Table of Contents

Struct Capsule2

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

A capsule shape in 2D space: a rectangle with semicircular caps on both ends.

Think of a capsule as the set of all points within a given Radius of a line segment called the spine. The spine runs along the capsule's long axis, centred at Center, with half-length extending in both the capsule's Orientation direction and the opposite direction.

Practical analogy: a standard vitamin capsule, a pill, or a running-track outline.

Capsule2 is a readonly record struct: value-type, immutable, and equality-by-value.

public readonly record struct Capsule2 : IEquatable<Capsule2>
Implements
Inherited Members
Extension Methods

Examples

// Horizontal capsule: 4 m long, 1 m radius, centred at origin
var capsule = new Capsule2(
    LengthPoint2.Origin,
    Length.FromMeters(4),
    Length.FromMeters(1),
    Direction2.East);

Length arc   = capsule.Perimeter; // 2π·1 + 2·4 ≈ 14.28 m
Area   area  = capsule.Area;      // π·1² + 4·2·1 ≈ 11.14 m²

bool inside  = capsule.Contains(LengthPoint2.FromMeters(3, 0)); // true — in end cap
bool outside = capsule.Contains(LengthPoint2.FromMeters(0, 1.5)); // false — beyond radius

Constructors

Capsule2(LengthPoint2, Length, Length, Direction2)

Constructs a Capsule2 from a centre, a spine length, a radius, and an orientation.

public Capsule2(LengthPoint2 center, Length length, Length radius, Direction2 orientation)

Parameters

center LengthPoint2

The midpoint of the spine segment.

length Length

The spine length (the flat portion between the two semicircular caps). Must be ≥ 0.

radius Length

The radius of the end caps and half-width of the body. Must be > 0.

orientation Direction2

The direction from SpineStart to SpineEnd.

Examples

var capsule = new Capsule2(LengthPoint2.Origin, Length.FromMeters(4), Length.FromMeters(1), Direction2.East);

Exceptions

ArgumentException

Thrown when length is negative or radius is non-positive.

Properties

Area

The area of the capsule: the rectangular body plus the two semicircular end caps.

Formula: Area = π·r² + Length · 2·r

public Area Area { get; }

Property Value

Area

Center

The geometric centre of the capsule — the midpoint of its spine segment.

public LengthPoint2 Center { get; }

Property Value

LengthPoint2

Length

The spine length: the distance from one flat centre point to the other, not including the caps.

public Length Length { get; }

Property Value

Length

Orientation

The orientation of the capsule's long axis: the direction from SpineStart toward SpineEnd.

public Direction2 Orientation { get; }

Property Value

Direction2

Perimeter

The perimeter of the capsule: the two straight sides plus a full circle of circumference.

Formula: Perimeter = 2·π·r + 2·Length

public Length Perimeter { get; }

Property Value

Length

Radius

The radius of the capsule's semicircular caps and the half-width of its rectangular body.

public Length Radius { get; }

Property Value

Length

SpineEnd

The point at the centre of the second semicircular cap — the other end of the spine.

Located at Center + (Length/2) × Orientation.

public LengthPoint2 SpineEnd { get; }

Property Value

LengthPoint2

SpineStart

The point at the centre of the first semicircular cap — one end of the spine.

Located at Center − (Length/2) × Orientation.

public LengthPoint2 SpineStart { get; }

Property Value

LengthPoint2

Methods

Contains(LengthPoint2)

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

A point is inside the capsule when its distance to the nearest point on the spine segment is ≤ Radius. This test correctly handles points off the end of the spine (covered by the semicircular caps) as well as points alongside the rectangular body.

public bool Contains(LengthPoint2 point)

Parameters

point LengthPoint2

The point to test.

Returns

bool

true when point is inside or on the capsule.

Examples

var capsule = new Capsule2(LengthPoint2.Origin, Length.FromMeters(4), Length.FromMeters(1), Direction2.East);

capsule.Contains(LengthPoint2.FromMeters(3, 0));   // true  — in the end cap
capsule.Contains(LengthPoint2.FromMeters(0, 0.8)); // true  — in the rectangular body
capsule.Contains(LengthPoint2.FromMeters(0, 1.5)); // false — outside

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 capsule's boundary nearest to point. The closest point on the spine is found, then the result is projected onto the capsule's surface in the direction from that spine point to point.

public LengthPoint2 GetClosestPoint(LengthPoint2 point)

Parameters

point LengthPoint2

The world-space query point.

Returns

LengthPoint2

The nearest point on the capsule's boundary.

Examples

var capsule = new Capsule2(LengthPoint2.Origin, Length.FromMeters(4), Length.FromMeters(1), Direction2.East);
LengthPoint2 p = capsule.GetClosestPoint(LengthPoint2.FromMeters(0, 3)); // (0, 1)

MoveCenterTo(LengthPoint2)

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

public Capsule2 MoveCenterTo(LengthPoint2 newCenter)

Parameters

newCenter LengthPoint2

The new centre position.

Returns

Capsule2

A translated Capsule2 centred at newCenter.

Examples

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

Overlaps(Circle2)

Returns true if this capsule overlaps (intersects or touches) a circle. The test checks whether the distance from the capsule's spine to the circle's centre is less than or equal to the sum of the capsule's radius and the circle's radius.

public bool Overlaps(Circle2 circle)

Parameters

circle Circle2

The circle to test against.

Returns

bool

true if the shapes share any area or touch.

Examples

var capsule = new Capsule2(LengthPoint2.Origin, Length.FromMeters(4), Length.FromMeters(1), Direction2.East);
var circle  = new Circle2(LengthPoint2.FromMeters(0, 1.5), Length.FromMeters(1));
capsule.Overlaps(circle); // true

TryGetManifold(Capsule2, out CollisionManifold)

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

public bool TryGetManifold(Capsule2 other, out CollisionManifold manifold)

Parameters

other Capsule2

The other capsule to test against.

manifold CollisionManifold

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

Returns

bool

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

TryGetManifold(Polygon2, out CollisionManifold)

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

public bool TryGetManifold(Polygon2 other, out CollisionManifold manifold)

Parameters

other Polygon2

The polygon to test against.

manifold CollisionManifold

When this method returns true, contains the collision data.

Returns

bool

true if the shapes penetrate (overlap strictly greater than zero).

TryGetManifold(Triangle2, out CollisionManifold)

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

public bool TryGetManifold(Triangle2 other, out CollisionManifold manifold)

Parameters

other Triangle2

The triangle to test against.

manifold CollisionManifold

When this method returns true, contains the collision data.

Returns

bool

true if the shapes penetrate (overlap strictly greater than zero).

Operators

operator +(Capsule2, LengthVector2)

Returns a new Capsule2 translated by right. The shape (length, radius, orientation) is preserved; only the centre moves.

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

Parameters

left Capsule2

The capsule to translate.

right LengthVector2

The displacement vector.

Returns

Capsule2

A new Capsule2 with centre shifted by right.

latest ▼