polygon2.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:pygorithm 作者: OmkarPathak 项目源码 文件源码
def contains_point(polygon, offset, point):
        """
        Determine if the polygon at offset contains point.

        Distinguish between points that are on the edge of the polygon and 
        points that are completely contained by the polygon.

        .. tip::

            This can never return True, True

        This finds the cross product of this point and the two points comprising
        every line on this polygon. If any are 0, this is an edge. Otherwise,
        they must all be negative (when traversed clockwise).

        :param polygon: the polygon 
        :type polygon: :class:`pygorithm.geometry.polygon2.Polygon2`
        :param offset: the offset of the polygon
        :type offset: :class:`pygorithm.geometry.vector2.Vector2` or None
        :param point: the point to check
        :type point: :class:`pygorithm.geometry.vector2.Vector2`
        :returns: on edge, contained
        :rtype: bool, bool
        """

        _previous = polygon.points[0]
        for i in range(1, len(polygon.points) + 1):
            curr = polygon.points[i % len(polygon.points)]

            vec1 = _previous + offset - point
            vec2 = curr + offset - point
            cross = vec1.cross(vec2)
            _previous = curr

            if math.isclose(cross, 0, abs_tol=1e-07):
                return True, False

            if cross > 0:
                return False, False

        return False, True
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号