Function polygonArea

Role-based algebraic area of a polygon.

AreaScalar!T polygonArea(T)(
  scope Polygon2View!T polygon
) pure nothrow @nogc @safe
if (is(T == int) || is(T == long) || is(T == float) || is(T == double));

Supported scalar types are int, long, float, and double. The result is binary64.

Ring zero is the exterior ring and contributes the magnitude of its exact algebraic area positively. Subsequent rings are interior rings and contribute their exact area magnitudes negatively.

Ring orientation therefore does not affect the result.

All ring contributions are combined exactly before one final correctly-rounded binary64 conversion.

The empty polygon has area positive zero.

A polygon containing any non-finite coordinate returns NaN.

This operation does not validate polygon topology. Invalid polygon representations may therefore produce a negative role-based result.

No allocation is performed.

Complexity

O(n) time and O(1) auxiliary space for n stored vertices across all rings.

Example

Example showing that polygon ring roles are defined by storage order.

import geo;

alias P = Point2!double;
alias R = LinearRing2View!double;
alias V = Polygon2View!double;

P[4] exteriorPoints = [
    P(0.0, 0.0),
    P(10.0, 0.0),
    P(10.0, 10.0),
    P(0.0, 10.0)
];

P[4] holePoints = [
    P(3.0, 3.0),
    P(7.0, 3.0),
    P(7.0, 7.0),
    P(3.0, 7.0)
];

R[2] rings = [
    R(exteriorPoints[]),
    R(holePoints[])
];

auto polygon =
    V(rings[]);

assert(
    polygonArea(polygon) ==
    84.0
);