def test_intersection_minuend(segments_triplet: SegmentsTriplet) -> None:
    first, second, third = segments_triplet

    first_second_intersection = intersect_segments(first, second)
    second_third_difference = subtract_segments(second, third)
    assert (not is_segment(first_second_intersection)
            or not is_segment(second_third_difference)
            or are_compounds_similar(
                subtract_segments(first_second_intersection, third),
                intersect_segments(first, second_third_difference)))
def test_reversals(segments_pair: SegmentsPair) -> None:
    first, second = segments_pair

    result = intersect_segments(first, second)
    assert result == intersect_segments(first, reverse_segment(second))
    assert are_compounds_similar(
        result,
        reverse_compound_coordinates(
            intersect_segments(reverse_segment_coordinates(first),
                               reverse_segment_coordinates(second))))
Exemple #3
0
def test_distribution_over_intersection(
        segments_triplet: SegmentsTriplet) -> None:
    first, second, third = segments_triplet

    first_second_union = unite_segments(first, second)
    first_third_union = unite_segments(first, third)
    second_third_intersection = intersect_segments(second, third)
    assert (not is_segment(first_second_union)
            or not is_segment(first_third_union)
            or not is_segment(second_third_intersection)
            or are_compounds_similar(
                unite_segments(first, second_third_intersection),
                intersect_segments(first_second_union, first_third_union)))
def test_properties(segments_pair: SegmentsPair) -> None:
    first, second = segments_pair

    result = intersect_segments(first, second)

    result_points, result_segments = pack_non_shaped(result)
    assert all(
        point_in_segment(point, first) is point_in_segment(point, second) is
        Location.BOUNDARY for point in result_points)
    assert (segment_in_segment(first, second) is not Relation.TOUCH
            or bool(result_points))
    assert (segment_in_segment(first,
                               second) not in (Relation.TOUCH, Relation.CROSS)
            or all(point in result_points or any(
                point == result_segment.start or point == result_segment.end
                for result_segment in result_segments)
                   for point in segments_intersections(first, second)))
    assert all(
        segment_in_segment(result_segment, first) in (Relation.EQUAL,
                                                      Relation.COMPONENT)
        for result_segment in result_segments)
    assert all(
        segment_in_segment(result_segment, second) in (Relation.EQUAL,
                                                       Relation.COMPONENT)
        for result_segment in result_segments)
    assert (segments_relation(
        first, second) in (Relation.CROSS, Relation.DISJOINT, Relation.TOUCH)
            or to_sorted_segment(first) in result_segments or any(
                segment_in_segment(result_segment, first) is Relation.COMPONENT
                for result_segment in result_segments))
def test_union_subtrahend(segments_triplet: SegmentsTriplet) -> None:
    first, second, third = segments_triplet

    first_second_difference = subtract_segments(first, second)
    first_third_difference = subtract_segments(first, third)
    second_third_union = unite_segments(second, third)
    assert (not is_segment(first_second_difference)
            or not is_segment(first_third_difference)
            or not is_segment(second_third_union) or are_compounds_similar(
                subtract_segments(first, second_third_union),
                intersect_segments(first_second_difference,
                                   first_third_difference)))
Exemple #6
0
def test_equivalents(segments_pair: SegmentsPair) -> None:
    first, second = segments_pair

    result = unite_segments(first, second)

    first_second_symmetric_difference = symmetric_subtract_segments(
        first, second)
    first_second_intersection = intersect_segments(first, second)
    assert (not is_segment(first_second_symmetric_difference)
            or not is_segment(first_second_intersection)
            or are_compounds_similar(
                result,
                symmetric_subtract_segments(first_second_symmetric_difference,
                                            first_second_intersection)))
Exemple #7
0
    def __and__(self, other: Compound[Coordinate]) -> Compound[Coordinate]:
        """
        Returns intersection of the segment with the other geometry.

        Time complexity:
            ``O(1)``
        Memory complexity:
            ``O(1)``

        >>> from gon.base import Point, Segment
        >>> segment = Segment(Point(0, 0), Point(2, 0))
        >>> segment & segment == segment
        True
        """
        return (intersect_segments(self, other, context=self._context)
                if isinstance(other, Segment) else NotImplemented)
def test_commutativity(segments_pair: SegmentsPair) -> None:
    first, second = segments_pair

    result = intersect_segments(first, second)

    assert are_compounds_similar(result, intersect_segments(second, first))
def test_absorption_identity(segments_pair: SegmentsPair) -> None:
    first, second = segments_pair

    first_second_union = unite_segments(first, second)
    assert (not is_segment(first_second_union)
            or intersect_segments(first, first_second_union) == first)
def test_idempotence(segment: Segment) -> None:
    result = intersect_segments(segment, segment)

    assert result == segment
def test_basic(segments_pair: SegmentsPair) -> None:
    first, second = segments_pair

    result = intersect_segments(first, second)

    assert is_homogeneous_non_shaped(result)