# Douglas-Peucker Algorithm in Java

«
»
This is a Java Program to implement Douglas-Peucker Algorithm. The Ramer–Douglas–Peucker algorithm (RDP) is an algorithm for reducing the number of points in a curve that is approximated by a series of points. This algorithm is also known under the names Douglas–Peucker algorithm, iterative end-point fit algorithm and split-and-merge algorithm. The purpose of the algorithm is, given a curve composed of line segments, to find a similar curve with fewer points. The algorithm defines ‘dissimilar’ based on the maximum distance between the original curve and the simplified curve. The simplified curve consists of a subset of the points that defined the original curve.

Here is the source code of the Java Program for Douglas-Peucker Algorithm Implementation. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `//This is a java program to filter out points using Douglas Peucker Algorithm`
2. `import static java.lang.Math.abs;`
3. `import static java.lang.Math.pow;`
4. `import static java.lang.Math.sqrt;`
5. ` `
6. `import java.util.Random;`
7. ` `
8. `class RamerDouglasPeuckerFilter`
9. `{`
10. ` `
11. `    private double epsilon;`
12. ` `
13. `    public RamerDouglasPeuckerFilter(double epsilon)`
14. `    {`
15. `        if (epsilon <= 0)`
16. `        {`
17. `            throw new IllegalArgumentException("Epsilon nust be > 0");`
18. `        }`
19. `        this.epsilon = epsilon;`
20. `    }`
21. ` `
22. `    public double[] filter(double[] data)`
23. `    {`
24. `        return ramerDouglasPeuckerFunction(data, 0, data.length - 1);`
25. `    }`
26. ` `
27. `    public double getEpsilon()`
28. `    {`
29. `        return epsilon;`
30. `    }`
31. ` `
32. `    protected double[] ramerDouglasPeuckerFunction(double[] points,`
33. `            int startIndex, int endIndex)`
34. `    {`
35. `        double dmax = 0;`
36. `        int idx = 0;`
37. `        double a = endIndex - startIndex;`
38. `        double b = points[endIndex] - points[startIndex];`
39. `        double c = -(b * startIndex - a * points[startIndex]);`
40. `        double norm = sqrt(pow(a, 2) + pow(b, 2));`
41. `        for (int i = startIndex + 1; i < endIndex; i++)`
42. `        {`
43. `            double distance = abs(b * i - a * points[i] + c) / norm;`
44. `            if (distance > dmax)`
45. `            {`
46. `                idx = i;`
47. `                dmax = distance;`
48. `            }`
49. `        }`
50. `        if (dmax >= epsilon)`
51. `        {`
52. `            double[] recursiveResult1 = ramerDouglasPeuckerFunction(points,`
53. `                    startIndex, idx);`
54. `            double[] recursiveResult2 = ramerDouglasPeuckerFunction(points,`
55. `                    idx, endIndex);`
56. `            double[] result = new double[(recursiveResult1.length - 1)`
57. `                    + recursiveResult2.length];`
58. `            System.arraycopy(recursiveResult1, 0, result, 0,`
59. `                    recursiveResult1.length - 1);`
60. `            System.arraycopy(recursiveResult2, 0, result,`
61. `                    recursiveResult1.length - 1, recursiveResult2.length);`
62. `            return result;`
63. `        } else`
64. `        {`
65. `            return new double[] { points[startIndex], points[endIndex] };`
66. `        }`
67. `    }`
68. ` `
69. `    public void setEpsilon(double epsilon)`
70. `    {`
71. `        if (epsilon <= 0)`
72. `        {`
73. `            throw new IllegalArgumentException("Epsilon nust be > 0");`
74. `        }`
75. `        this.epsilon = epsilon;`
76. `    }`
77. ` `
78. `}`
79. ` `
80. `public class Douglas_Peucker_Algorithm`
81. `{`
82. `    public static void main(String args[])`
83. `    {`
84. `        Random random = new Random();`
85. `        double[] points = new double;`
86. `        double[] fpoints;`
87. `        for (int i = 0; i < points.length; i++)`
88. `            points[i] = random.nextInt(10);`
89. `        RamerDouglasPeuckerFilter rdpf = new RamerDouglasPeuckerFilter(1);`
90. `        fpoints = rdpf.filter(points);`
91. ` `
92. `        System.out.println("Orginal points");`
93. `        for (int i = 0; i < points.length; i++)`
94. `            System.out.print(points[i] + " ");`
95. ` `
96. `        System.out.println("\nFiltered points");`
97. `        for (int i = 0; i < fpoints.length; i++)`
98. `            System.out.print(fpoints[i] + " ");       `
99. `    }`
100. `}`

Output:

```\$ javac Douglas_Peucker_Algorithm.java
\$ java Douglas_Peucker_Algorithm

Orginal points
5.0 0.0 8.0 7.0 2.0 9.0 4.0 4.0 0.0 7.0 4.0 1.0 9.0 6.0 8.0 9.0 6.0 6.0 9.0 6.0
Filtered points
5.0 0.0 8.0 2.0 9.0 0.0 7.0 1.0 9.0 6.0 9.0 6.0 9.0 6.0```

Sanfoundry Global Education & Learning Series – 1000 Java Programs. 