Skip to content
Snippets Groups Projects
Commit f4f7f1be authored by IDante's avatar IDante
Browse files

improve 3

parent 75ebe448
No related merge requests found
......@@ -228,18 +228,14 @@ public class Main extends JFrame {
for (List<Point> curve : bezierCurves) {
if (curve.size() >= 4) {
int numSegments = 100;
int numSegments = 500;
double[] xPoints = new double[numSegments + 1];
double[] yPoints = new double[numSegments + 1];
for (int i = 0; i <= numSegments; i++) {
double t = (double) i / numSegments;
xPoints[i] = 0;
yPoints[i] = 0;
for (int j = 0; j < 4; j++) {
double blend = blendFunction(3, j, t);
xPoints[i] += blend * curve.get(j).getX();
yPoints[i] += blend * curve.get(j).getY();
}
Point point = blendFunction(curve.get(0), curve.get(1), curve.get(2), curve.get(3), t);
xPoints[i] = point.getX();
yPoints[i] = point.getY();
}
for (int i = 1; i <= numSegments; i++) {
int x1 = (int) xPoints[i - 1];
......@@ -252,10 +248,17 @@ public class Main extends JFrame {
}
}
private double blendFunction(int n, int i, double t) {
int k = n - i;
double binomialCoefficient = factorial(n) / (factorial(i) * factorial(k));
return binomialCoefficient * Math.pow(t, i) * Math.pow(1 - t, k);
private Point blendFunction(Point p0, Point p1, Point p2, Point p3, double t) {
double u = 1 - t;
double tt = t * t;
double uu = u * u;
double uuu = uu * u;
double ttt = tt * t;
double x = p0.getX() * uuu + 3 * p1.getX() * t * uu + 3 * p2.getX() * tt * u + p3.getX() * ttt;
double y = p0.getY() * uuu + 3 * p1.getY() * t * uu + 3 * p2.getY() * tt * u + p3.getY() * ttt;
return new Point(x,y);
}
private double factorial(int n) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment