Fixes some sliders with too much control points.
This commit is contained in:
parent
64755bbc5c
commit
421cfad6ad
|
@ -77,8 +77,9 @@ public class Bezier2 {
|
||||||
Vec2f c = new Vec2f();
|
Vec2f c = new Vec2f();
|
||||||
int n = points.length - 1;
|
int n = points.length - 1;
|
||||||
for (int i = 0; i <= n; i++) {
|
for (int i = 0; i <= n; i++) {
|
||||||
c.x += points[i].x * bernstein(i, n, t);
|
double b = bernstein(i, n, t);
|
||||||
c.y += points[i].y * bernstein(i, n, t);
|
c.x += points[i].x * b;
|
||||||
|
c.y += points[i].y * b;
|
||||||
}
|
}
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -104,12 +105,21 @@ public class Bezier2 {
|
||||||
public float totalDistance() { return totalDistance; }
|
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) {
|
public static long binomialCoefficient(int n, int k) {
|
||||||
return (n <= 1 || n > 20) ? 1 : n * factorial(n - 1);
|
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.
|
* Calculates the Bernstein polynomial.
|
||||||
* @param i the index
|
* @param i the index
|
||||||
|
@ -117,7 +127,8 @@ public class Bezier2 {
|
||||||
* @param t the t value [0, 1]
|
* @param t the t value [0, 1]
|
||||||
*/
|
*/
|
||||||
private static double bernstein(int i, int n, float t) {
|
private static double bernstein(int i, int n, float t) {
|
||||||
return factorial(n) / (factorial(i) * factorial(n - i)) *
|
return binomialCoefficient(n, i) * Math.pow(t, i) * Math.pow(1 - t, n - i);
|
||||||
Math.pow(t, i) * Math.pow(1 - t, n - i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,13 +87,4 @@ public abstract class Curve {
|
||||||
return a * (1 - t) + b * t;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user