def generate_map(self, size=75, freq=20, lloyds=2, sigma=3.15, seed=None): """Initializes the map and generates the base noise and voronoi diagram""" # make up data points size_sqrt = size size = size**2 # compute Voronoi tesselation if seed is not None: np.random.seed(seed) points = np.random.random((size, 2)) vor = VoronoiDiagram(points) vor.generate_voronoi() self.voronoi_diagram = vor.relax_points(lloyds) regions, vertices = voronoi_finite_polygons_2d(self.voronoi_diagram) polygons = sorted(clip(points, regions, vertices), key=lambda x: x[0][0]) polygons_stripped = [[ vert for vert in poly if 0 <= vert[0] <= 1 and 0 <= vert[1] <= 1 ] for poly in polygons] self.polygons = [Polygon(i) for i in polygons_stripped] # Get noise, turn into 1D array noise = Perlin(freq, shuffle_seed=seed) noise_img = noise.create_image(save=True, width=200, height=200) noise_gauss = noise.gaussian_kernel(noise_img, nsig=sigma) noise_gauss.save("images/noise.png") noise_resized = noise_gauss.resize((size_sqrt, size_sqrt)) noise_arr = np.array(noise_resized) self._generate_base_terrain(noise_arr)
def frosted_glass(img_path, reduced_height=500): image = Image.open(img_path) width, height = image.size resize_height = reduced_height resize_width = round(resize_height * (width / height)) image = image.resize((resize_width, resize_height)) dummy_img = Image.new('RGB', (resize_width, resize_height)) draw = ImageDraw.Draw(dummy_img) num_of_points = round((resize_width * resize_width) / 100) vor = VoronoiDiagram(resize_width, resize_height, num_of_points) vor.optimize() for group in vor.coor_groupings: num_of_coors = max(1, len(group)) avg_red, avg_green, avg_blue = 0, 0, 0 for coor in group: red, green, blue = image.getpixel(coor) avg_red += red avg_green += green avg_blue += blue avg_red /= num_of_coors avg_green /= num_of_coors avg_blue /= num_of_coors new_color = (round(avg_red), round(avg_green), round(avg_blue)) for coor in group: draw.point(coor, new_color) return dummy_img
def auto_cleanup(sites, main_index): # test automatic cleanup: assert len(sites) > 1 d = Delaunay(test = True) d.include(sites[main_index]) for site in sites: if site == sites[main_index]: continue d.include_near(site, sites[main_index]) VoronoiDiagram.show(d, title = "Automatic 'main site' cleanup mode")
def manual_cleanup(sites, main_index): # test manual cleanup: assert len(sites) > 1 d = Delaunay(test = True) d.include(sites[main_index]) for site in sites: if site == sites[main_index]: continue d.include(site) d.remove_far_sites(sites[main_index]) VoronoiDiagram.show(d, title = "Manual 'main site' cleanup mode")
def simple(sites, main_index): # simple test without cleanup: assert len(sites) > 1 d = Delaunay(test = True) d.include(sites[main_index]) for site in sites: if site == sites[main_index]: continue d.include(site) if not d.fail_site is None: Preview.voronoi(d, title = "Fail") VoronoiDiagram.show(d, title = "Without cleanup")
def initializer(self): """ Initialize the host algorithm """ for node in self.network.nodes(): node.memory['neighbors_data'] = {} node.points = [(int(node.network.pos[node][0]), int(node.network.pos[node][1]))] node.voronoi = Delaunay() VoronoiDiagram.new_diagram(node.voronoi, str(node.id)) node.voronoi.include(node.points[0]) super(DistributedVoronoi, self).initializer()
def new_thread(points, delay, n, lines, columns, fps = 0, test_class = DiagramTest): # test multi-thread execution: VoronoiDiagram.start(title = test_class.__name__ + " - New-thread test", lines = lines, columns = columns, fps_limit = fps) # configure threads = [] # create tests in different threads for i in xrange(0,n): test = test_class(points, delay) threads.append(test) VoronoiDiagram.new_diagram(test.diagram, label="test") test.start() # waiting until previewer terminates while VoronoiDiagram.in_state(Preview.RUNNING): pass # stop tests threads for t in threads: t.stop()
def multi_circles(width, length, num_of_points=random.randint(3, 7), num_of_colors=random.randint(4, 10), optimize=False): vrn = VoronoiDiagram(width, length, num_of_points) if optimize: vrn.optimize() points = vrn.feature_points points_to_corners = [] corners = [(0, 0), (width, length), (width, 0), (0, length)] for corner in corners: min_dist = None for point in points: cur_dist = euclidean_distance(point[0], point[1], corner[0], corner[1]) if min_dist is None: min_dist = cur_dist elif cur_dist < min_dist: min_dist = cur_dist points_to_corners.append(min_dist) max_dist = math.ceil(max(points_to_corners)) colors = RGB.n_random_rbg(num_of_colors) gradient = create_color_gradient(colors, max_dist) img = Image.new('RGB', (width, length), gradient[-1]) draw = ImageDraw.Draw(img) for idx in range(max_dist - 1, -1, -1): cur_color = gradient[idx] for point in points: x = point[0] y = point[1] coors = (x - idx, y - idx, x + idx, y + idx) draw.ellipse(coors, cur_color) return img
import tkinter from voronoi import VoronoiDiagram root = tkinter.Tk() root.state('zoomed') w, h = root.winfo_screenwidth(), root.winfo_screenheight() root.geometry("%dx%d+0+0" % (w, h)) voronoi_diagram = VoronoiDiagram(root, width=w, height=h, borderwidth=0, highlightthickness=0, bg="black") voronoi_diagram.pack() root.update() voronoi_diagram.init_diagram() def task(): voronoi_diagram.delete("all") voronoi_diagram.render() root.after(10, task) # reschedule event in 2 seconds root.after(10, task) root.mainloop()
import logging.config from pymote.networkgenerator import NetworkGenerator from pymote.simulation import Simulation from pymote.npickle import write_npickle from pymote.conf import global_settings from distributed_voronoi import DistributedVoronoi from point2D import Point2D from voronoi import VoronoiDiagram # Do not show log logging.config.dictConfig({'version': 1,'loggers':{}}) VoronoiDiagram.start() global_settings.ENVIRONMENT2D_SHAPE = VoronoiDiagram.panel_dim() # generates the network with 10 hosts net_gen = NetworkGenerator(n_count=99, n_min=1, n_max=100) net = net_gen.generate_random_network() # Defines the network algorithm net.algorithms = ((DistributedVoronoi, {'informationKey':'axis'}),) # Assign to node memory its position for node in net.nodes(): node.memory['axis'] = (int(net.pos[node][0]), int(net.pos[node][1]))
y_offset = Y_OFFSET + ((x + 1) % 2) * Y_SHIFT points.append( Vector2( x * X_DELTA + gauss(0, X_SD) + x_offset, y * Y_DELTA + gauss(0, Y_SD) + y_offset, )) # Instead place points by uniform random sampling? if False: points = [] for i in range(N_X * N_Y): x = randint(0, 1000) y = randint(0, 900) points.append(Vector2(x, y)) voronoi = VoronoiDiagram(points) start_ix = voronoi.create_maze() def main(): draw_points = False draw_triangulation = False draw_circumcircles1 = False draw_circumcircles2 = False draw_voronoi_cells = False draw_voronoi_connections = True draw_maze_edges = True pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT))