-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_from_file.py
217 lines (176 loc) · 6.46 KB
/
main_from_file.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from plotter import Plotter
class Geometry:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
class Point(Geometry):
def __init__(self, name, x, y):
super().__init__(name)
self.__x = x
self.__y = y
def get_x(self):
return self.__x
def get_y(self):
return self.__y
class Line(Geometry):
def __init__(self, name, point_1, point_2):
super().__init__(name)
self.__point_1 = point_1
self.__point_2 = point_2
def get_point_1(self):
return self.__point_1
def get_point_2(self):
return self.__point_2
class Polygon(Geometry):
def __init__(self, name, points):
super().__init__(name)
self.__points = points
def get_points(self):
return self.__points
def lines(self):
res = []
points = self.get_points()
point_a = points[0]
for point_b in points[1:]:
res.append(Line(point_a.get_name() + '-' + point_b.get_name(), point_a, point_b))
point_a = point_b
# res.append(Line(point_a.get_name() + '-' + points[0].get_name(), point_a, points[0]))
return res
class RCA:
def __init__(self, polygen):
self.polygon = polygen
def find_category(self, point):
lines = self.polygon.lines()
# Store the number of intersections
intersec_points_num = 0
for line in lines:
# Check whether the point is on the line of the polygen
if self.is_point_on_line(point, line):
return 'boundary'
# Check whether the corresponding ray intersect with the line
if self.is_ray_intersect_line(point, line):
intersec_points_num += 1
return 'inside' if intersec_points_num % 2 == 1 else 'outside'
@staticmethod
def is_point_on_line(point, line):
px = point.get_x()
py = point.get_y()
p_1_x = line.get_point_1().get_x()
p_1_y = line.get_point_1().get_y()
p_2_x = line.get_point_2().get_x()
p_2_y = line.get_point_2().get_y()
if (px - p_1_x) * (p_2_y - p_1_y) == (p_2_x - p_1_x) * (py - p_1_y) and min(p_1_x, p_2_x) <= px <= max(p_1_x,
p_2_x) \
and min(p_1_y, p_2_y) <= py <= max(p_1_y, p_2_y) and (p_1_x != p_2_x or p_1_y != p_2_y):
return True
else:
return False
@staticmethod
def is_ray_intersect_line(point, line):
px = point.get_x()
py = point.get_y()
p_1_x = line.get_point_1().get_x()
p_1_y = line.get_point_1().get_y()
p_2_x = line.get_point_2().get_x()
p_2_y = line.get_point_2().get_y()
if p_1_y == p_2_y:
return False
if p_1_y > py and p_2_y > py:
return False
if p_1_y < py and p_2_y < py:
return False
if p_1_y == py and p_2_y > py:
return False
if p_2_y == py and p_1_y > py:
return False
if p_1_x < px and p_2_y < py:
return False
xseg = p_2_x - (p_2_x - p_1_x) * (p_2_y - py) / (p_2_y - p_1_y)
if xseg < px:
return False
return True
class MBR:
def __init__(self, polygen):
self.polygen = polygen
def find_category(self, point):
points = self.polygen.get_points()
# Find the points which has minimum or maximum coordinates
xs = [point.get_x() for point in points]
ys = [point.get_y() for point in points]
x_min = min(xs)
x_max = max(xs)
y_min = min(ys)
y_max = max(ys)
point_x = point.get_x()
point_y = point.get_y()
# If the point is in the minimum bounding rectangle, the point is outside of the polygen.
if point_x > x_max or point_x < x_min or point_y > y_max or point_y < y_min:
return 'outside'
else:
return None
def find_category(polygen, point):
# First use MBR to check the point, if not work, use the RCA algorithm
mbr = MBR(polygen)
mbr_category = mbr.find_category(point)
if mbr_category is not None:
return mbr_category
else:
rca = RCA(polygen)
rca_category = rca.find_category(point)
return rca_category
def main():
plotter = Plotter()
print("read polygon.csv")
# Init polygen_points to store points in the polygen
polygen_points = []
# Read polygon.csv
with open('polygon.csv', 'r') as f:
for line in f:
if not line.startswith('id'):
id = line.split(',')[0].strip()
x = float(line.split(',')[1].strip())
y = float(line.split(',')[2].strip())
# Init a Point object
point = Point(id, x, y)
# Add the Point object to the list of points
polygen_points.append(point)
# Init a Polygon object
polygen = Polygon('polygen', polygen_points)
print("read input.csv")
# Init test_points to store points to be tested
test_points = []
# Read input.csv
with open('input.csv', 'r') as f:
for line in f:
if not line.startswith('id'):
id = line.split(',')[0].strip()
x = float(line.split(',')[1].strip())
y = float(line.split(',')[2].strip())
# Init a Point object
point = Point(id, x, y)
# Add the Point object to the list of test points
test_points.append(point)
print("categorize points")
test_points_categories = []
for id, test_point in enumerate(test_points):
category = find_category(polygen, test_point)
test_points_categories.append(category)
print("write output.csv")
with open('output.csv', 'w') as f:
f.write('id,category\n')
for i, category in enumerate(test_points_categories):
f.write(f'{i+1},{category}\n')
print("plot polygon and points")
polygen_points = polygen.get_points()
xs = []
ys = []
for polygen_point in polygen_points:
xs.append(polygen_point.get_x())
ys.append(polygen_point.get_y())
plotter.add_polygon(xs, ys)
for id, test_point in enumerate(test_points):
plotter.add_point(test_point.get_x(), test_point.get_y(), test_points_categories[id])
plotter.show()
if __name__ == "__main__":
main()