Java Geometry Algorithm applyDynamism(final Point[] spline, final int msForMove, final int msPerMove)

Here you can find the source of applyDynamism(final Point[] spline, final int msForMove, final int msPerMove)

Description

Omits points along the spline in order to move in steps rather then pixel by pixel

License

Open Source License

Parameter

Parameter Description
spline The pixel by pixel spline
msForMove The ammount of time taken to traverse the spline. should be a value from #fittsLaw
msPerMove The ammount of time per each move

Return

The stepped spline

Declaration

public static Point[] applyDynamism(final Point[] spline, final int msForMove, final int msPerMove) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

public class Main {
    /**/*from w  w w . ja  v a 2  s. c o  m*/
     * Omits points along the spline in order to move in steps rather then pixel
     * by pixel
     *
     * @param spline    The pixel by pixel spline
     * @param msForMove The ammount of time taken to traverse the spline. should be a
     *                  value from {@link #fittsLaw}
     * @param msPerMove The ammount of time per each move
     *
     * @return The stepped spline
     */
    public static Point[] applyDynamism(final Point[] spline, final int msForMove, final int msPerMove) {
        final int numPoints = spline.length;
        final double msPerPoint = (double) msForMove / (double) numPoints;
        final double undistStep = msPerMove / msPerPoint;
        final int steps = (int) Math.floor(numPoints / undistStep);
        final Point[] result = new Point[steps];
        final double[] gaussValues = gaussTable(result.length);
        double currentPercent = 0;
        for (int i = 0; i < steps; i++) {
            currentPercent += gaussValues[i];
            final int nextIndex = (int) Math.floor(numPoints * currentPercent);
            if (nextIndex < numPoints) {
                result[i] = spline[nextIndex];
            } else {
                result[i] = spline[numPoints - 1];
            }
        }
        if (currentPercent < 1D) {
            result[steps - 1] = spline[numPoints - 1];
        }
        return result;
    }

    /**
     * Returns an array of gaussian values that add up to 1 for the number of
     * steps Solves the problem of having using an intergral to distribute
     * values
     *
     * @param steps Number of steps in the distribution
     *
     * @return An array of values that contains the percents of the distribution
     */
    private static double[] gaussTable(final int steps) {
        final double[] table = new double[steps];
        final double step = 1D / steps;
        double sum = 0;
        for (int i = 0; i < steps; i++) {
            sum += gaussian(i * step);
        }
        for (int i = 0; i < steps; i++) {
            table[i] = gaussian(i * step) / sum;
        }
        return table;
    }

    /**
     * Satisfies Integral[gaussian(t),t,0,1] == 1D Therefore can distribute a
     * value as a bell curve over the intervel 0 to 1
     *
     * @param t = A value, 0 to 1, representing a percent along the curve
     *
     * @return The value of the gaussian curve at this position
     */
    private static double gaussian(double t) {
        t = 10D * t - 5D;
        return 1D / (Math.sqrt(5D) * Math.sqrt(2D * Math.PI)) * Math.exp(-t * t / 20D);
    }
}

Related

  1. absoluteBearing(Point2D source, Point2D target)
  2. absoluteBearing(Point2D.Double sourceLocation, Point2D.Double target)
  3. applyCoG(Point2D.Double[] points, Point2D.Double cog)
  4. applyMidPointApprox(CubicCurve2D cubic, QuadCurve2D quad)
  5. arcThroughThreePoints(float x1, float y1, float x2, float y2, float x3, float y3, boolean clockwise)
  6. area2(Point2D a, Point2D b, Point2D c)
  7. AreAlign(Point2D.Double pivot, Point2D.Double a, Point2D.Double b)