-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinpoly.py
64 lines (53 loc) · 1.45 KB
/
inpoly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def inpoly(xv,yv,xt,yt):
# Originally in C by Bob Stein and Craig Yap
# http://www.visibone.com/inpoly/
# Converted to python by Bryan Miller
# 2013.04.18
# Inputs:
# xv - x vertices of polygon (does not have to be 'closed', ie. last = first)
# yv - y vertices of polygon
# xt - x of test point(s)
# yt - y of test point(s)
# 2016.06.25 - generalize to handle input arrays
nvert = len(xv)
if nvert != len(yv) or nvert < 3:
return -1
l_xt = xt
l_yt = yt
try:
npoints = len(l_xt)
except:
l_xt = [l_xt]
npoints = len(l_xt)
try:
npointsy = len(l_yt)
except:
l_yt = [l_yt]
npointsy = len(l_yt)
if npoints != npointsy:
return -1
inside = [False for ii in range(npoints)]
for jj in range(npoints):
xold=xv[nvert-1]
yold=yv[nvert-1]
for i in range(nvert):
xnew=xv[i]
ynew=yv[i]
if xnew > xold:
x1=xold
x2=xnew
y1=yold
y2=ynew
else:
x1=xnew
x2=xold
y1=ynew
y2=yold
# /* edge "open" at one end */
if (xnew < l_xt[jj]) == (l_xt[jj] <= xold) and (l_yt[jj]-y1)*(x2-x1) < ((y2-y1)*(l_xt[jj]-x1)):
inside[jj] = not inside[jj]
xold=xnew
yold=ynew
if npoints == 1:
inside = inside[0]
return inside