Monday, January 26, 2009

Finished Bezier Curve Algorithms

Alright, I finally debugged and finished my algorithm for cubic and quadratic Bezier curves. Below is a pic of the applet running the program.
Here is the code used:

public void setup(){
size(250,300);
background(0);
}

public void draw(){
stroke(0,255,0);
myCurve(25, 100, 50, 210, 200, 100, 200, 30, 0.01);
stroke(255,0,0);
myCurve(50, 210, 200, 100, 200, 30, 0.01);
stroke(0,0,255);
myCurve(200, 100, 200, 30, 0.01);
}

public void myCurve(int x0, int y0, int x1, int y1, int x2, int y2, double t){
//Quadratic
double x = x0;
double y = y0;
point(x0, y0);
for(double i = t; i <= 1; i+=t){
//System.out.println(i);
x = Math.pow((1.0-i),2)*x0 + 2.0*(1.0-i)*i*x1 + i*i*x2;
y = Math.pow((1.0-i),2)*y0 + 2.0*(1.0-i)*i*y1 + i*i*y2;
point(Math.round(x),Math.round(y));
}
point(x2,y2);
}

public void myCurve(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, double t){
//cubic
double x = x0;
double y = y0;
point(x0, y0);
for(double i = t; i <= 1; i+=t){
x = Math.pow((1.0-i),3)*x0 + Math.pow((1-i),2)*3*i*x1 + Math.pow((1-i),2)*3*i*i*x2 + i*i*i*x3;
y = Math.pow((1.0-i),3)*y0 + Math.pow((1-i),2)*3*i*y1 + Math.pow((1-i),2)*3*i*i*y2 + i*i*i*y3;
point(Math.round(x),Math.round(y));
}
point(x3, y3);
}

public void myCurve(int x0, int y0, int x1, int y1, double t){
//linear
double x = x0;
double y = y0;
point(x0, y0);
for(double i = 0; i <= 1; i+=t){
x += (x1-x0)*t;
y += (y1-y0)*t;
point(Math.round(x),Math.round(y));
}
point(x1, y1);
}

No comments:

Post a Comment