Thursday, February 2, 2017

Voting Districts Day 5: The district/distance matrix

Next, I'm going to build a matrix showing the distance between each voting district and the start points.  

To figure out the distance between the two points, I'm going to attempt to use the pythagorean theorem (a² + b² = c²) where a equals the difference between the x coordinates and b equals the difference between the y coordinates.  This'll be a measurement "as the crow flies." 

Our point shared by the x and y axis will have the same latitude as one point and the same longitude as the other point to make a right triangle.  To calculate the distance for the x and y lines, we'll subtract the latitudes and longitudes of the points between the centroid and the starting point.  

(voting_district_x - starting_point_x)² + (voting_district_y - starting_point_y)² = (the_distance between the points)²

OR

math.sqrt(((start_points[a][0] - coords[b][2]) ** 2) + ((start_points[a][1] - coords[b][5]) ** 2))

Now I just have to do a double loop to build the matrix showing the distance between each voting district and starting point like so:

matrix = list()

for a in range(0, len(start_points)):
    point_line = list()
    for b in range(0, len(coords)):
        point_line.append(math.sqrt(((start_points[a][0] - coords[b][2]) ** 2) + ((start_points[a][1] - coords[b][5]) ** 2)))
    matrix.append(point_line)

And that should give me a nice matrix that I can run a draft through to assign voting districts to starting points!  That's what we're going to work on next.  

No comments: