多边形求面积编程
題目:
給出一個簡單多邊形(沒有缺口),它的邊要么是垂直的,要么是水平的。要求計算多邊形的面積。 多邊形被放置在一個 X-Y 的卡笛爾平面上,它所有的邊都平行于兩條坐標軸之一。 然后按逆時針方向給出各頂點的坐標值。所有的坐標值都是整數(因此多邊形的面積也為整數) 輸入描述: 第一行給出多邊形的頂點數 n(n≤100) 。接下來的幾行每行給出多邊形一個頂點的坐標值 X 和 Y (都為整數并且用空格隔開)。頂點按逆時針方向逐個給出。 并且多邊形的每一個頂點的坐標值 -200≤x,y≤200 。多邊形最后是靠從最后一個頂點到第一個頂點畫一條邊來封閉的。 輸出描述: 一個整數,表示多邊形的面積。 示例 示例1 輸入 10 0 0 4 0 4 1 3 1 3 3 2 3 2 2 1 2 1 3 0 3輸出 9 示例2 輸入 10 0 0 1 0 1 5 2 5 2 2 4 2 4 3 6 3 6 6 0 6輸出 21?代碼
public class duobianxing1 {public static void main(String[] args) {Scanner scan = new Scanner(System.in);String str_0 = scan.nextLine().trim();int n = Integer.parseInt(str_0);ArrayList<ArrayList<Integer>> vector = new ArrayList<>();for (int i = 0; i < n; i++) {String str_1 = scan.nextLine();String[] line_list_1 = str_1.trim().split(" ");ArrayList<Integer> temp_1 = new ArrayList<>();for (int j = 0; j < line_list_1.length; j++) {temp_1.add(Integer.parseInt(line_list_1[j]));}vector.add(temp_1);}scan.close();int result = solution(n, vector);System.out.println(result);}public static int solution(int n, ArrayList<ArrayList<Integer>> vector) {int result = 0,j;// TODO: 請在此編寫代碼 簡寫for(int i=0;i<n;i++){j=(i+1)%n;result += vector.get(i).get(0)*vector.get(j).get(1)-vector.get(j).get(0)*vector.get(i).get(1);}return Math.abs(result)/2;} }面積公式找到了
?找某個大神證明公式(豎著看)
?
總結
                            
                        - 上一篇: 基于One—stage的目标检测算法综述
 - 下一篇: 分面导航的详细操作方案