Created
April 14, 2014 02:28
-
-
Save huyx/10611613 to your computer and use it in GitHub Desktop.
判断点是否在多边形内部
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""判断点是否在多边形内部 | |
参考: https://github.com/flaviamissi/Point-Inside-Polygon | |
""" | |
def get_number_of_intersected_sides(self, point, polygon): | |
crossed_by_left = 0; crossed_by_right = 0 | |
for i in range(1, len(polygon)): | |
if point[1] in range(polygon[i-1][1], polygon[i][1]+1) or point[1] in range(polygon[i][1], polygon[i-1][1]+1): | |
if point[0] > polygon[i-1][0] and point[0] > polygon[i][0]: | |
crossed_by_left += 1 | |
else: | |
crossed_by_right += 1 | |
return {"left" : crossed_by_left, "right" : crossed_by_right} | |
def point_inside_polygon(self, point, polygon): | |
crossed = self.get_number_of_intersected_sides(point) | |
return crossed["left"] % 2 != 0 and crossed["right"] % 2 != 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment