Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Created April 5, 2019 18:47
Show Gist options
  • Save edgabaldi/86be8bb384531e2d48aa0fd3aeb41973 to your computer and use it in GitHub Desktop.
Save edgabaldi/86be8bb384531e2d48aa0fd3aeb41973 to your computer and use it in GitHub Desktop.
def point_in_polygon(point, polygon):
inside=False
j=len(polygon)-1
for i in range(len(polygon)):
if ((polygon[i][1]>point[1])!=(polygon[j][1]>point[1]) and (point[0]<(polygon[j][0]-polygon[i][0])*(point[1]-polygon[i][1])/( polygon[j][1] - polygon[i][1] ) + polygon[i][0])):
inside =not inside
j=i
return inside
if __name__ == "__main__":
fora = (-1, -1)
area1 = [(0,0),(0,5),(2,5),(2,4),(5,4),(5,0)]
dentro = (1, 1)
area2 = [(2,5),(2,4), (5, 4),(5, 5)]
dentro_a2 = (3, 4)
assert point_in_polygon(dentro, area1) is True
assert point_in_polygon(fora, area1) is False
assert point_in_polygon(dentro_a2, area2) is True
assert point_in_polygon(fora, area2) is False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment