Example #1
0
def test_properties(polygon_with_segment: PolygonWithSegment
                    ) -> None:
    polygon, segment = polygon_with_segment

    result = complete_intersect_segment_with_polygon(segment, polygon)

    result_points, result_segments = pack_non_shaped(result)
    assert all(point_in_segment(point, segment) is Location.BOUNDARY
               for point in result_points)
    assert all(point_in_polygon(point, polygon) is Location.BOUNDARY
               for point in result_points)
    assert (not (segment_in_polygon(segment, polygon) is Relation.TOUCH
                 and all(segments_relation(segment, contour_segment)
                         in (Relation.CROSS, Relation.DISJOINT, Relation.TOUCH)
                         for contour in to_polygon_contours(polygon)
                         for contour_segment in to_contour_segments(contour)))
            or any(point_in_segment(point, segment) is Location.BOUNDARY
                   for point in result_points)
            or any(segments_relation(segment, result_segment)
                   is Relation.TOUCH
                   for result_segment in result_segments))
    assert all(segment_in_segment(result_segment, segment)
               in (Relation.EQUAL, Relation.COMPONENT)
               for result_segment in result_segments)
    assert all(segment_in_polygon(result_segment, polygon)
               in (Relation.COMPONENT, Relation.ENCLOSED, Relation.WITHIN)
               for result_segment in result_segments)
    assert (segment_in_polygon(segment, polygon)
            not in (Relation.CROSS, Relation.COMPONENT, Relation.ENCLOSED,
                    Relation.WITHIN)
            or to_sorted_segment(segment) in result_segments
            # in case of cross
            or any(segment_in_segment(result_segment, segment)
                   is Relation.COMPONENT
                   for result_segment in result_segments))
Example #2
0
def test_connection_with_intersect(polygon_with_segment
                                   : PolygonWithSegment) -> None:
    polygon, segment = polygon_with_segment

    result = complete_intersect_segment_with_polygon(segment, polygon)

    assert (compound_to_linear(result)
            == intersect_segment_with_polygon(segment, polygon))
Example #3
0
    def __and__(self, other: Compound) -> Compound:
        """
        Returns intersection of the polygon with 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 & polygon == polygon
        True
        """
        return (complete_intersect_segment_with_polygon(other, self,
                                                        context=self._context)
                if isinstance(other, Segment)
                else
                (complete_intersect_multisegment_with_polygon(
                        other, self,
                        context=self._context)
                 if isinstance(other, Linear)
                 else ((complete_intersect_polygons(self, other,
                                                    context=self._context)
                        if self.holes or other.holes
                        else complete_intersect_regions(self.border,
                                                        other.border,
                                                        context=self._context))
                       if isinstance(other, Polygon)
                       else NotImplemented)))
Example #4
0
def test_reversals(polygon_with_segment: PolygonWithSegment
                   ) -> None:
    polygon, segment = polygon_with_segment

    result = complete_intersect_segment_with_polygon(segment, polygon)

    assert result == complete_intersect_segment_with_polygon(
            segment, reverse_polygon_border(polygon))
    assert result == complete_intersect_segment_with_polygon(
            segment, reverse_polygon_holes(polygon))
    assert result == complete_intersect_segment_with_polygon(
            segment, reverse_polygon_holes_contours(polygon))
    assert result == complete_intersect_segment_with_polygon(
            reverse_segment(segment), polygon)
    assert are_compounds_similar(
            result, reverse_compound_coordinates(
                    complete_intersect_segment_with_polygon(
                            reverse_segment_coordinates(segment),
                            reverse_polygon_coordinates(polygon))))
Example #5
0
def test_basic(polygon_with_segment: PolygonWithSegment) -> None:
    polygon, segment = polygon_with_segment

    result = complete_intersect_segment_with_polygon(segment, polygon)

    assert is_non_shaped(result)