-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawvoronoid.py
81 lines (65 loc) · 2.44 KB
/
drawvoronoid.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
import numpy as np
from voronoid import *
from drawvoronoid import *
import os
import sys
# decorater used to block function printing to the console
def blockPrinting(func):
def func_wrapper(*args, **kwargs):
# block all printing to the console
sys.stdout = open(os.devnull, 'w')
# call the method in question
value = func(*args, **kwargs)
# enable all printing to the console
sys.stdout = sys.__stdout__
# pass the return value of the method back
return value
return func_wrapper
@blockPrinting
def findRegion(points, xmin=None, xmax=None, ymin=None, ymax=None):
new_regions = []
xmin, xmax, ymin, ymax, finalpoints, regions = voronoid(
points, xmin, xmax, ymin, ymax)
regions = np.asarray(regions)
for region in regions:
region = np.asarray(region)
c = region.mean(axis=0)
angles = np.arctan2(region[:, 1] - c[1], region[:, 0] - c[0])
region = np.array(region)[np.argsort(angles)]
#polygon = vertices[region]
new_regions.append(region)
return new_regions, finalpoints, xmin, xmax, ymin, ymax
def plotVoronoi(points, xmin=None, xmax=None, ymin=None, ymax=None):
regions, finalpoints, xmin, xmax, ymin, ymax = findRegion(
points, xmin, xmax, ymin, ymax)
for region in regions:
plt.fill(*zip(*region), alpha=0.4)
for ver in region:
plt.annotate('({:.2f},{:.2f})'.format(
ver[0], ver[1]), (ver[0], ver[1]))
for p in finalpoints:
plt.annotate('({:.2f},{:.2f})'.format(p[0], p[1]), (p[0], p[1]))
plt.plot(finalpoints[:, 0], finalpoints[:, 1], 'ko')
fig = plt.gcf()
#for p in finalpoints:
# plt.annotate('({:.2f},{:.2f})'.format(p[0],p[1]), (p[0],p[1]))
plt.xlim(xmin - 0.1, xmax + 0.1)
plt.ylim(ymin - 0.1, ymax + 0.1)
plt.show()
if __name__=='__main':
np.random.seed(11)
points = np.random.randint(0, 10, (5, 2))
points = [(x[0], x[1]) for x in points]
#points = [(0, 1), (1, 8), (9, 0), (9, 4), (10, 10)]
#n = len(points)
points = list(set(points))
xygraph = Xygraph(vl=points)
xmin = xygraph.xmin - 1
ymin = xygraph.ymin - 1
v = (xmin, ymin)
points.sort(key=lambda p: ((p[0]-v[0])**2 +
(p[1]-v[1])**2)**(1/2), reverse=True)
cur_points = []
for i in range(n):
cur_points.append(points.pop())
plotVoronoi(cur_points, -1, 13, -1, 11)