Here you can find the source of applyDynamism(final Point[] spline, final int msForMove, final int msPerMove)
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 |
public static Point[] applyDynamism(final Point[] spline, final int msForMove, final int msPerMove)
//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); } }