Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,当多边形中的第一个点与最后一个点相交时,如何找到停止while循环的方法?_Python - Fatal编程技术网

在python中,当多边形中的第一个点与最后一个点相交时,如何找到停止while循环的方法?

在python中,当多边形中的第一个点与最后一个点相交时,如何找到停止while循环的方法?,python,Python,我需要只使用坐标来计算多边形的周长 我的职能: def definePerimeter(xCoords, yCoords): i = 0 sum = 0 while xCoords[i] != xCoords[i+1] and yCoords[i] != yCoords[i+1]: dx = xCoords[i] - xCoords[i+1] dy = yCoords[i] - yCoords[i+1] dsquared = dx**2 + dy**2 resu

我需要只使用坐标来计算多边形的周长

我的职能:

def definePerimeter(xCoords, yCoords):

i = 0
sum = 0
while xCoords[i] != xCoords[i+1] and yCoords[i] != yCoords[i+1]:
    dx = xCoords[i] - xCoords[i+1]
    dy = yCoords[i] - yCoords[i+1]
    dsquared = dx**2 + dy**2
    result = math.sqrt(dsquared)
    print "The list of segments:"
    print "The segment: ", result
    i += 1
    sum = sum + result           
print  "Total 2D Perimeter is " , sum ,"m" *
给出了错误的周长(与ArcGIS相比)


在python中,当多边形中的第一个点与最后一个点相交时,如何找到停止while循环的方法?

您的算法不正确。首先,需要根据坐标与多边形质心所形成角度的反正切对坐标进行排序。(以获得形状中坐标的正确顺序)

然后,可以使用对坐标计算形状边的长度,并将所有边相加以获得周长:

def perimeter(coordinates):
    return sum(math.sqrt(pow(y2-y1,2)+pow(x2-x1,2)) for (x1,y1),(x2,y2) in zip(coordinates, coordinates[1:]))

看到while循环的情况,我猜最后一个和第一个坐标是相同的

x = [0,1,2,3,0]

y = [0,2,4,5,0]
我不认为while循环条件有任何其他意义。如果是这样,那么你应该试试

i = math.fmod((i+1),len(xCoords))

您不需要在这里执行
while
循环。您可以使用
for
循环来执行此操作,因为您要遍历多边形的所有顶点:

sum = 0
for i in xrange(len(xCoords) - 1):
      sum += np.sqrt((xCoords[i] - xCoords[i + 1]) ** 2) + (yCoords[i] -yCoords[i + 1]) ** 2))
sum+=np.sqrt((xCoords[0] - xCoords[-1]) ** 2) + (yCoords[0] -yCoords[-1]) ** 2))
如果坚持使用
while
循环执行此操作,则可以通过以下方式执行:

sum = 0
i = 0
while (i < len(xCoords) - 1):
          sum += np.sqrt((xCoords[i] - xCoords[i + 1]) ** 2) + (yCoords[i] -yCoords[i + 1]) ** 2))
          i += 1
sum+=np.sqrt((xCoords[0] - xCoords[-1]) ** 2) + (yCoords[0] -yCoords[-1]) ** 2))
sum=0
i=0
而(i
您能给我们一些
xCoords
yCoords
的硬接线值,以及它们的输出图片和任何错误的文本,而不是将其作为一个带有两个参数的函数来表示吗?当你说错误的周长时,你的意思是一个微小的舍入误差,可能会涉及一些看起来像投影的东西,或者更像是由于使用的跳线太少而被截断成三角形的正方形?尝试dx=float(…),dy=float(…),并用dxdx+dy*dy替换*2。我还将使用枚举(xCoords)中的I,x对不起,我看不出问题出在哪里。。所有列表都是有限的,它们从元素0开始,然后转到元素“n”。Esri中的多边形的最后一个点与其第一个点重合(位于其右上方并相同)。从多边形的零件获取阵列时,只需按范围(len(array)-1)迭代一次多边形。你能解释一下你是如何得到你的阵列的吗?也许问题就在这里。测试不应该针对coord[0]吗?我看到它是从gis堆栈迁移过来的,所以我假设它与gis相关。您是使用GDAL、shapely还是其他模块来阅读coords?多边形数据的格式是什么(shp、csv)?
sum = 0
i = 0
while (i < len(xCoords) - 1):
          sum += np.sqrt((xCoords[i] - xCoords[i + 1]) ** 2) + (yCoords[i] -yCoords[i + 1]) ** 2))
          i += 1
sum+=np.sqrt((xCoords[0] - xCoords[-1]) ** 2) + (yCoords[0] -yCoords[-1]) ** 2))