def testVoronoi(self): """ I ve tested how voronoi package works but it has problems when dataset of points is greater than 20 """ print("test using voronoi lib") self._loadMap() # print("len self map:", len(self.map)) print(self.map) raw_points = self.map points = self.fuse( raw_points.copy(), 6 ) # here is magic of rounding problem is that i have to specify round radius (2nd param) by my self # Define a bounding box polygon = Polygon([(0.0, 0.0), (0.0, 400.0), (400.0, 0.0), (400.0, 400.0)]) # Initialize the algorithm v = Voronoi(polygon) # Create the diagram v.create_diagram(points=points, vis_steps=False, verbose=False, vis_result=True, vis_tree=True) # Get properties edges = v.edges vertices = v.vertices arcs = v.arcs points = v.points print("voronoi properties:") print("edges:", edges) print("vertices:", vertices) print("arcs:", arcs) print("points:", points) print(v.points[0].get_coordinates()) for point in v.points: print(f"{(point.x, point.y)} \t {point.cell_size()}")
from voronoi import Voronoi from voronoi.graph.bounding_circle import BoundingCircle # Define a set of points points = [ (1, 2), (7, 13), (12, 6), (5, 5), ] # Define a bounding circle bounding_circle = BoundingCircle(5., 5., 9.) v = Voronoi(bounding_circle) v.create_diagram(points=points, vis_before_clipping=True, vis_steps=True, verbose=True, vis_result=True, vis_tree=True) edges = v.edges vertices = v.vertices arcs = v.arcs points = v.points
callback=lambda observer, figure: figure.savefig(f"output/voronoi/{observer.n_messages:02d}.png") ) ) # Attach observer that visualizes the tree every step. This is a binary tree data structure that keeps track of where # the arcs and the breakpoints between the arcs are going. v.attach_observer( TreeObserver( # Callback that saves the figure every step # If no callback is provided, it will render the figure in a window callback=lambda observer, dot: dot.render(f"output/tree/{observer.n_messages:02d}") ) ) # Attach a listener that listens to debug messages. # If no callback is provided, it will print the messages. v.attach_observer(DebugObserver(callback=lambda _: print(_))) # Create the output directory if it doesn't exist if not os.path.exists("output"): os.mkdir("output") if not os.path.exists("output/tree/"): os.mkdir("output/tree/") if not os.path.exists("output/voronoi/"): os.mkdir("output/voronoi/") # Create the Voronoi diagram v.create_diagram(points=points)
# Initialize the algorithm v = Voronoi(triangle) # Optional: attach observer that visualizes Voronoi diagram every step v.attach_observer( VoronoiObserver( # Settings to put into the visualizer settings=dict(polygon=True, edges=True, vertices=True, sites=True, outgoing_edges=False, border_to_site=False, scale=1, edge_labels=False, site_labels=False, triangles=False, arcs=False), # Callback that saves the figure every step callback=lambda observer, figure: figure.savefig( f"output/{observer.n_messages:02d}.png"))) # Make the output directory if not os.path.exists("output"): os.mkdir("output") # Start the procedure v.create_diagram(points=[(p.xd, p.yd) for p in points], )
polygon = Polygon([ (2.5, 10), (5, 10), (10, 5), (10, 2.5), (5, 0), (2.5, 0), (0, 2.5), (0, 5), ]) # Initialize the algorithm v = Voronoi(polygon) # Create the diagram v.create_diagram(points=points, vis_steps=False, verbose=False, vis_result=True, vis_tree=True) # Get properties edges = v.edges vertices = v.vertices arcs = v.arcs points = v.points # Calculate the sell size for each point for point in v.points: print(f"{(point.x, point.y)} \t {point.cell_size()}") # for point in v.points: # print([(round(p.x, 2), round(p.y, 2)) for p in point.get_coordinates()]) print(v.points[0].get_coordinates())