The
Source code is as follows:
Package Com.zznode.tnms.douglas; Import java.awt.*; Import Java.util.Random; Import Javax.swing.JFrame; /** * @author Weih * @date Oct/public class Polycompress extends JFrame {private static final int number = 50 ;//The number of original curve nodes private static final int tolerance = 10;//compression distance threshold private int[] source_x = new int[number];//original curve node's coordinate PRI Vate int[] source_y = new int[number];//original curve node ordinate private int[] result_x;//storage compressed curve node horizontal axis private int[] result_y;//storage compression After the curve node ordinate private int[] index = new int[number];//record the position of the node in the original curve node coordinate array private int count = 0;//reserved number of nodes private int wid th, height; Public polycompress () {setSize); SetBackground (Color.White); Dimension srcsize = Toolkit.getdefaulttoolkit (). Getscreensize (); width = getwidth (); Height = getheight (); SetLocation ((srcsize.width-width)/2, (Srcsize.height-height)/2); SetVisible (TRUE); Random Random = new Random (); Source_x[0] = 50; SOURCE_Y[0] = 100; for (int i = 1; i < number; i++) {Source_x[i] = source_x[I-1] + random.nextint (10); Source_y[i] = + random.nextint (50); } System.out.println ("Raw data point:"); for (int i = 0; i < number; i++) {System.out.println ("source_xy[" + i + "]:" + source_x[i] + "," + Source_y[i ");} Keep the end and end nodes of the original curve. index[count++] = 0; index[count++] = NUMBER-1; Call recursive function compress (0, NUMBER-1); Sort (); result_x = new Int[count]; result_y = new Int[count]; for (int i = 0; i < count; i++) {result_x[i] = Source_x[index[i]]; Result_y[i] = source_y[index[i]]; System.out.println ("The number of nodes in the original curve is:" +); System.out.println ("Node choice threshold is:" + tolerance); System.out.println ("The number of nodes in the compressed curve is:" + count); System.out.println (the position of the reserved node in the original Curve node data is as follows: "); SYSTEM.OUT.PRINTLN ("The compression ratio is:" + * count/number + "%"); public double distance (int start, int ' O, int current) {Double A = (double) (Source_y[end]-source_y[start]); double b = (double) (Source_x[end]-source_x[start]); Double c = (double) (Source_y[end]-source_y[start])-(double) (Source_x[end]-source_x[start)); DoubLe dist = Math.Abs (A * source_x[current] + b * Source_y[current] + c)/math.sqrt (A * a + b * b); return dist; @Override public void Paint (Graphics g) {super.paint (g);//Draw the original curve g.setcolor (Color.gray); g.drawline (0, width, 10 0); G.setcolor (color.red); G.drawpolyline (source_x, source_y, number); If the number of nodes after compression is not 0, draw the compressed curve if (count!= 0) {g.setcolor (Color.gray); G.drawline (0, M, width); G.setcolor (Color.green) ; G.drawpolyline (result_x, result_y, Count); } public void Sort () {to (int i = 0; i < count; i++) {for (int j = i + 1; j < Count; J +) {if (Index[j] < I Ndex[i]) {int temp = Index[j]; Index[j] = Index[i]; index[i] = temp;} } public void compress (int i, int j) {Double temp_dist double max = 0; int temp_p = 0; for (int k = i + 1; k < J; k + +) {temp_dist = distance (I, j, K); if (Max < temp_dist) {max = temp_dist; temp_p = k;}} if (Max > tolerance) { index[count++] = temp_p; Compress (i, temp_p); Compress (Temp_p, J); }} public static void Main (string[] args) {polycompress pc = new polycompress ();}