简体   繁体   English

使用 Python 列表为多边形创建坐标

[英]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.

相关问题 Python中逆时针方向的多边形坐标列表排序 - Sorting lists of polygon coordinates in counter-clockwise direction in Python 使用 Python Geopandas 从 Shapefile 中查找多边形坐标 - Find Polygon Coordinates from Shapefile using Python Geopandas 使用 Python 和 BeautifulSoup 抓取多边形的坐标 - Webscraping coordinates of a polygon with Python and BeautifulSoup 使用列表Python创建矩阵 - Creating a matrix using lists Python Python包/函数使用地理坐标获取另一个多边形中一个多边形覆盖的百分比面积 - Python package/function to get percentage area covered by one polygon in another polygon using geo coordinates 找到包含给定坐标的多边形并找到多边形的坐标(python opencv) - find the polygon enclosing the given coordinates and find the coordinates of polygon (python opencv) 使用角坐标收缩多边形 - Shrink polygon using corner coordinates 是否有将坐标转换为创建圆形多边形的坐标列表的功能? - Is there a function to convert coordinates to a list of coordinates creating a circular polygon? 比较python中的两个坐标列表并使用坐标值来指定值 - Comparing two lists of coordinates in python and using coordinate values to assign values 使用 python 创建一个覆盖其他多边形层的多边形 - creating a polygon that covers other polygons layer using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM