Douglas-Peucker演算法的簡單實現(JAVA)

來源:互聯網
上載者:User

原始碼如下:

package com.zznode.tnms.douglas; import java.awt.*; import java.util.Random; import javax.swing.JFrame; /** * @author Weih * @date Oct 13, 2010 */ public class PolyCompress extends JFrame { private static final int NUMBER = 50;// 原始曲線節點數 private static final int TOLERANCE = 10;// 壓縮時的距離閥值 private int[] source_x = new int[NUMBER];// 原始曲線節點的橫座標 private int[] source_y = new int[NUMBER];// 原始曲線節點的縱座標 private int[] result_x;// 儲存壓縮後的曲線節點橫座標 private int[] result_y;// 儲存壓縮後的曲線節點縱座標 private int[] index = new int[NUMBER];// 記錄保留的節點在原始曲線節點座標數組中的位置 private int count = 0;// 保留的節點個數 private int width, height; public PolyCompress() { setSize(400, 300); 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] = 75 + random.nextInt(50); } System.out.println("未經處理資料點:"); for (int i = 0; i < NUMBER; i++) { System.out.println("source_xy[" + i + "]:" + source_x[i] + "," + source_y[i]); } // 將原始曲線的首尾節點保留下來。 index[count++] = 0; index[count++] = NUMBER - 1; // 調用遞迴函式 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("原曲線中節點數為:" + NUMBER); System.out.println("節點取捨的閾值為:" + TOLERANCE); System.out.println("壓縮後的曲線中節點數為:" + count); System.out.println("保留節點在原曲線節點資料中的位置如下:"); System.out.println("本次壓縮比為:" + 100 * count / NUMBER + "%"); } public double distance(int start, int end, 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); // 繪製原始曲線 g.setColor(Color.gray); g.drawLine(0, 100, width, 100); g.setColor(Color.red); g.drawPolyline(source_x, source_y, NUMBER); // 如果壓縮後節點數不為0,則繪製壓縮後的曲線 if (count != 0) { g.setColor(Color.gray); g.drawLine(0, 200, width, 200); g.setColor(Color.green); g.drawPolyline(result_x, result_y, count); } } public void sort() { for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (index[j] < index[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(); } }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.