2015-01-30 02:36:23 +01:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu.objects.curves;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Representation of a Bezier curve with the distance between each point calculated.
|
2015-02-11 08:56:02 +01:00
|
|
|
*
|
|
|
|
* @author fluddokt (https://github.com/fluddokt)
|
2015-01-30 02:36:23 +01:00
|
|
|
*/
|
2015-04-13 04:33:20 +02:00
|
|
|
public class Bezier2 extends CurveType {
|
2015-01-30 02:36:23 +01:00
|
|
|
/** The control points of the Bezier curve. */
|
|
|
|
private Vec2f[] points;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param points the control points
|
|
|
|
*/
|
|
|
|
public Bezier2(Vec2f[] points) {
|
|
|
|
this.points = points;
|
|
|
|
|
|
|
|
// approximate by finding the length of all points
|
|
|
|
// (which should be the max possible length of the curve)
|
|
|
|
float approxlength = 0;
|
|
|
|
for (int i = 0; i < points.length - 1; i++)
|
|
|
|
approxlength += points[i].cpy().sub(points[i + 1]).len();
|
|
|
|
|
2015-04-12 16:25:03 +02:00
|
|
|
init(approxlength);
|
2015-01-30 02:36:23 +01:00
|
|
|
}
|
|
|
|
|
2015-04-12 19:19:33 +02:00
|
|
|
@Override
|
2015-01-30 02:36:23 +01:00
|
|
|
public Vec2f pointAt(float t) {
|
|
|
|
Vec2f c = new Vec2f();
|
|
|
|
int n = points.length - 1;
|
|
|
|
for (int i = 0; i <= n; i++) {
|
2015-03-08 02:54:16 +01:00
|
|
|
double b = bernstein(i, n, t);
|
|
|
|
c.x += points[i].x * b;
|
|
|
|
c.y += points[i].y * b;
|
2015-01-30 02:36:23 +01:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-08 05:57:18 +01:00
|
|
|
* Calculates the binomial coefficient.
|
2015-03-08 02:54:16 +01:00
|
|
|
* http://en.wikipedia.org/wiki/Binomial_coefficient#Binomial_coefficient_in_programming_languages
|
2015-01-30 02:36:23 +01:00
|
|
|
*/
|
2015-04-13 04:33:20 +02:00
|
|
|
private static long binomialCoefficient(int n, int k) {
|
2015-03-08 02:54:16 +01:00
|
|
|
if (k < 0 || k > n)
|
|
|
|
return 0;
|
|
|
|
if (k == 0 || k == n)
|
|
|
|
return 1;
|
2015-03-08 05:57:18 +01:00
|
|
|
k = Math.min(k, n - k); // take advantage of symmetry
|
2015-03-08 02:54:16 +01:00
|
|
|
long c = 1;
|
2015-03-08 05:57:18 +01:00
|
|
|
for (int i = 0; i < k; i++)
|
2015-03-08 02:54:16 +01:00
|
|
|
c = c * (n - i) / (i + 1);
|
|
|
|
return c;
|
2015-01-30 02:36:23 +01:00
|
|
|
}
|
2015-03-08 05:57:18 +01:00
|
|
|
|
2015-01-30 02:36:23 +01:00
|
|
|
/**
|
|
|
|
* Calculates the Bernstein polynomial.
|
|
|
|
* @param i the index
|
|
|
|
* @param n the degree of the polynomial (i.e. number of points)
|
|
|
|
* @param t the t value [0, 1]
|
|
|
|
*/
|
|
|
|
private static double bernstein(int i, int n, float t) {
|
2015-03-08 02:54:16 +01:00
|
|
|
return binomialCoefficient(n, i) * Math.pow(t, i) * Math.pow(1 - t, n - i);
|
2015-01-30 02:36:23 +01:00
|
|
|
}
|
|
|
|
}
|