@@ -35,8 +35,8 @@ private static final double distanceToSegmentSquared(double px, double py, doubl
3535 return distanceBetweenPoints (px , py , (vx + t * (wx - vx )), (vy + t * (wy - vy )));
3636 }
3737
38- private static final double perpendicularDistance (Double [] v , Double [] w , Double [] p ) {
39- return Math .sqrt (distanceToSegmentSquared (p [ 0 ], p [ 1 ], v [ 0 ], v [ 1 ], w [ 0 ], w [ 1 ] ));
38+ private static final double perpendicularDistance (double px , double py , double vx , double vy , double wx , double wy ) {
39+ return Math .sqrt (distanceToSegmentSquared (px , py , vx , vy , wx , wy ));
4040 }
4141
4242 private static final List <Double []> douglasPeucker (List <Double []> list , int s , int e , double epsilon ) {
@@ -47,7 +47,16 @@ private static final List<Double[]> douglasPeucker(List<Double[]> list, int s, i
4747 final int start = s ;
4848 final int end = e -1 ;
4949 for (int i =start +1 ; i <end ; i ++) {
50- double d = perpendicularDistance (list .get (start ), list .get (end ), list .get (i ));
50+ // Point
51+ final double px = list .get (i )[0 ];
52+ final double py = list .get (i )[1 ];
53+ // Start
54+ final double vx = list .get (start )[0 ];
55+ final double vy = list .get (start )[1 ];
56+ // End
57+ final double wx = list .get (end )[0 ];
58+ final double wy = list .get (end )[1 ];
59+ final double d = perpendicularDistance (px , py , vx , vy , wx , wy );
5160 if (d > dmax ) {
5261 index = i ;
5362 dmax = d ;
@@ -77,6 +86,13 @@ private static final List<Double[]> douglasPeucker(List<Double[]> list, int s, i
7786 return resultList ;
7887 }
7988
89+ /**
90+ * Given a curve composed of line segments find a similar curve with fewer points.
91+ *
92+ * @param list List of Double[] points (x,y)
93+ * @param epsilon Distance dimension
94+ * @return Similar curve with fewer points
95+ */
8096 public static final List <Double []> douglasPeucker (List <Double []> list , double epsilon ) {
8197 return douglasPeucker (list , 0 , list .size (), epsilon );
8298 }
0 commit comments