道格拉斯-普克 DOUGLAS-PEUKER(DP算法) JAVA实现


1、道格拉斯-普克抽稀算法说明

道格拉斯-普克抽稀算法是用来对大量冗余的图形数据点进行压缩以提取必要的数据点。
该算法实现抽稀的过程是:
1)对曲线的首末点虚连一条直线,求曲线上所有点与直线的距离,并找出最大距离值dmax,用dmax与事先给定的阈值D相比: 
2)若dmax  若dmax≥D,保留dmax对应的坐标点,并以该点为界,把曲线分为两部分,对这两部分重复使用该方法,即重复1),2)步,直到所有dmax均显然,本算法的抽稀精度也与阈值相关,阈值越大,简化程度越大,点减少的越多,反之,化简程度越低,点保留的越多,形状也越趋于原曲线。 

2、举例说明

输出结果:1.0 4.0,4.0 2.0,7.0 7.0,9.0 5.0,10.0 10.0,其中2 3,6 6,8 6两个坐标被抽稀掉了。

示意图:

假设在平面坐标系上有一条由N个坐标点组成的曲线,已设定一个阈值epsilon。
(1)首先,将起始点与结束点用直线连接, 再找出到该直线的距离最大,同时又大于阈值epsilon的点并记录下该点的位置(这里暂且称其为最大阈值点),如图所示:

(2)接着,以该点为分界点,将整条曲线分割成两段(这里暂且称之为左曲线和右曲线),将这两段曲线想象成独立的曲线然后重复操作(1),找出两边的最大阈值点,如图所示:

(3)最后,重复操作(2)(1)直至再也找不到最大阈值点为止,然后将所有最大阈值点按顺序连接起来便可以得到一条更简化的,更平滑的,与原曲线十分近似的曲线,如图所示:


3、JAVA代码实现

Point类:

  1.   public class Point {
  2.   double x;
  3.   double y;
  4.    
  5.   public Point(int x, int y) {
  6.   this.x = x;
  7.   this.y = y;
  8.   System.out.print("(" + x + "," + y + ") ");
  9.   }
  10.    
  11.   public static Point instance(int x, int y) {
  12.   return new Point(x, y);
  13.   }
  14.   }

DouglasPeuckerUtil 类:

  1.   public class DouglasPeuckerUtil {
  2.    
  3.   public static void main(String[] args) {
  4.    
  5.   System.out.print("原始坐标:");
  6.    
  7.   List points = new ArrayList<>();
  8.   List result = new ArrayList<>();
  9.    
  10.   points.add(Point.instance(1, 1));
  11.   points.add(Point.instance(2, 2));
  12.   points.add(Point.instance(3, 4));
  13.   points.add(Point.instance(4, 1));
  14.   points.add(Point.instance(5, 0));
  15.   points.add(Point.instance(6, 3));
  16.   points.add(Point.instance(7, 5));
  17.   points.add(Point.instance(8, 2));
  18.   points.add(Point.instance(9, 1));
  19.   points.add(Point.instance(10, 6));
  20.    
  21.   System.out.println("");
  22.   System.out
  23.   .println("=====================================================================");
  24.   System.out.print("抽稀坐标:");
  25.    
  26.   result = DouglasPeucker(points, 1);
  27.    
  28.   for (Point p : result) {
  29.   System.out.print("(" + p.x + "," + p.y + ") ");
  30.   }
  31.   }
  32.    
  33.   public static List DouglasPeucker(List points, int epsilon) {
  34.   // 找到最大阈值点,即操作(1)
  35.   double maxH = 0;
  36.   int index = 0;
  37.   int end = points.size();
  38.   for (int i = 1; i < end - 1; i++) {
  39.   double h = H(points.get(i), points.get(0), points.get(end - 1));
  40.   if (h > maxH) {
  41.   maxH = h;
  42.   index = i;
  43.   }
  44.   }
  45.    
  46.   // 如果存在最大阈值点,就进行递归遍历出所有最大阈值点
  47.   List result = new ArrayList<>();
  48.   if (maxH > epsilon) {
  49.   List leftPoints = new ArrayList<>();// 左曲线
  50.   List rightPoints = new ArrayList<>();// 右曲线
  51.   // 分别提取出左曲线和右曲线的坐标点
  52.   for (int i = 0; i < end; i++) {
  53.   if (i <= index) {
  54.   leftPoints.add(points.get(i));
  55.   if (i == index)
  56.   rightPoints.add(points.get(i));
  57.   } else {
  58.   rightPoints.add(points.get(i));
  59.   }
  60.   }
  61.    
  62.   // 分别保存两边遍历的结果
  63.   List leftResult = new ArrayList<>();
  64.   List rightResult = new ArrayList<>();
  65.   leftResult = DouglasPeucker(leftPoints, epsilon);
  66.   rightResult = DouglasPeucker(rightPoints, epsilon);
  67.    
  68.   // 将两边的结果整合
  69.   rightResult.remove(0);//移除重复点
  70.   leftResult.addAll(rightResult);
  71.   result = leftResult;
  72.   } else {// 如果不存在最大阈值点则返回当前遍历的子曲线的起始点
  73.   result.add(points.get(0));
  74.   result.add(points.get(end - 1));
  75.   }
  76.   return result;
  77.   }
  78.    
  79.   /**
  80.   * 计算点到直线的距离
  81.   *
  82.   * @param p
  83.   * @param s
  84.   * @param e
  85.   * @return
  86.   */
  87.   public static double H(Point p, Point s, Point e) {
  88.   double AB = distance(s, e);
  89.   double CB = distance(p, s);
  90.   double CA = distance(p, e);
  91.    
  92.   double S = helen(CB, CA, AB);
  93.   double H = 2 * S / AB;
  94.    
  95.   return H;
  96.   }
  97.    
  98.   /**
  99.   * 计算两点之间的距离
  100.   *
  101.   * @param p1
  102.   * @param p2
  103.   * @return
  104.   */
  105.   public static double distance(Point p1, Point p2) {
  106.   double x1 = p1.x;
  107.   double y1 = p1.y;
  108.    
  109.   double x2 = p2.x;
  110.   double y2 = p2.y;
  111.    
  112.   double xy = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  113.   return xy;
  114.   }
  115.    
  116.   /**
  117.   * 海伦公式,已知三边求三角形面积
  118.   *
  119.   * @param cB
  120.   * @param cA
  121.   * @param aB
  122.   * @return 面积
  123.   */
  124.   public static double helen(double CB, double CA, double AB) {
  125.   double p = (CB + CA + AB) / 2;
  126.   double S = Math.sqrt(p * (p - CB) * (p - CA) * (p - AB));
  127.   return S;
  128.   }

输出结果: