Java计算两点之间距离 & 三角形面积 & 多边形面积

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:53   2500   0
  1. 计算两个直接距离

       /**
         * 单位:米
         *
         * @param lon1
         * @param lat1
         * @param lon2
         * @param lat2
         * @return
         */
        public static double getDistance(double lon1, double lat1, double lon2, double lat2) {
            double radLat1 = rad(lat1);
            double radLat2 = rad(lat2);
            double a = radLat1 - radLat2;
            double b = rad(lon1) - rad(lon2);
            double c = 2 * Math.asin(Math.sqrt(
                    Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
            c = c * 6378.137;// 6378.137赤道半径
    
            return (Math.round(c * 10000d) / 10000d) * 1000;
        }
    
        private static double rad(double d) {
            return d * Math.PI / 180.0;
        }

  2. 三角形面积

        public static Double calculateArea(Loca A, Loca B, Loca C) {
            Double a = getDistance(A.getLongitude(), A.getLatitude(), B.getLongitude(), B.getLatitude());
            Double b = getDistance(B.getLongitude(), B.getLatitude(), C.getLongitude(), C.getLatitude());
            Double c = getDistance(C.getLongitude(), C.getLatitude(), A.getLongitude(), A.getLatitude());
            Double p = (a + b + c) / 2;
            Double area = Math.sqrt(p * Math.abs((p - a)) * Math.abs((p - b)) * Math.abs((p - c)));
            return area;
        }

  3. 多边形面积

    /**
         * 获取多边形面积,单位:平方米
         *
         * @param pointsStr
         * @return
         */
        public static Double getEnclArea(String pointsStr) throws Exception {
            JSONArray jsonArray = JSONArray.parseArray(pointsStr);
            Double Spoints = 0.0;
            if (jsonArray != null && jsonArray.size() > 2) {
                List<Loca> locas = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Loca loca = new Loca();
                    Double latitude = jsonObject.getDouble("latitude");
                    Double longitude = jsonObject.getDouble("longitude");
                    loca.setLatitude(latitude);
                    loca.setLongitude(longitude);
                    locas.add(loca);
                }
                Loca lowestPoint = locas.get(0);
                Integer lowestIndex = 0;
                List<Loca> lastList = new ArrayList<>();
                for (int i = 0; i < locas.size(); i++) {
                    Loca point = locas.get(i);
                    if (point.getLatitude() < lowestPoint.getLatitude()) {
                        lowestIndex = i;
                        lowestPoint = point;
                    }
                }
                System.out.println("lowestIndex:" + lowestIndex);
                for (int i = lowestIndex; i < locas.size(); i++) {
                    lastList.add(locas.get(i));
                }
                for (int i = 0; i < lowestIndex; i++) {
                    lastList.add(locas.get(i));
                }
    
                for (int i = 0; i < lastList.size() - 2; i++) {
                    Loca A = lastList.get(0);
                    Loca B = lastList.get(i + 1);
                    Loca C = lastList.get(i + 2);
                    if (A.getLongitude() == B.getLongitude() && A.getLongitude() == C.getLongitude()) {
                        //k1 、k2不存在
                        continue;
                    } else if (A.getLongitude() == B.getLongitude()) {
                        //k1不存在 k2存在
                        Double k2 = (C.getLatitude() - A.getLatitude()) / (C.getLongitude() - A.getLongitude());
                        if (k2 < 0) {//凹
                            Spoints -= calculateArea(A, B, C);
                        } else {//凸
                            Spoints += calculateArea(A, B, C);
                        }
                    } else if (A.getLongitude() == C.getLongitude()) {
                        //k1存在 k2不存在
                        Double k1 = (B.getLatitude() - A.getLatitude()) / (B.getLongitude() - A.getLongitude());
                        if (k1 > 0) {//凹
                            Spoints -= calculateArea(A, B, C);
                        } else {//凸
                            Spoints += calculateArea(A, B, C);
                        }
                    } else {
                        //k1、k2都存在
                        Double k1 = (B.getLatitude() - A.getLatitude()) / (B.getLongitude() - A.getLongitude());
                        Double k2 = (C.getLatitude() - A.getLatitude()) / (C.getLongitude() - A.getLongitude());
                        if (k1 > 0 && k2 < 0) {//凹
                            Spoints -= calculateArea(A, B, C);
                        } else if (k1 < 0 && k2 < 0) {//同号都为负数
                            if (k1 < k2) {//凹
                                Spoints -= calculateArea(A, B, C);
                            } else {//凸
                                Spoints += calculateArea(A, B, C);
                            }
                        } else if (k1 > 0 && k2 > 0) {//同号都为正数
                            if (k1 < k2) {//凹
                                Spoints -= calculateArea(A, B, C);
                            } else {//凸
                                Spoints += calculateArea(A, B, C);
                            }
                        } else {//其余情况都为凸
                            Spoints += calculateArea(A, B, C);
                        }
                    }
                }
            }
            DecimalFormat df = new DecimalFormat("#.00");
            return Double.valueOf(df.format(Math.abs(Spoints)));
        }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP