【问题标题】:Complex Polygon Area复杂多边形区域
【发布时间】:2013-04-18 03:35:47
【问题描述】:

cloneList = Point[](放入构造函数的一系列点) 我已经尝试了很多次来修复这个公式,但我想要。公式是在 http://en.wikipedia.org/wiki/Shoelace_formula index(i) 是一个同时具有 x 和 y 值的点。

public double getArea() {
    double area = 0;


    for (int i = 0; i < cloneList.length-1; i++){



    area += cloneList[i].getX()*cloneList[i+1].getY() - cloneList[i+1].getX()+cloneList[i].getY();

}


    area = area/2;
    //System.out.println(Math.abs(area));
return Math.abs(area);
}

【问题讨论】:

  • 当i = cloneList.length-1时,变量区加什么?提示:此时添加不正确。
  • 嗯...我不知道..

标签: java arrays area


【解决方案1】:

我不熟悉这个公式,但我会试一试... 在我看来,您似乎没有正确遵循公式,这将是我的实现(基于我从 wiki 页面 xD 收集的内容)

public double getArea(){
    double area = 0;
    int n = cloneList.length;
    double firstSum = 0;
    double secondSum = 0;
    for(int i = 0;i< cloneList.length - 1;i++){
        firstSum+= cloneList[i].getX()*cloneList[i+1].getY();
        secondSum+= cloneList[i+1].getX()*cloneList[i].getY();
    }
    firstSum+=cloneList[cloneList.length-1].getX()*cloneList[0].getY();
    secondSum-=cloneList[0].getX()*cloneList[cloneList.length-1].getY();

    double finalSum = firstSum-secondSum;
    area = Math.abs(finalSum)/2;
    return area;



}

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2012-03-02
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多