Пример #1
0
def test_rotations(
        polygon_with_multisegment: Tuple[Polygon, Multisegment]) -> None:
    polygon, multisegment = polygon_with_multisegment

    result = multisegment_in_polygon(multisegment, polygon)

    assert all(result is multisegment_in_polygon(rotated, polygon)
               for rotated in multisegment_rotations(multisegment))
Пример #2
0
def test_step(polygon_with_multisegment: Tuple[Polygon, Multisegment]) -> None:
    polygon, multisegment = polygon_with_multisegment
    first_segment, rest_multisegment = multisegment_pop_left(multisegment)

    result = multisegment_in_polygon(rest_multisegment, polygon)
    next_result = multisegment_in_polygon(multisegment, polygon)

    relation_with_first_segment = segment_in_polygon(first_segment, polygon)
    assert equivalence(
        next_result is Relation.DISJOINT,
        result is relation_with_first_segment is Relation.DISJOINT)
    assert implication(
        next_result is Relation.TOUCH, result is Relation.TOUCH
        and relation_with_first_segment is not Relation.CROSS
        or result is Relation.DISJOINT
        and relation_with_first_segment is Relation.TOUCH)
    assert implication(
        result is Relation.DISJOINT
        and relation_with_first_segment is Relation.TOUCH
        or result is Relation.TOUCH
        and relation_with_first_segment is Relation.DISJOINT,
        next_result is Relation.TOUCH)
    assert equivalence(
        next_result is Relation.CROSS, result is Relation.CROSS
        or relation_with_first_segment is Relation.CROSS
        or (bool(rest_multisegment.segments) and result is Relation.DISJOINT
            or result is Relation.TOUCH) and
        (relation_with_first_segment is Relation.ENCLOSED
         or relation_with_first_segment is Relation.WITHIN)
        or (result is Relation.ENCLOSED or result is Relation.WITHIN) and
        (relation_with_first_segment is Relation.DISJOINT
         or relation_with_first_segment is Relation.TOUCH))
    assert equivalence(
        next_result is Relation.COMPONENT,
        (not rest_multisegment.segments or result is Relation.COMPONENT)
        and relation_with_first_segment is Relation.COMPONENT)
    assert equivalence(
        next_result is Relation.ENCLOSED, not rest_multisegment.segments
        and relation_with_first_segment is Relation.ENCLOSED
        or (result is Relation.COMPONENT or result is Relation.ENCLOSED) and
        (relation_with_first_segment is Relation.ENCLOSED
         or relation_with_first_segment is Relation.WITHIN)
        or (result is Relation.ENCLOSED or result is Relation.WITHIN)
        and relation_with_first_segment is Relation.COMPONENT
        or result is Relation.WITHIN
        and relation_with_first_segment is Relation.ENCLOSED)
    assert equivalence(
        next_result is Relation.WITHIN,
        (not rest_multisegment.segments or result is Relation.WITHIN)
        and relation_with_first_segment is Relation.WITHIN)
Пример #3
0
    def relate(self, other: Compound) -> Relation:
        """
        Finds relation between the polygon and the other geometry.

        Time complexity:
            ``O(vertices_count * log vertices_count)``
        Memory complexity:
            ``O(vertices_count)``

        where

            .. code-block:: python

                vertices_count = (len(self.border.vertices)
                                  + sum(len(hole.vertices)\
 for hole in self.holes))

        >>> from gon.base import Contour, Point, Polygon
        >>> polygon = Polygon(Contour([Point(0, 0), Point(6, 0), Point(6, 6),
        ...                            Point(0, 6)]),
        ...                   [Contour([Point(2, 2), Point(2, 4), Point(4, 4),
        ...                             Point(4, 2)])])
        >>> polygon.relate(polygon) is Relation.EQUAL
        True
        """
        return (segment_in_polygon(other, self)
                if isinstance(other, Segment)
                else (multisegment_in_polygon(other, self)
                      if isinstance(other, Linear)
                      else (polygon_in_polygon(other, self)
                            if isinstance(other, Polygon)
                            else other.relate(self).complement)))
Пример #4
0
def test_basic(
        polygon_with_multisegment: Tuple[Polygon, Multisegment]) -> None:
    polygon, multisegment = polygon_with_multisegment

    result = multisegment_in_polygon(multisegment, polygon)

    assert isinstance(result, Relation)
    assert result in LINEAR_COMPOUND_RELATIONS
def test_properties(
        polygon_with_multisegment: PolygonWithMultisegment) -> None:
    polygon, multisegment = polygon_with_multisegment

    result = unite_multisegment_with_polygon(multisegment, polygon)

    relation = multisegment_in_polygon(multisegment, polygon)
    assert (not is_mix(result)
            or (relation in (Relation.DISJOINT, Relation.TOUCH, Relation.CROSS)
                and (is_empty(result.discrete) and not is_empty(result.linear)
                     and are_compounds_similar(result.shaped, polygon))))
    assert (not is_polygon(result) or relation
            in (Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN))
Пример #6
0
def test_reversals(
        polygon_with_multisegment: Tuple[Polygon, Multisegment]) -> None:
    polygon, multisegment = polygon_with_multisegment

    result = multisegment_in_polygon(multisegment, polygon)

    assert result is multisegment_in_polygon(
        reverse_multisegment(multisegment), polygon)
    assert result is multisegment_in_polygon(multisegment,
                                             reverse_polygon_border(polygon))
    assert result is multisegment_in_polygon(multisegment,
                                             reverse_polygon_holes(polygon))
    assert result is multisegment_in_polygon(
        multisegment, reverse_polygon_holes_contours(polygon))
    assert result is multisegment_in_polygon(
        reverse_multisegment_coordinates(multisegment),
        reverse_polygon_coordinates(polygon))
Пример #7
0
def test_self(polygon: Polygon) -> None:
    assert multisegment_in_polygon(polygon_to_multisegment(polygon),
                                   polygon) is Relation.COMPONENT