计算多边形面积
1.多边形计算面积模型
2.代码
public class Point{
public int x;
public int y;
Point(){
this.x=0;
this.y=0;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
import java.util.ArrayList; import java.util.List; public class CaculateArea { public static float CalculateArea(List<Point> vectorPoints) { int i, iCount; i = 0; float iArea = 0; iCount = vectorPoints.size(); for (i = 0; i < iCount; i++) { iArea = iArea + (vectorPoints.get(i).getX() * vectorPoints.get((i + 1) % iCount).getY() - vectorPoints.get((i + 1) % iCount).getX() * vectorPoints.get(i).getY()); } return (float)Math.abs(0.5 * iArea); } public static void main(String args[]) { List<Point> vectorPoints = new ArrayList<Point>(); Point p1 = new Point(); p1.setX(1); p1.setY(1); Point p2 = new Point(); p2.setX(0); p2.setY(2); Point p3 = new Point(); p3.setX(3); p3.setY(3); Point p4 = new Point(); p4.setX(4); p4.setY(1); vectorPoints.add(p1); vectorPoints.add(p2); vectorPoints.add(p3); vectorPoints.add(p4); System.out.println("多边形面积是:"+CalculateArea(vectorPoints)); } }
3 运行结果
多边形面积是:5.0