-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon_graphical_test.py
65 lines (50 loc) · 2.01 KB
/
polygon_graphical_test.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
import random
import argparse
from typing import Final
from matplotlib import pyplot
from matplotlib.patches import Polygon as PolygonPatch
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from alive_progress import alive_bar
# TODO: Have a look at this project as well: https://geopandas.org/en/stable/getting_started.html
# The diagram area
SIZE_X_AXIS: Final = 1
SIZE_Y_AXIS: Final = 1
# Results path
PDF_PATH: Final = 'output/points_in_polygon.pdf'
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--points', help='Number of points to test.', type=int)
parser.add_argument('-r', help='Record the result to a PDF file instead of just showing it on screen', action='store_true')
args = parser.parse_args()
# Number of random points
count_points: Final = args.points or 1000
# The polygon TODO: randomize this (can be hard to do though...)
polygon = Polygon([(0.1, 0.3), (0.1, 0.6), (0.4, 0.7), (0.8, 0.7), (0.8, 0.3), (0.5, 0.5)])
print('Prepare the pyplot figure...')
figure = pyplot.figure()
axes = figure.add_subplot(1, 1, 1, aspect='equal')
axes.set_title(f'Test the point_in_polygon function with \n {polygon}')
axes.set_xlim(0, SIZE_X_AXIS)
axes.set_ylim(0, SIZE_Y_AXIS)
print('Drawing the polygon...')
axes.add_patch(PolygonPatch(polygon.exterior.coords, linewidth=1, edgecolor='k', facecolor='none'))
print('Plotting the point...')
with alive_bar(count_points, bar='filling', spinner='waves') as bar:
for _ in range(count_points):
x = random.uniform(0, SIZE_X_AXIS)
y = random.uniform(0, SIZE_Y_AXIS)
point = Point(x, y)
if polygon.contains(point):
pyplot.plot(x, y, '.g')
else:
pyplot.plot(x, y, '.r')
bar()
if args.r:
print(f'Saving results in {PDF_PATH}')
figure.savefig(PDF_PATH)
else:
pyplot.show()
print('Done!')
if __name__ == '__main__':
main()