java 多边形外扩

该博客介绍了如何使用Java实现地图上的折线坐标进行内缩和外扩操作。通过定义Point2D类和PolygonOutlineUtil工具类,实现了向量运算和新顶点计算,提供了一种从原始顶点坐标生成新顶点坐标的解决方案。主要方法包括计算两顶点间向量差、单位化向量和计算新顶点坐标。示例代码展示了如何对外轮廓进行0.01的外扩操作。
摘要由CSDN通过智能技术生成

地图围栏坐标内陷外廓,目前搜索到很多Swift和c++的没有java的,所以我觉得翻译一下
参考:折线平行线的计算方法
https://blog.csdn.net/happy__888/article/details/315762

不废话直接上代码:

public class Point2D {
    public double x=0.0;
    public double y=0.0;
    Point2D(double x,double y){
        this.x = x;
        this.y = y;
    }

    @Override
    public String toString() {
        return "Point2D{" +
                "x=" + x +
                ", y=" + y +
                '}';
    }
}
public class PolygonOutlineUtil {

    //+
    public static Point2D plus(Point2D left,Point2D right){
        return new Point2D(left.x+right.x, left.y+right.y);
    }
    //-
    public static Point2D reduce(Point2D left,Point2D right) {
        return new Point2D(left.x-right.x, left.y-right.y);
    }
    //value*
    public static Double rideDouble(Point2D left, Point2D right) {
        return left.x*right.x + left.y*right.y;
    }
    //*
    public static Point2D ride(Point2D left, Double value){
        return new Point2D(left.x*value, left.y*value);
    }

    // 自定义的向量差乘运算符号 **
    public static Double operator (Point2D left, Point2D right) {
        return left.x*right.y - left.y*right.x;
    }
    public static List<Point2D> pList = new ArrayList<>(); // 原始顶点坐标, 在initPList函数当中初始化赋值
    public static List<Point2D> dpList = new ArrayList<>();// 边向量dpList[i+1]- dpLIst[i] 在 initDPList函数当中计算后赋值
    public static List<Point2D> ndpList = new ArrayList<>(); // 单位化的边向量, 在initNDPList函数当中计算后肤质,实际使用的时候,完全可以用dpList来保存他们
    public static List<Point2D> newList = new ArrayList<>();  // 新的折线顶点,在compute函数当中,赋值


    // 初始化顶点队列
    public static void initPList(String str){
         String[] split = str.split(";");
        for(String point:split){
            String[] split1 = point.split(",");
            pList.add(new Point2D(Double.valueOf(split1[0]), Double.valueOf(split1[1])));
        }
    }


    // 初始化dpList  两顶点间向量差
    public static void initDPList(){
        System.out.println("计算两顶点间向量差:dpList");
        for (int index=0; index<pList.size();index++){
            int i= index==pList.size()-1 ? 0: index+1;
            dpList.add(reduce(pList.get(index==pList.size()-1 ? 0: index+1),pList.get(index)));
        }
    }



    // 初始化ndpList,单位化两顶点向量差
    public static void initNDPList(){
        System.out.println("开始计算单位化两顶点向量差:ndpList");
        for(int index =0; index<dpList.size();index++) {
            ndpList.add(ride(dpList.get(index),( 1.0 / Math.sqrt(rideDouble(dpList.get(index),dpList.get(index))))));
        }
    }

    // 计算新顶点, 注意参数为负是向内收缩, 为正是向外扩张
    public static void computeLine(Double dist){
        System.out.println("开始计算新顶点");
        int count = pList.size();
        for (int index = 0 ; index<count; index++) {
            int startIndex = index==0 ? count-1 : index-1;
            int endIndex   = index;
            Double sina = operator(ndpList.get(startIndex),ndpList.get(endIndex));
            Double length = dist / sina;
            Point2D vector = reduce(ndpList.get(endIndex),ndpList.get(startIndex));
            newList.add(plus(pList.get(index),ride(vector,length)));
        }
    }

    // 整个算法的调用顺序
    public static void main(String[] args) {
        String str = "30.280196656341005,101.70201860368253;31.17443221616588,102.44943223893644;31.612013574947458,105.5988347902894;30.606585065678747,105.95238883048297";
        initPList(str);
        initDPList();//0.005
        initNDPList();
        computeLine(0.01D);//正数为外廓,负数为内陷
        newList.forEach(e->{
			System.out.println("{lat: "+ e.x +", lng: "+e.y+"},");
        });
    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最清晰的脚印.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值