forked from AmphibiaWeb/SpeciesLookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray_casting.py
56 lines (46 loc) · 1.65 KB
/
ray_casting.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
# implementation of ray_casting algorithm
class points:
def __init__(self, x, y):
self.x = x
self.y = y
class edge:
def __init__(self, pointa, pointb):
self.a = pointa
self.b = pointb
def intersect(self, point):
# I am assuming the ray always goes straight down along x = point.x line
hit = 0
# ray casting method here
# The edge is entirely left of the point
if min(self.a.x, self.b.x) > point.x:
return hit
# The edge is entirely right of the point
if max(self.a.x, self.b.x) < point.x:
return hit
# This is for a vertical edge
if self.b.x == self.a.x:
# tangents are not counted as inside the polygon
return 0
projected_y = self.a.y + (self.b.y - self.a.y) / (self.b.x - self.a.x) * (point.x - self.a.x)
if projected_y <= point.y:
# ignore the right point if the query point is right on top of it.
if point.x == max(self.a.x, self.b.x):
# Have to account for overcounting
hit = 0
return hit
else:
hit = 1
return hit
else:
return hit
class polygon:
def __init__(self, points):
# convert points to a list of edges
self.edges = [edge(points[i], points[(i + 1) % len(points)]) for i in range(len(points))]
def is_inside(self, query_point):
hits = sum([edge.intersect(query_point) for edge in self.edges])
if hits % 2 == 1:
# odd number of intersections
return True
else:
return False