java 任意多边形面积计算
生活随笔
收集整理的這篇文章主要介紹了
java 任意多边形面积计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考文章中的介紹方法任意多邊形面積計算公式
用代碼的形式表達
/*** 根據鞋帶定理計算* @author YiDianDian*/ public class Area {public static void main(String[] args) {Point point1 = new Point(3,4);Point point2 = new Point(5,11);Point point3 = new Point(12,8);Point point4 = new Point(9,5);Point point5 = new Point(5,6);List<Point> list = new ArrayList<>();list.add(point1);list.add(point2);list.add(point3);list.add(point4);list.add(point5);System.out.println(getArea(list)); //30}public static double getArea(List<Point> pointList){double area = 0;for (int i = 1; i <= pointList.size(); i++) {double X = pointList.get(i - 1).x;double Y = pointList.get(i - 1).y;double nextX = pointList.get(i % pointList.size()).x;double nextY = pointList.get(i % pointList.size()).y; // if (i == pointList.size()) { // nextX = pointList.get(0).x; // nextY = pointList.get(0).y; // } else { // nextX = pointList.get(i).x; // nextY = pointList.get(i).y; // }double temp = X * nextY - nextX * Y;area += temp;}area = Math.abs(area/2.0);return area;}@Data@Builder(toBuilder = true)@AllArgsConstructorstatic class Point {private double x;private double y;} }總結
以上是生活随笔為你收集整理的java 任意多边形面积计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Performing Basic Ama
- 下一篇: 牛客网——华为题库(91~100)