Function tryClassifyPointInPolygon

Classifies a point relative to a polygon.

bool tryClassifyPointInPolygon(T)(
  scope Polygon2View!T polygon,
  Point2!T point,
  out PointPolygonLocation location
) 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. real is deliberately outside the robust predicate domain.

Returns false when the query point or any stored polygon coordinate is non-finite.

On failure, location is PointPolygonLocation.outside.

For finite supported input, classification is exact and returns one of:

outside boundary inside

A finite query against an empty polygon succeeds and is classified as outside.

Boundary has precedence over inside and outside across all stored rings.

Ring zero is the exterior ring. Subsequent rings are interior rings. Ring orientation does not affect classification.

No topology validation, normalization, tolerance, or floating-point ray intersection is performed.

No allocation is performed.

Every stored ring is inspected so that non-finite coordinates and boundary precedence are handled globally.

Complexity

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

Example

Example classifying interior, boundary, and exterior points.

import geo;

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

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

R[1] rings = [
    R(points[])
];

auto polygon =
    V(rings[]);

PointPolygonLocation location;

assert(
    tryClassifyPointInPolygon(
        polygon,
        P(5.0, 5.0),
        location
    )
);

assert(
    location ==
    PointPolygonLocation.inside
);

assert(
    tryClassifyPointInPolygon(
        polygon,
        P(0.0, 5.0),
        location
    )
);

assert(
    location ==
    PointPolygonLocation.boundary
);

assert(
    tryClassifyPointInPolygon(
        polygon,
        P(20.0, 5.0),
        location
    )
);

assert(
    location ==
    PointPolygonLocation.outside
);