- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpointinpoly.py
More file actions
Latest commit
34 lines (25 loc) · 873 Bytes
/
Copy pathpointinpoly.py
File metadata and controls
34 lines (25 loc) · 873 Bytes
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
# 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".
defpoint_in_poly(x,y,poly):
n=len(poly)
inside=False
p1x,p1y=poly[0]
foriinrange(n+1):
p2x,p2y=poly[i%n]
ify>min(p1y,p2y):
ify<=max(p1y,p2y):
ifx<=max(p1x,p2x):
ifp1y!=p2y:
xinters= (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x
ifp1x==p2xorx<=xinters:
inside=notinside
p1x,p1y=p2x,p2y
returninside
## 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
printpoint_in_poly(point_x,point_y,polygon)