1 Star 0 Fork 1

weishujie / Learn

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
pointinpoly.py 840 Bytes
一键复制 编辑 原始数据 按行查看 历史
geospatialpython 提交于 2015-06-18 12:37 . Consolidated repositories
# Determine if a point is inside a given polygon or not
# Polygon is a list of (x,y) pairs. This fuction
# returns True or False. The algorithm is called
# "Ray Casting Method".
def point_in_poly(x,y,poly):
n = len(poly)
inside = False
p1x,p1y = poly[0]
for i in range(n+1):
p2x,p2y = poly[i % n]
if y > min(p1y,p2y):
if y <= max(p1y,p2y):
if x <= max(p1x,p2x):
if p1y != p2y:
xinters = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
if p1x == p2x or x <= xinters:
inside = not inside
p1x,p1y = p2x,p2y
return inside
## Test
polygon = [(0,10),(10,10),(10,0),(0,0)]
point_x = 5
point_y = 5
## Call the fuction with the points and the polygon
print point_in_poly(point_x,point_y,polygon)
1
https://gitee.com/wei_shujie/Learn.git
git@gitee.com:wei_shujie/Learn.git
wei_shujie
Learn
Learn
master

搜索帮助