Reputation: 3885
I want to make an algorithm that will create coordinates (corner coordinates) for square polygons. See the picture below:
I have written some code so far, but only for the X-axis, and I think it could be improved.
My desired output should be two nested lists, one for X and one for Y. There should be 25 polygons (5x5):
X_list = [[0, 5, 5, 0], [5, 10, 10, 5], [10, 15, 15, 10], ...]
Y_list = [[0, 0, 5, 5] , [0, 0, 5, 5], [0, 0, 5, 5], ...]
This is the code that I have. How can I make it work, so that it can make polygons on Y-axis too.
max_x = 20
max_y = 20
x = 0
y = 0
xlist = []
ylist = []
lon = []
lad = []
while x < max_x and y < max_y:
xlist = []
ylist = []
x = x
y = y
xlist.append(x)
ylist.append(y)
x += 5
y = y
xlist.append(x)
ylist.append(y)
x = x
y += 5
xlist.append(x)
ylist.append(y)
x -= 5
y = y
xlist.append(x)
ylist.append(y)
x += 5
y -= 5
lon.append(xlist)
lad.append(ylist)
print(lon)
print(lad)
Upvotes: 2
Views: 241
Reputation: 3807
Here's a simple solution using list comprehensions.
x_count = 5
y_count = 5
step = 5
x_list = y_count * [[i*step,(i+1)*step,(i+1)*step,i*step] for i in range(x_count)]
y_list = [[i*step,i*step,(i+1)*step,(i+1)*step] for i in range(y_count) for j in range(x_count)]
Upvotes: 2