-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoronoid.py
150 lines (96 loc) · 3.77 KB
/
voronoid.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
from bisector import *
from line_intersection import *
from dcel import *
from xygraph import *
from sklearn.neighbors import NearestNeighbors
import numpy as np
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
import numpy as np
import math
import matplotlib.patches as patches
from PIL import Image
import numpy as np
import matplotlib
from PIL import Image, ImageDraw
#matplotlib.use('GTK3Cairo')
def voronoid(points, xmin=None, xmax=None, ymin=None, ymax=None):
if xmin==None:
xygraph = Xygraph(vl=points)
xmin = xygraph.xmin - 1
xmax = xygraph.xmax + 3
ymin = xygraph.ymin - 1
ymax = xygraph.ymax + 1
# Ensure there is no points in the same location
points = list(set(points))
n = len(points)
# Sort points in advance by the distance to the l
v = (xmin, ymin)
points.sort(key=lambda p: ((p[0]-v[0])**2 +
(p[1]-v[1])**2)**(1/2), reverse=True)
# Points displayed
cur_points = []
p = points.pop()
cur_points.append(p)
cur_points = cur_points
V = Dcel(vl=[(xmin, ymax), (xmin, ymin), (xmax, ymin), (xmax, ymax)], el=[
(0, 1), (1, 2), (2, 3), (3, 0)], site=p, border=[xmin, xmax, ymin, ymax])
for i in range(n-1):
p = points.pop()
cur_points.append(p)
# Closest site to the new site p_i+1
nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(
np.array(cur_points))
indices = nbrs.kneighbors(
np.array(p).reshape(1, -1), return_distance=False)
pc = cur_points[indices[0][1]]
# face associated with pc
fn = V.getFace(pc)
p1, q1 = perpendicular_bisector(p, pc, xmin, xmax, ymin, ymax)
intersect_vl = []
intersect_edges = {}
vertices = [v.coord for v in V.vertices]
move = -10e-2
m = {}
findEdge = True
eps = 10e-3
for h in fn.hedges:
m[h] = slope(h.v1.coord, h.origin.coord)
if doIntersect(p1, q1, h.vertices[0].coord, h.vertices[1].coord):
# Find the intersection between the bisector and the intersect line
pt = intersection(
p1, q1, h.vertices[0].coord, h.vertices[1].coord)
# Handle the intersect vertex and is the same as the existing vertex
if pt in vertices and findEdge:
shift = 0.0000000001
pt_before = pt
pt = intersection(
p1, q1, h.vertices[0].coord, h.vertices[1].coord, shift)
if abs(pt_before[0]-pt[0]) > eps:
if pt_before[0]-pt[0] > 0:
pt = (-eps, pt[1])
else:
pt = (eps, pt[1])
if not isOnLine(pt, h):
continue
else:
findEdge = False
vertex = Vertex(pt[0], pt[1])
intersect_vl.append(vertex)
intersect_edges[vertex] = h
V.update(p, pc, intersect_vl, intersect_edges, xmin, xmax, ymin, ymax)
regions = []
for f in V.faces.values():
region = []
for v in f.vertices:
try:
region.append(v.coord)
except:
pass
regions.append(list(set(region)))
return xmin, xmax, ymin, ymax, np.array(cur_points),np.array(regions)
if __name__ == '__main__':
np.random.seed(10)
points = np.random.randint(0, 10, (5, 2))
points = [(x[0], x[1]) for x in points]
voronoid(points)