def simpleTest(): width = 50 height = 100 #points = np.array([ # [0.2, 0.3], # [0.7, 0.5], # [0.6,0.2], # [0.3, 0.9], # [0.8, 0.1] #]) points = sampleRandomPoints(100, width, height) #np.random.rand(20, 2) print("Points:") print(points) cells = bounded_voronoi(points, [0, width, 0, height]) for i, cell in enumerate(cells): print("cell", i, "with point", cell.generator()) print(cell.corner()) # example density map density = np.zeros((width, height)) for x in range(width): for y in range(height): density[x, y] = 1 - math.sqrt((x / width - 0.5)**2 + (y / height - 0.5)**2) # compute cell integrals cell_densities = [cell.integrate(density) for cell in cells] # plotting plt.figure(figsize=(20, 10)) ax1 = plt.subplot(1, 2, 1) ax1.imshow(density, cmap=cm.hot) ax2 = plt.subplot(1, 2, 2) for cell, (d, _) in zip(cells, cell_densities): vertices = np.array(list(cell.corner()) + [cell.corner()[0]]) ax2.fill(vertices[:, 0], vertices[:, 1], facecolor=cm.hot(d)) ax2.plot([cell.generator()[0] for cell in cells], [cell.generator()[1] for cell in cells], 'b.') ax2.plot([d[1][0] for d in cell_densities], [d[1][1] for d in cell_densities], 'r.') for cell in cells: ax2.plot(cell.corner()[:, 0], cell.corner()[:, 1], 'go') for cell in cells: vertices = np.array(list(cell.corner()) + [cell.corner()[0]]) ax2.plot(vertices[:, 0], vertices[:, 1], 'k-') #fig.show() plt.savefig("bounded_voronoi.png")
def redo_voronoi(self): bound = self.boundary() points = map(lambda x: (x.x, x.y), self.stones) self.voronoi = bounded_voronoi(bound, points)
def init_voronoi(self): bound = self.boundary() points = [] self.voronoi = bounded_voronoi(bound, points)
def simpleOptim(): N = 200 width = 100 height = 100 iterations = 10 # create density density = np.zeros((width, height)) for x in range(width): for y in range(height): density[x, y] = (1 - math.sqrt(2) * math.sqrt((x / width - 0.5)**2 + (y / height - 0.5)**2))**2 # create algorithms algs = [WeightedLloyd(), WeightedLindeBuzoGray()] algNames = ["Lloyd", "Linde-Buzo-Gray"] init_points = sampleRandomPoints(N, width, height) for alg in algs: alg.init(density, N, init_points) plt.figure(figsize=(5, 5)) ax1 = plt.subplot(1, 1, 1) ax1.imshow(density, cmap=cm.hot) plt.savefig("stipplingDensity.png") plt.close() # create animation def plot(cellsx, next_pointsx, algNames, filename): plt.figure(figsize=(10 * len(algs), 10)) for i in range(len(algs)): ax2 = plt.subplot(1, len(algs), i + 1) ax2.title.set_text(algNames[i]) ax2.set_xlim(0, width) ax2.set_ylim(0, height) cells = cellsx[i] ax2.plot([cell.generator()[0] for cell in cells], [cell.generator()[1] for cell in cells], 'b.') ax2.plot(next_pointsx[i][:, 0], next_pointsx[i][:, 1], 'r.') for cell in cells: vertices = np.array(list(cell.corner()) + [cell.corner()[0]]) ax2.plot(vertices[:, 0], vertices[:, 1], 'k-') plt.savefig(filename) plt.close() #plot(algs, algNames, "stippling%02d.png"%0) for i in range(iterations): cellsx = [] next_pointsx = [] for alg, algName in zip(algs, algNames): points = alg.points() cells = bounded_voronoi(points, [0, width, 0, height]) converged = alg.step() next_points = alg.points() cellsx.append(cells) next_pointsx.append(next_points) print(algName, "converged:", converged) plot(cellsx, next_pointsx, algNames, "stippling%02d.png" % i)