Java Geometry Polygon sparsifyPolygon(java.awt.Polygon p, double minDistance)

Here you can find the source of sparsifyPolygon(java.awt.Polygon p, double minDistance)

Description

sparsify Polygon

License

Open Source License

Declaration

public static java.awt.Polygon sparsifyPolygon(java.awt.Polygon p, double minDistance) 

Method Source Code


//package com.java2s;
/*// w ww  . j a  va2s.co m
This file is part of Mroi, an ImageJ plugin to handle multiple regions
of interest in image stacks.
Copyright (C) 2007 Frederick Ross
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    public static java.awt.Polygon sparsifyPolygon(java.awt.Polygon p, double minDistance) {
        int n = p.npoints;
        int[] xs = p.xpoints;
        ArrayList<Integer> nxs = new ArrayList<Integer>(n);
        int[] ys = p.ypoints;
        ArrayList<Integer> nys = new ArrayList<Integer>(n);
        nxs.add(xs[0]);
        nys.add(ys[0]);
        int px = xs[0];
        int py = ys[0];
        int qx, qy;
        double dist;
        for (int i = 1; i < n; i++) {
            qx = xs[i];
            qy = ys[i];
            dist = Math.sqrt((qx - px) * (qx - px) + (qy - py) * (qy - py));
            if (dist > minDistance) {
                nxs.add(qx);
                nys.add(qy);
                px = qx;
                py = qy;
            }
        }
        dist = Math.sqrt((px - xs[0]) * (px - xs[0]) + (py - ys[0]) * (py - ys[0]));
        if (dist <= minDistance) {
            nxs.set(nxs.size() - 1, xs[0]);
            nys.set(nys.size() - 1, ys[0]);
        } else {
            nxs.add(xs[0]);
            nys.add(ys[0]);
        }
        int[] anxs = new int[nxs.size()];
        int[] anys = new int[nys.size()];
        for (int i = 0; i < nxs.size(); i++) {
            anxs[i] = nxs.get(i);
            anys[i] = nys.get(i);
        }
        return new java.awt.Polygon(anxs, anys, nxs.size());
    }
}

Related

  1. boundingTest(Polygon p, int x, int y, int w, int h)
  2. inside_polygon(float[] xpts, float[] ypts, double ptx, double pty)
  3. makeArea(Polygon polygon, int steps, int dX, int dY)
  4. polyArea(Polygon target)
  5. regularPolygon(int sides, double x, double y, double width, double height)