Recursively Calculation of DouglasPeucker Algorithm - Java java.lang

Java examples for java.lang:Math Vector

Description

Recursively Calculation of DouglasPeucker Algorithm

Demo Code

/*/*from w ww .j  a va 2s. co m*/
 *  Flingbox - An OpenSource physics sandbox for Google's Android
 *  Copyright (C) 2009  Jon Ander Pe?alba & Endika Guti?rrez
 *
 *  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.ArrayList;

public class Main{
    /**
     * Recursively Calculation of DouglasPeucker Algorithm
     */
    private static void douglasPeucker(final Vector2D[] points,
            final float epsilon, final int first, final int last,
            final ArrayList<Vector2D> resultPoints) {

        float maxDistance = 0.0f;
        int maxDistanceIndex = 0;

        /* Find maximum distance point.  */
        for (int i = first + 1; i < last; i++) {
            float distance = distanceFromLineToPoint(points[first],
                    points[last], points[i]);
            if (distance > maxDistance) { // Store point
                maxDistance = distance;
                maxDistanceIndex = i;
            }
        }

        /* If point distance is more than epsilon then split points array in 
         * two parts and iterate for each. 
         */
        if (maxDistance > epsilon) {
            /* Find in previous segment */
            if ((maxDistanceIndex - first) > 1)
                douglasPeucker(points, epsilon, first, maxDistanceIndex,
                        resultPoints);
            /* Put point in buffer(2 coords) */
            resultPoints.add(points[maxDistanceIndex]);
            /* Continue searching important points */
            if ((last - maxDistanceIndex) > 1)
                douglasPeucker(points, epsilon, maxDistanceIndex, last,
                        resultPoints);
        }
    }
    /**
     * Computes minimum distance from line to point
     */
    public static float distanceFromLineToPoint(final Vector2D p0,
            final Vector2D p1, final Vector2D p) {
        final float area = (p0.i * p1.j + p1.i * p.j + p.i * p0.j - p1.i
                * p0.j - p.i * p1.j - p0.i * p.j) / 2f;
        final float base = (float) Math.sqrt((p1.i - p0.i) * (p1.i - p0.i)
                + (p1.j - p0.j) * (p1.j - p0.j));
        return (float) Math.abs(2f * area / base);
    }
}

Related Tutorials