Function signedArea

Algebraic signed area of a linear ring.

AreaScalar!T signedArea(T)(
  scope LinearRing2View!T ring
) 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.

Counter-clockwise traversal has positive area and clockwise traversal has negative area in the usual Cartesian coordinate system.

For finite supported coordinates, the exact determinant sum is accumulated before one final correctly-rounded binary64 conversion.

Empty, singleton, two-vertex and otherwise algebraically degenerate rings have area zero.

A ring containing any non-finite coordinate returns NaN.

Self-intersecting rings produce their algebraic signed area; this operation does not validate polygon topology.

No allocation is performed.

Complexity

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

Example

Example showing the sign defined by ring traversal orientation.

import geo;

alias P = Point2!int;
alias R = LinearRing2View!int;

P[4] counterClockwisePoints = [
    P(0, 0),
    P(4, 0),
    P(4, 3),
    P(0, 3)
];

P[4] clockwisePoints = [
    P(0, 0),
    P(0, 3),
    P(4, 3),
    P(4, 0)
];

assert(
    signedArea(
        R(counterClockwisePoints[])
    ) ==
    12.0
);

assert(
    signedArea(
        R(clockwisePoints[])
    ) ==
    -12.0
);