Merge pull request #39 from fluddokt/omaster

Fixes some sliders with too much control points. / Attempts to load music with incorrect file names. #33
This commit is contained in:
Jeffrey Han 2015-03-07 23:28:28 -05:00
commit cfd335de88
3 changed files with 28 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);
}
}

View File

@ -149,6 +149,14 @@ public class OpenALStreamPlayer {
audio = new Mp3InputStream(ResourceLoader.getResourceAsStream(ref));
else
audio = new OggInputStream(ResourceLoader.getResourceAsStream(ref));
if(audio.getRate() == 0 && audio.getChannels() == 0) {
if (ref.toLowerCase().endsWith(".mp3"))
audio = new OggInputStream(ResourceLoader.getResourceAsStream(ref));
else
audio = new Mp3InputStream(ResourceLoader.getResourceAsStream(ref));
}
}
this.audio = audio;