Mapping¶
District Mapping¶
Defines the algorithms that perform the mapping from precincts to districts.
Voronoi Mapping¶
-
class
distopia.mapping.voronoi.
VoronoiMapping
(**kwargs)[source]¶ Uses the Voronoi algorithm to assign precincts to districts.
-
add_fiducial
(location, identity)[source]¶ Adds a new fiducial at
location
.- Parameters
location – The fiducial location
(x, y)
.- Returns
The (assigned) ID of the fiducial.
-
assign_precincts_to_districts
(n_districts, pixel_district_map)[source]¶ Uses the pre-computed precinct and district maps and assigns all the precincts to districts.
Returns list of precincts, per district. Indices correspond with the district identity index in unique_ids as filled into pixel_district_map.
-
compute_district_pixels
(fiducials, fiducials_identity, unique_ids)[source]¶ Computes the assignment of pixels to districts and creates the associated districts.
fiducials and fiducials_identity must be sorted such that they correspond to each other. All ids must be in unique ids. pixel_district_map is filled in with the index of the district identity in unique_ids to make it 0-n-1.
-
districts
= []¶ A list of all current
distopia.district.District
instances.
-
fiducial_locations
= {}¶ dict of tuples, each containing the
(x, y)
coordinates of the fiducial at that key.
-
get_pos_district
(pos)[source]¶ The district under the fiducial.
- Parameters
pos – position.
- Returns
The district under the position, or None if none.
-
move_fiducial
(fiducial, location)[source]¶ Moves
fiducial
from its previous location to a new location.- Parameters
fiducial –
fiducial
ID as returned byadd_fiducial()
.location – The new fiducial location
(x, y)
.
-
pixel_district_map
= None¶ A width by height matrix, where each item is the district index in
districts
it belongs to. Read_only (used by the thread).
-
precincts
= []¶ A list of all the
distopia.precinct.Precinct
instances.
-
remove_fiducial
(fiducial)[source]¶ Removes
fiducial
from the diagram.- Parameters
fiducial –
fiducial
ID as returned byadd_fiducial()
.
-
screen_size
= (1920, 1080)¶ The size of the screen, in pixels, on which the districts and precincts are drawn, and on which the fiducials are drawn.
It’s
(width, height)
-
set_precincts
(precincts)[source]¶ Adds the precincts to be used by the mapping.
Must be called only (or every time) after
screen_size
is set.- Parameters
precincts – List of
distopia.precinct.Precinct
instances.
-
sites
= []¶ A list of tuples describing the
x
,y
coordinates of each of the objects placed by the user at that location.
-
voronoi_finite_polygons_2d
(vor)[source]¶ Reconstruct infinite voronoi regions in a 2D diagram to finite regions.
based on https://stackoverflow.com/a/20678647.
- vorVoronoi
Input diagram
- regionslist of tuples
Indices of vertices in each revised Voronoi regions.
- verticeslist of tuples
Coordinates for revised Voronoi vertices. Same as coordinates of input vertices, with ‘points at infinity’ appended to the end.
-
Collider¶
The collider module contains classes which can be used to test membership of a point in some space. See individual class documentation for details.

To use it you first have to cythonize the code. To do that, cd to the directory containing collider.pyx and:
python setup.py build_ext --inplace
-
class
distopia.mapping._voronoi.
PolygonCollider
¶ PolygonCollider checks whether a point is within a polygon defined by a list of corner points.
rect
is (x, y, width, height)Based on http://alienryderflex.com/polygon/
For example, a simple triangle:
>>> collider = PolygonCollider([10., 10., 20., 30., 30., 10.], ... cache=True) >>> (0.0, 0.0) in collider False >>> (20.0, 20.0) in collider True
The constructor takes a list of x,y points in the form of [x1,y1,x2,y2…] as the points argument. These points define the corners of the polygon. The boundary is linearly interpolated between each set of points. The x, and y values must be floating points. The cache argument, if True, will calculate membership for all the points so when collide_point is called it’ll just be a table lookup.
-
bounding_box
()¶ Returns the bounding box containing the polygon as 4 points (x1, y1, x2, y2), where x1, y1 is the lower left and x2, y2 is the upper right point of the rectangle.
-
get_inside_points
()¶ Returns a list of all the points that are within the polygon.
-
-
exception
distopia.mapping._voronoi.
ColliderException
¶