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

  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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值