Bezier Curves (Linear, Quadratic, Cubic)
My Curves

void setup() {
size(400, 400);
background(0);
saveFrame("bezier.png");
}
void draw() {
stroke(233, 34, 300);
myBeZier(200, 300, 180, 95);
stroke(150, 20, 300);
myBeZquad(300, 200, 50, 270, 210, 310);
stroke(10, 30, 403);
myBeZcubic(350, 100, 350, 200, 150, 65, 90, 120);
}
//Linear Bezier Curve
//From formula B(t) = p0 + t(p1-p0) = (1-t)p0+tp1
void myBeZier(int x0, int y0, int x1, int y1) {
float x, y;
for(float t=0.0; t<=1; t+=0.0000005) {
x = x0 + t*(x1-x0);
y = y0 + t*(y1-y0);
point(x,y);
}
}
//Quadratic Bezier Curve
//From formula B(t) = (1-t)^2 p0+2(1-t)t p1+ t^2 p2
void myBeZquad(int x0, int y0, int x1, int y1, int x2, int y2) {
float x, y;
for(float t=0.0; t<=1; t+=0.0000005) {
x = pow((1-t), 2)*x0+ 2*(1-t)*t*x1+ pow(t, 2) * x2;
y = pow((1-t), 2)*y0+ 2*(1-t)*t*y1+ pow(t, 2) * y2;
point(x,y);
}
}
//Cubic Bezier Cube
//From formula B(t) = (1-t)^3 p0 + 3(1-t)^2 tp1+3(1-t)p2+t^3p3
void myBeZcubic(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) {
float x, y;
for(float t=0.0; t<=1; t+=0.0000005) {
x = pow((1-t), 3) * x0+3*(pow((1-t),2))*t*x1 + 3*(1-t)*t*t*x2 + (t*t*t)*x3;
y = pow((1-t), 3) * y0+3*(pow((1-t),2))*t*y1 + 3*(1-t)*t*t*y2 + (t*t*t)*y3;
point(x,y);
}
}

No comments:
Post a Comment