Function clipSegmentToPolygon

Constructs the positive-length one-dimensional components of a finite segment retained by a prevalidated polygon.

SegmentPolygonClipResult clipSegmentToPolygon(T)(
  Segment2!T segment,
  scope Polygon2View!T polygon
) @safe
if (isSegmentPolygonClipScalar!T);

Semantically, the operation constructs the closed-set segment/polygon intersection and omits connected components of topological dimension zero.

Therefore isolated tangencies and isolated vertex contacts produce no constructed component. Use classifySegmentPolygonRelationship when those contact facts are required.

Positive-length portions lying on polygon boundary are retained.

Preconditions

- segment is finite; - polygon satisfies validatePolygon(polygon).valid.

An empty polygon is valid and yields a successful empty result.

A degenerate finite segment is valid input and yields a successful empty result under the one-dimensional regularization contract.

Supported input scalar types are int, long, float, and double. real is deliberately outside the robust topology/construction domain.

Exact represented geometry determines topology and component order before any coordinate rounding. No global epsilon is used.

Constructed coordinates follow IntersectionScalar!T, currently double for every supported input scalar.

Materialization is all-or-nothing. Every selected exact endpoint must round finitely, and the complete rounded endpoint sequence must preserve strict query traversal order on the source segment's monotone x/y axis. Otherwise the result status is unrepresentableConstruction and no partial geometry is exposed.

The operation allocates O(n) private exact-event workspace for n polygon boundary edges plus immutable result storage. It deliberately does not promise @nogc.

The correctness-first implementation collects O(n) exact events and sorts them in O(n log n) time.

Parameters

NameDescription
segment finite source segment
polygon prevalidated polygon

Returns

Checked immutable owning clipping result.

Example

Example clipping a crossing segment to one polygon component.

import geo.linear_ring_view :
    LinearRing2View;

import geo.point :
    Point2;

alias P = Point2!int;
alias R = LinearRing2View!int;
alias G = Polygon2View!int;
alias S = Segment2!int;

P[4] points = [
    P(0, 0),
    P(6, 0),
    P(6, 6),
    P(0, 6),
];

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

const result =
    clipSegmentToPolygon(
        S(P(-2, 3), P(8, 3)),
        G(rings[])
    );

assert(result.succeeded);
assert(result.length == 1);

assert(
    result[0] ==
    Segment2!double(
        Point2!double(0.0, 3.0),
        Point2!double(6.0, 3.0)
    )
);