[英]Creating coordinates for polygon using Python lists
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.到目前为止,我已经编写了一些代码,但仅限于 X 轴,我认为可以改进。
My desired output should be two nested lists, one for X and one for Y. There should be 25 polygons (5x5):我想要的输出应该是两个嵌套列表,一个用于 X,一个用于 Y。应该有 25 个多边形(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.
我怎样才能使它工作,以便它也可以在 Y 轴上制作多边形。
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)
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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.