地理空间-Java实现航迹稀释

打印 上一主题 下一主题

主题 1770|帖子 1770|积分 5310

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Java实现航迹点稀释算法(Douglas - Peucker算法)的示例代码,该算法可在保证航迹团体外形变化不大的情况下减少航迹点数量:

import java.util.ArrayList;
import java.util.List;
 
class Point {
    double x;
    double y;
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
 
public class TrackThinning {
 
    public static List<oint> douglasPeucker(List<oint> points, double epsilon) {
        if (points.size() < 3) {
            return points;
        }
 
        int index = -1;
        double dmax = 0;
        int end = points.size();
        for (int i = 1; i < end - 1; i++) {
            double d = perpendicularDistance(points.get(i), points.get(0), points.get(end - 1));
            if (d > dmax) {
                index = i;
                dmax = d;
            }
        }
 
        List<oint> result = new ArrayList<>();
        if (dmax > epsilon) {
            List<oint> recursiveResult1 = douglasPeucker(points.subList(0, index + 1), epsilon);
            List<oint> recursiveResult2 = douglasPeucker(points.subList(index, points.size()), epsilon);
 
            result.addAll(recursiveResult1.subList(0, recursiveResult1.size() - 1));
            result.addAll(recursiveResult2);
        } else {
            result.add(points.get(0));
            result.add(points.get(points.size() - 1));
        }
 
        return result;
    }
 
    private static double perpendicularDistance(Point point, Point start, Point end) {
        double dx = end.x - start.x;
        double dy = end.y - start.y;
        double numerator = Math.abs((dy * point.x - dx * point.y) + (end.x * start.y - end.y * start.x));
        double denominator = Math.sqrt(dy * dy + dx * dx);
        return numerator / denominator;
    }
 
    public static void main(String[] args) {
        List<oint> points = new ArrayList<>();
        points.add(new Point(0, 0));
        points.add(new Point(1, 1));
        points.add(new Point(2, 2));
        points.add(new Point(3, 2));
        points.add(new Point(4, 3));
        points.add(new Point(5, 4));
 
        double epsilon = 0.5;
        List<oint> thinnedPoints = douglasPeucker(points, epsilon);
 
        for (Point p : thinnedPoints) {
            System.out.println("(" + p.x + ", " + p.y + ")");
        }
    }
}
 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

吴旭华

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表