Java Geometry Algorithm resample(Vector points, int n, Vector newPoints)

Here you can find the source of resample(Vector points, int n, Vector newPoints)

Description

resample

License

Open Source License

Declaration

public static void resample(Vector<Point2D> points, int n, Vector<Point2D> newPoints) 

Method Source Code

//package com.java2s;
/*  // www  .  j a  v a 2 s  .c o  m
 *   Authors: Caroline Appert (caroline.appert@lri.fr)
 *   Copyright (c) Universite Paris-Sud XI, 2007. All Rights Reserved
 *   Licensed under the GNU LGPL. For full terms see the file COPYING.
*/

import java.awt.geom.Point2D;

import java.util.Vector;

public class Main {
    public static void resample(Vector<Point2D> points, int n, Vector<Point2D> newPoints) {
        if (points.isEmpty())
            return;
        Vector<Point2D> dstPts = new Vector<Point2D>(n);

        double segLength = pathLength(points) / (n - 1);
        double currentSegLength = 0;
        Vector<Point2D> srcPts = new Vector<Point2D>(points);
        dstPts.add((Point2D) srcPts.get(0).clone());
        for (int i = 1; i < srcPts.size(); i++) {
            Point2D pt1 = srcPts.get(i - 1);
            Point2D pt2 = srcPts.get(i);
            double d = pt1.distance(pt2);
            if ((currentSegLength + d) >= segLength) {
                double qx = pt1.getX() + ((segLength - currentSegLength) / d) * (pt2.getX() - pt1.getX());
                double qy = pt1.getY() + ((segLength - currentSegLength) / d) * (pt2.getY() - pt1.getY());
                Point2D q = new Point2D.Double(qx, qy);
                dstPts.add(q); // append new point 'q'
                srcPts.add(i, q); // insert 'q' at position i in points s.t.
                // 'q' will be the next i
                currentSegLength = 0.0;
            } else {
                currentSegLength += d;
            }
        }
        // sometimes we fall a rounding-error short of adding the last point, so
        // add it if so
        if (dstPts.size() == (n - 1)) {
            dstPts.add((Point2D) srcPts.get(srcPts.size() - 1).clone());
        }
        newPoints.clear();
        newPoints.addAll(dstPts);
    }

    public static double pathLength(Vector<Point2D> points) {
        double d = 0;
        for (int i = 1; i < points.size(); i++) {
            d += points.get(i - 1).distance(points.get(i));
        }
        return d;
    }
}

Related

  1. projectionFactor(Point pt, Point start, Point stop)
  2. projectPoint(float x, float y, Line2D ray, float distance)
  3. projectPointOntoLine(Point2D pt, Line2D ln)
  4. quantisePoint(Point2D.Double dpos, Point gpos)
  5. relTo(final Point2D from, final Point2D to, final Point2D rel)
  6. setPathAnchor(Shape s, Point2D pt)
  7. setPathControlPoint(Shape s, int i, Point2D pt)
  8. setRevisePoint(Point2D.Double revisePoint, Point2D.Double startPoint)
  9. slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2)