如何在Python中创建用于存储多边形的x和y坐标的数组
问题描述 投票:0回答:4
因此,我试图从文件中读取多边形的描述,并给出方向及其移动到目前为止,我的方法是btw(我是python的菜鸟,而c ++具有背景,因此代码可能有很多语法错误)与c ++的相似性]def Area(A):
n = len(A) # of corners
area = 0.0
j = n - 1
for i in range(n):
area += (A[0][j] + A[0][i]) * (A[1][j] - A[1][i])
area = abs(area) / 2.0
#reading from the file and connecting the polygen in a vector array in terms of X and Y coordinates
file = input('Enter file name:')
with open(file, 'r') as f:
file = f.read()
fileContent =file
A = [[],[]] #trying to create an vector of x and y ordinates
xa = 0.0
yb = 0.0
data = ""
if fileContent > data and data == "(":
data.join(fileContent)
if data == "N":
data.join(fileContent)
if data != ",":
print("Description is wrong!")
if file>temp:
yb += temp
A[1].append(yb) #Storing the value in y co ordinate of vector A
A[0].append(xa) #Storing the value in x co ordinate of vector A
if data == "W":
data.join(fileContent)
if data != ",":
print("Description is wrong!")
if file > temp:
xa -= temp
A[1].append(yb)
A[0].append(xa)
if data == "E":
data.join(fileContent)
if data != ",":
print("Description is wrong!")
if file > temp:
xa += temp
A[1].append(yb)
A[0].append(xa)
if data == "S":
data.join(fileContent)
if data != ",":
print("Description is wrong!")
if file > temp:
xa -= temp
A[1].append(yb)
A[0].append(xa)
#If polygon is complete A CONDITION on completeion of polygen in c++ if(x.back()==0 && y.back()==0)
p= polygonArea(A)
print ("area = ",p)
arrays
4个回答
1
投票
[这里,我定义了一个函数,该函数将吸收多边形的长度,从长度为17.5的垂直线处的线开始,并顺时针进行:
1
投票
这可以帮助您入门;这显示了从文件中读取元素。关于处理“每个”列表,您应该能够填写其余的内容。在这里,我们将根据“)”字符分割输入,然后将每个分割成可以使用的格式。您需要N-S-E-W,还有一个值,对吗?可能希望使用.upper或其他调整项,以便您不必担心大小写('e'与'E')...
1
投票
这里是一个完整的解决方案。首先,我们使用正则表达式解析数据,找到基本方向并在每对括号内移动。
0
投票
如果可以获取从任何顶点开始的所有点的顺时针或逆时针方向的坐标,则可以使用以下公式:
热门问题