简体   繁体   English

如何使用坐标通过Simpson规则计算多边形面积?

[英]How to calculate Area of polygon by Simpson's rule using coordinates?

This code uses Simpson's rule , but gives the wrong area of a polygon by the formula: 此代码使用Simpson的规则 ,但通过公式给出多边形的错误区域:

在此输入图像描述

def defineArea(xCoords, yCoords):    

    i = 0
    sum = 0
    for i in xrange(len(xCoords) - 1):         
        result = (xCoords[i] - xCoords[i+1])*(yCoords[i]+ yCoords[i+1])

        i +=1
        sum = 0.5*(sum + result)
    print "Total 2D area is: ", sum*

What I am doing wrong? 我做错了什么? How do I calculate the area of polygon by Simpson's rule using only coordinates? 如何仅使用坐标计算Simpson规则的多边形面积?

Don't do sum = 0.5*(sum + result) inside the loop. 不要在循环内做sum = 0.5*(sum + result) Also, you don't need the sum variable at all once it's outside the loop. 此外,一旦它在循环外,你根本不需要sum变量。 Just do result = 0.5 * result after the loop is complete, then print result . 只需在循环完成后得到result = 0.5 * result ,然后打印result

You also need to switch the order of xCoords[i] and xCoords[i+1] . 您还需要切换xCoords[i]xCoords[i+1]的顺序。

Do what "Bill the Lizard" said (sorry I can't post a comment yet), or you can do it inside the loop too: 做“蜥蜴比尔”说的话(抱歉我还不能发表评论),或者你也可以在循环中做到这一点:

sum = sum + 0.5*result

Also, you are doing (xCoords[i] - xCoords[i+1]) instead of (xCoords[i+1] - xCoords[i]) 另外,你正在做(xCoords[i] - xCoords[i+1])而不是(xCoords[i+1] - xCoords[i])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM