I'm relatively new to Python and having trouble figuring out how to populate a 2D array (a matrix) with output from a double for loop.
I have 24 shapely polygons in a list called "triangles". Some of these triangles are adjacent to each other, as you can see. I have plotted the centroids as numbers for reference.
I am interested in populating a matrix based on whether triangles are adjacent to each other or not. For example, polygon 0 is adjacent to polygons 1, 2, and 10. By hand, as a Boolean, the first line of this matrix would be:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 T T T
1
...
Rather than just a Boolean output, I'd like to get the index of the adjacent polygon. Like:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 1 2 10
1
...
I think I will need a double for loop that looks something like:
flower_matrix = []
for i in range(len(triangles)):
for j in range(len(triangles)):
if j is adjacent to i, then write index in matrix
I could do this by hand, but of course I'd rather not!
Any suggestions?! Thanks for looking!
The code to generate the triangles is below:
import matplotlib.pyplot as plt
from shapely.geometry import Point, LineString
from shapely.ops import unary_union, polygonize
from matplotlib.pyplot import cm
import numpy as np
def plot_coords(coords, color):
pts = list(coords)
x, y = zip(*pts)
# print(color)
plt.plot(x,y, color='k', linewidth=1)
plt.fill_between(x, y, facecolor=color)
def plot_polys(polys, color):
for poly, color in zip(polys, color):
plot_coords(poly.exterior.coords, color)
x = 0
y = 0
h = 1.73205080757
points = [# center
Point(x, y),
# first ring
Point((x + 2), y),
Point((x - 2), y),
Point((x + 1), (y + h)),
Point((x - 1), (y + h)),
Point((x + 1), (y - h)),
Point((x - 1), (y - h)),
# second ring
Point((x + 3), h),
Point((x - 3), h),
Point((x + 3), -h),
Point((x - 3), -h),
Point((x + 2), (h + h)),
Point((x - 2), (h + h)),
Point((x + 2), (-h + -h)),
Point((x - 2), (-h + -h)),
Point((x + 4), y),
Point((x - 4), y),
Point(x, (h + h)),
Point(x, (-h + -h)),
#third ring
Point((x + 4), (h + h)),
Point((x - 4), (h + h)),
Point((x + 4), (-h + -h)),
Point((x - 4), (-h + -h)),
Point((x + 1), (h + h + h)),
Point((x - 1), (h + h + h)),
Point((x + 1), (-h + -h + -h)),
Point((x - 1), (-h + -h + -h)),
Point((x + 5), h),
Point((x - 5), h),
Point((x + 5), -h),
Point((x - 5), -h)]
# buffer points to create circle polygons
circles = []
for point in points:
circles.append(point.buffer(2))
# unary_union and polygonize to find overlaps
rings = [LineString(list(pol.exterior.coords)) for pol in circles]
union = unary_union(rings)
result_polys = [geom for geom in polygonize(union)]
# remove tiny sliver polygons
threshold = 0.01
filtered_polys = [polygon for polygon in result_polys if polygon.area > threshold]
# remove outer circle fragments
complete_polys = [polygon for polygon in filtered_polys if (polygon.centroid.x**2 + polygon.centroid.y**2 < 4**2)]
print("total polygons = " + str(len(result_polys)))
print("filtered polygons = " + str(len(filtered_polys)))
print("complete polygons = " + str(len(complete_polys)))
centroidCoords = [
Point(0.9999999999999997, -0.5773635180985456),
Point(1.5000000000000004, -0.866025403785),
Point(1.9999999999999987, -1.154687289471454),
Point(0, -1.1546872894714544),
Point(0, -1.7320508075699992),
Point(-0.0000000000000002, -2.3094143256685467),
Point(-1.0000000000000004, -0.577363518098546),
Point(-1.5, -0.8660254037850001),
Point(-1.9999999999999996, -1.154687289471454),
Point(-1, 0.5773635180985459),
Point(-1.5000000000000004, 0.8660254037850001),
Point(-1.9999999999999993, 1.1546872894714537),
Point(-0.0000000000000001, 1.1546872894714542),
Point(0, 1.73205080757),
Point(0.0000000000000001, 2.309414325668546),
Point(1, 0.5773635180985456),
Point(1.5000000000000002, 0.8660254037849999),
Point(1.9999999999999993, 1.1546872894714542),
Point(2.9999999999999987, -0.5773635180985457),
Point(3.5, -0.8660254037849997),
Point(2, -1.73205080757),
Point(1.9999999999999996, -2.309414325668546),
Point(0.5, -0.8660254037850001),
Point(0.5, 0.866025403785),
Point(2, 1.7320508075700005),
Point(2.0000000000000004, 2.3094143256685467),
Point(3.0000000000000004, 0.5773635180985458),
Point(3.4999999999999996, 0.8660254037849999),
Point(-0.5000000000000001, -0.866025403785),
Point(-2.0000000000000004, -1.7320508075699999),
Point(-2.000000000000001, -2.3094143256685475),
Point(-3.000000000000001, -0.5773635180985459),
Point(-3.500000000000001, -0.866025403785),
Point(-3.0000000000000018, 0.5773635180985461),
Point(-3.4999999999999987, 0.8660254037849999),
Point(-2.0000000000000004, 1.73205080757),
Point(-1.9999999999999996, 2.3094143256685467),
Point(-0.5, 0.8660254037849999),
Point(2.5, 0.866025403785),
Point(1, 0),
Point(-0.5000000000000001, 2.5980762113550004),
Point(-0.9999999999999994, 2.886738097041452),
Point(1.0000000000000009, 2.8867380970414565),
Point(0.9999999999999997, 3.4641016151399993),
Point(2.5000000000000004, 2.598076211355),
Point(-0.9999999999999997, 0),
Point(-2.5, 0.8660254037850001),
Point(-2.500000000000001, 2.598076211355001),
Point(-0.9999999999999999, 3.4641016151399993),
Point(0.4999999999999998, 2.5980762113549987),
Point(2.5000000000000004, -2.598076211355),
Point(1.0000000000000002, -2.8867380970414547),
Point(1.0000000000000002, -3.4641016151400006),
Point(-0.5, -2.598076211355001),
Point(-0.9999999999999997, -2.8867380970414533),
Point(2.5000000000000004, -0.8660254037850001),
Point(0.5, -2.598076211355),
Point(-0.9999999999999992, -3.464101615139999),
Point(-2.5, -2.598076211355001),
Point(-2.5000000000000004, -0.8660254037850001),
Point(3, 0),
Point(1.5000000000000002, 2.5980762113550004),
Point(-3.0000000000000004, 0),
Point(-1.4999999999999998, 2.5980762113549987),
Point(1.4999999999999998, -2.598076211355001),
Point(-1.5, -2.598076211355)]
# separating into petals and triangles
limit = 0.66
triangles = [polygon for polygon in complete_polys if polygon.area < limit]
petals = [polygon for polygon in complete_polys if polygon.area > limit]
for i in range(len(triangles)):
plot_coords(triangles[i].exterior.coords, triangles_colors[i])
centroidCoords = triangles[i].centroid
plt.scatter(centroidCoords.x,centroidCoords.y, marker="$"+str(i)+"$")
print(len(triangles))
ax.set_aspect('equal')
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.