2015-08-25 54 views
4

这可能是一个非常简单的问题,但似乎我无法看到它。 我有顺时针点有序的列表,并希望根据this使用下面的函数来计算这些点的重心(凸多边形):Python如何处理复杂的计算?

enter image description here enter image description here

enter image description here

def calculateCentroid(raLinks,raNodes, links, nodes): 

orderedPointsOfLinks = orderClockwise(raLinks,raNodes, links, nodes) 

arg1 = 0 
arg2 = 0 
Xc = 0 
Yc = 0 
i = 0 
for point in orderedPointsOfLinks: 
    arg1 += point.Y*(orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].X) 
    arg2 += (orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].Y)*point.X 
    Xc += (point.X+(orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].X))*(((orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].Y)*point.X)-(point.Y*(orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].X))) 
    Yc += (point.Y+(orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].Y))*(((orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].Y)*point.X)-(point.Y*(orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].X)))  
    i+=1 

area = (arg1-arg2)*0.5 
print area 

X = -Xc/(6*area) 
Y = -Yc/(6*area) 
print X , " ", Y 

使用Arcpy计算面积和centorid表明计算的ar ea通过上述函数是正确的,但质心是错误的。

Xc和Yc有什么问题,我无法修复它?

如果我以下列方式改变为循环工作原理:

for point in orderedPointsOfLinks: 
     y0 = point.Y 
     x0 = point.X 
     x1 = orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].X 
     y1 = orderedPointsOfLinks[i+1 if i+1<len(orderedPointsOfLinks) else 0].Y 
     a = x0*y1 - x1*y0 
     area += a 
     Xc += (x0+x1)*a 
     Yc += (y0+y1)*a 
     i+=1 
    area *= 0.5 
    print area 

    X = Xc/(6*area) 
    Y = Yc/(6*area) 
    print X , " ", Y 

这里是节点的列表来检查代码:

[(371623.876, 6159668.714),(371625.994, 6159661.094), (371624.319, 6159654.634), (371619.654, 6159649.86), (371614.194, 6159647.819), (371608.401, 6159648.449), (371601.544, 6159652.652), (371598.77, 6159658.058), (371599.318, 6159665.421), (371603.025, 6159671.805), (371611.372, 6159674.882), (371619.417, 6159673.065)] 
+1

个人而言,我会找到我的代码*很多*更容易阅读,如果我附加了第一点的副本到的结束所以我不必在每次想参考第(i + 1)点时重新输入(正确)那个环绕测试。这就是维基百科所做的。另外,为什么不迭代我的列表长度(-1,如果你追加第一个点到最后),并始终使用我的索引?我试图从你的代码中解码你的表达式,但放弃了。 – barny

+0

@ barny:你说得对,我可以用其他方式做,在列表末尾添加第一个点或在for循环结束后添加它。但是使用列表理解可以更容易地理解代码(可能效率更高),因为您不需要额外的步骤,只需要检查是否在代码中,考虑第一个关闭德路。 – msc87

+1

这是您的代码,但我的0.01美元表示您的代码几乎难以辨认,难以辨认的代码难以理解,调试和维护。 “简化”for循环遍历点列表使您可以:a)编写代码来初始化,使用和维护循环计数器i,b)每次需要i + 1时必须手动将代码绕回到第一个节点 - 如果i + 1 barny

回答

2

source

尝试:

import numpy 

tp = [(371623.876, 6159668.714),(371625.994, 6159661.094), (371624.319, 6159654.634), (371619.654, 6159649.86),\ 
     (371614.194, 6159647.819), (371608.401, 6159648.449), (371601.544, 6159652.652), (371598.77, 6159658.058), \ 
     (371599.318, 6159665.421), (371603.025, 6159671.805), (371611.372, 6159674.882), (371619.417, 6159673.065),(371623.876, 6159668.714)] 



# cx = sigma (x[i]+x[i+1])*((x[i]*y[i+1]) - (x[i+1]*y[i])) 
# cy = sigma (y[i]+y[i+1])*((x[i]*y[i+1]) - (x[i+1]*y[i])) 
cx = 0 
cy = 0 

p = numpy.array(tp) 

x = p[:, 0] 
y = p[:, 1] 

a = x[:-1] * y[1:] 
b = y[:-1] * x[1:] 

cx = x[:-1] + x[1:] 
cy = y[:-1] + y[1:] 



tp = tp[:-1] #dont need repeat 


def area(): 
    tox=0 
    toy=0 
    for i in range(len(tp)): 
     if i+1 == len(tp): 
      tox += tp[-1][0]*tp[0][1] 
     else: 
      tox += tp[i][0]*tp[i+1][1] 

    for i in range(len(tp)): 
     if i+1 == len(tp): 
      toy += tp[-1][1]*tp[0][0] 
     else: 
      toy += tp[i][1]*tp[i+1][0] 
    return abs(tox-toy)*0.5 

ar = area() 
Cx = abs(numpy.sum(cx * (a - b))/(6. * ar)) 
Cy = abs(numpy.sum(cy * (a - b))/(6. * ar)) 
print Cx,Cy 

警告!

tp[0] == tp[-1] 

所以:第一和最后一个坐标是相同的值...

+0

我对这个地区没有问题。问题在于计算坐标,因为它在区域正确时返回错误值。我怀疑python处理复杂方程式的方式是个问题,因为现在和接下来坐标值的变化都是不合理的,如果点列表被修复的话! – msc87

+0

@ msc87更新代码! – dsgdfg

+0

@ msc87在立陶宛的地方? – dsgdfg