Function trySegmentIntersectionOverlap

Constructs the positive-length collinear overlap of two closed segments.

bool trySegmentIntersectionOverlap(T)(
  Segment2!T first,
  Segment2!T second,
  out Segment2!T overlap
) pure nothrow @nogc @safe
if (isIntersectionScalar!T);

Returns true exactly when:

segmentIntersectionKind(first, second) == SegmentIntersectionKind.overlap

Supported scalar types:

int long float double

On success, overlap contains the exact common segment in the original scalar type. Its endpoints are selected from the input endpoints and returned in canonical lexicographic order.

Returns false for:

- disjoint segments; - unique-point intersections.

No numerical intersection coordinate is constructed.

For floating-point coordinates all endpoints must be finite.

On failure, overlap is Segment2!T.init.

No allocation is performed.

Complexity

time O(1) space O(1)

Example

Example constructing the exact canonical overlap in the input scalar type.

import geo;

alias P = Point2!int;
alias S = Segment2!int;

const first =
    S(
        P(10, 0),
        P(0, 0)
    );

const second =
    S(
        P(3, 0),
        P(12, 0)
    );

S overlap;

assert(
    trySegmentIntersectionOverlap(
        first,
        second,
        overlap
    )
);

static assert(
    is(typeof(overlap) == S)
);

assert(
    overlap ==
    S(
        P(3, 0),
        P(10, 0)
    )
);