Struct Sector2
A sector ("pie slice") of a circle in 2D space: the region bounded by two radii and the arc between them.
A sector is defined by:
- A Circle — provides centre and radius.
- A StartDirection — the direction from the centre to the start edge.
- An EndDirection — the direction from the centre to the end edge.
- A Direction — whether the sweep is clockwise (CW) or counter-clockwise (CCW).
From these, Area, ArcLength, Perimeter, and SweepAngle are computed automatically.
Sector2 is a readonly record struct: value-type, immutable,
and equality-by-value.
public readonly record struct Sector2 : IEquatable<Sector2>
- Implements
- Inherited Members
- Extension Methods
Examples
// Quarter-circle sector: unit circle from East to North, CCW
var circle = new Circle2(LengthPoint2.Origin, Radius.FromMeters(1));
var sector = new Sector2(circle, Direction2.East, Direction2.North, ArcDirection.CounterClockwise);
Area area = sector.Area; // π/4 ≈ 0.7854 m²
Length arcLen = sector.ArcLength; // π/2 ≈ 1.5708 m
Length perim = sector.Perimeter; // π/2 + 2 ≈ 3.5708 m
Constructors
Sector2(Circle2, Direction2, Direction2, ArcDirection)
Constructs a Sector2 from a circle, two bounding directions, and a sweep direction.
public Sector2(Circle2 circle, Direction2 startDirection, Direction2 endDirection, ArcDirection direction)
Parameters
circleCircle2The circle on which the sector lies.
startDirectionDirection2Direction from the centre to the start edge.
endDirectionDirection2Direction from the centre to the end edge.
directionArcDirectionSweep direction: CounterClockwise or Clockwise.
Examples
var sector = new Sector2(
new Circle2(LengthPoint2.Origin, Radius.FromMeters(2)),
Direction2.East,
Direction2.North,
ArcDirection.CounterClockwise);
Properties
ArcLength
The length of the arc forming the curved edge of the sector.
Formula: ArcLength = r · |sweep_radians|
public Length ArcLength { get; }
Property Value
- Length
Area
The area of the sector: the "pie slice" region bounded by the two radii and the arc.
Formula: Area = ½ · r² · |sweep_radians|
public Area Area { get; }
Property Value
- Area
Center
The centre position of this sector (the centre of its underlying circle).
Alias for Circle.Center. Exposes the position under the standardised Center
name shared by all shape types, enabling uniform access and source-generator contracts.
public LengthPoint2 Center { get; }
Property Value
Examples
var radius = Radius.FromMeters(2);
var sector = new Sector2(new Circle2(LengthPoint2.FromMeters(3, 4), radius),
Direction2.East, Direction2.North, ArcDirection.CounterClockwise);
LengthPoint2 c = sector.Center; // (3, 4) — same as sector.Circle.Center
Circle
The circle on which the sector lies. Provides centre position and radius.
public Circle2 Circle { get; }
Property Value
Direction
Whether the sector sweeps clockwise or counter-clockwise from start to end.
public ArcDirection Direction { get; }
Property Value
EndDirection
The direction from the circle's centre to the second (end) edge of the sector.
public Direction2 EndDirection { get; }
Property Value
Perimeter
The total perimeter of the sector: the arc length plus the two straight radii.
Formula: Perimeter = ArcLength + 2 · r
public Length Perimeter { get; }
Property Value
- Length
StartDirection
The direction from the circle's centre to the first (start) edge of the sector.
public Direction2 StartDirection { get; }
Property Value
SweepAngle
The angle swept from StartDirection to EndDirection in the specified Direction.
Always positive. A full circle has a sweep of 360°.
public Angle SweepAngle { get; }
Property Value
- Angle
Methods
Contains(LengthPoint2)
Returns true if point lies inside or on the
boundary of this sector.
A point is inside the sector when two conditions are met:
- Its distance from the circle's centre is ≤ the radius.
- The direction from the centre to the point falls within the angular span (from StartDirection to EndDirection in the sweep Direction).
public bool Contains(LengthPoint2 point)
Parameters
pointLengthPoint2The point to test.
Returns
Examples
var circle = new Circle2(LengthPoint2.FromMeters(0, 0), Radius.FromMeters(1));
var sector = new Sector2(circle, Direction2.East, Direction2.North, ArcDirection.CounterClockwise);
sector.Contains(LengthPoint2.FromMeters(0.5, 0.5)); // true — inside the pie slice
sector.Contains(LengthPoint2.FromMeters(1.0, 1.0)); // false — outside the radius
sector.Contains(LengthPoint2.FromMeters(-0.5, 0)); // false — wrong angular direction
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));
GetClosestPoint(LengthPoint2)
Returns the point on the sector's boundary (or interior) nearest to point.
If the point is inside the sector it is returned unchanged. Otherwise the minimum-distance
candidate among the arc boundary, start edge, and end edge is returned.
public LengthPoint2 GetClosestPoint(LengthPoint2 point)
Parameters
pointLengthPoint2The world-space query point.
Returns
- LengthPoint2
The nearest point on or inside the sector.
Examples
var sector = new Sector2(
new Circle2(LengthPoint2.Origin, Length.FromMeters(2)),
Direction2.East, Direction2.North, ArcDirection.CounterClockwise);
LengthPoint2 p = sector.GetClosestPoint(LengthPoint2.FromMeters(3, 0)); // (2, 0)
MoveCenterTo(LengthPoint2)
Returns a new Sector2 with its centre moved to
newCenter, preserving all other properties.
public Sector2 MoveCenterTo(LengthPoint2 newCenter)
Parameters
newCenterLengthPoint2The new centre position.
Returns
Examples
var moved = shape.MoveCenterTo(LengthPoint2.FromMeters(5, 0));
Operators
operator +(Sector2, LengthVector2)
Returns a new Sector2 translated by right.
The sweep angles and radius are preserved; only the circle's centre moves.
public static Sector2 operator +(Sector2 left, LengthVector2 right)
Parameters
leftSector2The sector to translate.
rightLengthVector2The displacement vector.