Voronoi module¶
-
class
Voronoi.Voronoi(num_district, bounds, seed=None)¶ Bases:
objectGenerates and stores a Voronoi diagram as a graph and list of polygons
- Attributes
- seedsList
List of points which are the seeds for the voronoi diagram
- polygonsList
List of polygons where each polygon is one voronoi cell
- graphnx.Graph
The graph representation of the voronoi ridges
- num_districtint
The number of districts in the map, or the number of seed points
- boundsPolygon
The bounding polygon
Methods
Uses scipy and QHull to generate a voronoi diagram based off the the seeds from generate_seeds, then build a NetworkX graph and list of polygons to match.
Take the graph of the Voronoi diagram and generate the polygon tesselation of the plane.
generate_seeds([seed])Generate random Points which are used as seeds in the construction of a voronoi diagram.
relax()Finds the centroid of each polygons and sets that to be the seed point of the next generation of voronoi diagram
-
generate_graph()¶ Uses scipy and QHull to generate a voronoi diagram based off the the seeds from generate_seeds, then build a NetworkX graph and list of polygons to match.
Notes
For vertices with two defined endpoint, creating the graph is not hard. However, for vertices with an infinite end the process is more complex.
From the definition of a voronoi ridge, it is the perpendicular bisector of the line segment between two voronoi points, and thus a tangent line. Model the tangent as a vector, using a Point object to store the end of the vector, assuming the other end is at the origin. Normalize it into a unit vector and find the normal vector, which has an x coordinate equal to the negative y of the tangent and a y coordinate equal to the x coordinate of the tangent.
Now we have a vector which is perpendicular to the segment between the voronoi points, but because this is a ray, we need to account for which direction the infinite end extents. It can NEVER go towards the origin as that it where the center of the diagram is and the most other points. So we find the midpoint between the two voronoi points of interest, also modelling it as a vector like the tangent. Because the midpoint actually starts at the origin, unlike the tangent which originates from one of the voronoi points, the midpoint vector will always point away from the origin.
The dot product of two vectors is \(a \cdot b = ||a|| \times ||b|| \times \cos(\theta)\) where \(\theta\) is the angle between the two vectors. We do not care about the product of the magnitudes, but we can use the cosine to our advantage. The cosine is positive if \(\theta\) is within \(\pm\ \frac{\pi}{2}\) radians. We need the direction of this ray to be no more than \(\frac{\pi}{2}\) radians from the midpoint vector or else it is closer to pointing at the origin and thus pointing in the wrong direction. If the dot product between the normal vector and the midpoint vector is negative, be multiply the normal vector by negative 1 to invert the direction it points.
Finally we take a large value that is outside the display range of the map, scale the voronoi directional vector by that amount and then add it to the position of the known end of the ray.
-
generate_polygons()¶ Take the graph of the Voronoi diagram and generate the polygon tesselation of the plane.
Stores the polygons in a class attribute list
-
generate_seeds(seed=None)¶ Generate random Points which are used as seeds in the construction of a voronoi diagram.
The notes are more concentrated in the middle of the map.
-
relax()¶ Finds the centroid of each polygons and sets that to be the seed point of the next generation of voronoi diagram
-
Voronoi.bfs_path(G, source, destination)¶ Use a breadth first search to find the path from vertex source to vertex destination.
- Parameters
- Gnx.Graph
The graph to search
- sourcePoint
Origin point
- destinationPoint
Destination point
- Returns
- deque
A queue with the path from source to destination already enqueued.
-
Voronoi.print_dict(dict_name, points)¶
-
Voronoi.print_list(list_name, points)¶
-
Voronoi.remove_vertices(G, vertices)¶ Remove all vertices in vertices from the graph G
- Parameters
- Gnx.Graph
The graph from which vertices will be removed
- verticeslist
The list of vertices in G to be removed
- Returns
- nx.Graph
A copy of the graph G without the vertices listed in vertices