Fixes some sliders with too much control points.

This commit is contained in:
fd 2015-03-07 20:54:16 -05:00
parent 64755bbc5c
commit 421cfad6ad
2 changed files with 20 additions and 18 deletions

View File

@ -77,8 +77,9 @@ public class Bezier2 {
Vec2f c = new Vec2f();
int n = points.length - 1;
for (int i = 0; i <= n; i++) {
c.x += points[i].x * bernstein(i, n, t);
c.y += points[i].y * bernstein(i, n, t);
double b = bernstein(i, n, t);
c.x += points[i].x * b;
c.y += points[i].y * b;
}
return c;
}
@ -102,14 +103,23 @@ public class Bezier2 {
* Returns the total distances of this Bezier curve.
*/
public float totalDistance() { return totalDistance; }
/**
* Calculates the factorial of a number.
* http://en.wikipedia.org/wiki/Binomial_coefficient#Binomial_coefficient_in_programming_languages
*
*/
private static long factorial(int n) {
return (n <= 1 || n > 20) ? 1 : n * factorial(n - 1);
public static long binomialCoefficient(int n, int k) {
if (k < 0 || k > n)
return 0;
if (k == 0 || k == n)
return 1;
k = Math.min(k, n - k); // # take advantage of symmetry
long c = 1;
for (int i = 0; i < k; i++) {
c = c * (n - i) / (i + 1);
}
return c;
}
/**
* Calculates the Bernstein polynomial.
* @param i the index
@ -117,7 +127,8 @@ public class Bezier2 {
* @param t the t value [0, 1]
*/
private static double bernstein(int i, int n, float t) {
return factorial(n) / (factorial(i) * factorial(n - i)) *
Math.pow(t, i) * Math.pow(1 - t, n - i);
return binomialCoefficient(n, i) * Math.pow(t, i) * Math.pow(1 - t, n - i);
}
}

View File

@ -87,13 +87,4 @@ public abstract class Curve {
return a * (1 - t) + b * t;
}
/**
* A recursive method to evaluate polynomials in Bernstein form or Bezier curves.
* http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
*/
protected float deCasteljau(float[] a, int i, int order, float t) {
if (order == 0)
return a[i];
return lerp(deCasteljau(a, i, order - 1, t), deCasteljau(a, i + 1, order - 1, t), t);
}
}