Tuesday, February 3, 2009

Bezier Curves (hw 3 part 1)

Haven't yet figured out how to make an applet work, but here is an image of a cubic bezier curve.
The linear and quadratics also work, but I don't have an image, if you want to play with it to make sure, the code is below.

void setup()
{
size (400, 400);
background (0,0,0);
stroke (255);
}

void draw()
{
saveFrame("bezier.jpeg");
}

int count = 0;
int x0=0,x1=0,x2=0,x3=0,y0=0,y1=0,y2=0,y3=0;

void mousePressed()
{
if (count == 0)
{
x0 = mouseX;
y0 = mouseY;
ellipse(x0, y0, 5, 5);
count++;
}
else if (count == 1)
{
x1 = mouseX;
y1 = mouseY;
ellipse(x1, y1, 5, 5);
linearB(x0, y0, x1, y1);
count++;
}
else if (count == 2)
{
x2 = mouseX;
y2 = mouseY;
background(0,0,0);
ellipse(x0, y0, 5, 5);
ellipse(x1, y1, 5, 5);
ellipse(x2, y2, 5, 5);
quadB(x0, y0, x1, y1, x2, y2);
count++;
}
else if (count == 3)
{
x3 = mouseX;
y3 = mouseY;
background(0,0,0);
ellipse(x0, y0, 5, 5);
ellipse(x1, y1, 5, 5);
ellipse(x2, y2, 5, 5);
ellipse(x3, y3, 5, 5);
cubicB(x0, y0, x1, y1, x2, y2, x3, y3);
count++;
}
else if (count >= 4)
{
background(0,0,0);
count = 0;
}
}

void linearB (int x0, int y0, int x1, int y1)
{
for (float t=0; t<1; t+=.001)
{
point ((1-t)*x0 + (t*x1), (1-t)*y0 + (t*y1));
}
}

void quadB (int x0, int y0, int x1, int y1, int x2, int y2)
{
for (float t=0; t<1; t+=.001)
{
point ((sq(1-t)*x0 + 2*(1-t)*t*x1 + sq(t)*x2),
(sq(1-t)*y0 + 2*(1-t)*t*y1 + sq(t)*y2));
}
}

void cubicB (int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3)
{
for (float t=0; t<1; t+=.001)
{
point ((pow((1-t),3)*x0 + 3*sq(1-t)*t*x1 + 3*(1-t)*sq(t)*x2 + pow(t,3)*x3),
(pow((1-t),3)*y0 + 3*sq(1-t)*t*y1 + 3*(1-t)*sq(t)*y2 + pow(t,3)*y3));
}
}

Homework 2 Revisited


I've finally gone back and tracked down my errors as well as compiled them into one nice image for homework 2. Here is the code:

void setup(){
size (400, 400);
background (0,0,0);
stroke (255);
}

void myCircle (int x0, int y0, int rad){

int f = 1 - rad;
int ddF_x = 1;
int ddF_y = -2 * rad;
int x = 0;
int y = rad;

point(x0, y0 + rad);
point(x0, y0 - rad);
point(x0 + rad, y0);
point(x0 - rad, y0);

while (x < y){
if (f >= 0){
y--;
ddF_y += 2;
f += ddF_y;
}

x++;
ddF_x += 2;
f += ddF_x;
point (x0 + x, y0 + y);
point (x0 - x, y0 + y);
point (x0 + x, y0 - y);
point (x0 - x, y0 - y);
point (x0 + y, y0 + x);
point (x0 - y, y0 + x);
point (x0 + y, y0 - x);
point (x0 - y, y0 - x);
}
}

void web(){

int x1, x2, y1, y2, n;

n = 500;

x1 = 0;
x2 = n;
y1 = 1;
y2 = 0;

int i;
for (i = 0; i < n; i++){
line (x1, x2, y1, y2);
x2-=10;
y1+=10;
}
}

void myLine(int x0, int y0, int x1, int y1){

boolean steep = (abs(y1 - y0) > abs(x1 - x0));

int temp;
if (steep){
temp = x0;
x0 = y0;
y0 = temp;
temp = x1;
x1 = y1;
y1 = temp;
}

if (x0 > x1){
temp = x0;
x0 = x1;
x1 = temp;
temp = y0;
y0 = y1;
y1 = temp;
}

int delta_x = x1 - x0;
int delta_y = abs(y1 - y0);
float error = 0;
float delta_err = (float)delta_y/delta_x;
int y_step;
int y = y0;

if (y0y_step = 1;
}
else{
y_step = -1;
}

for (int x = x0; x < x1; x++){
if (steep){
point (y, x);
}
else{
point (x, y);
}
error += delta_err;

if (error >= 0.5){
y += y_step;
error -= 1.0;
}
}
}

void draw(){
web();
myCircle (200,200,50);
myLine (300, 80, 269, 300);
saveFrame("hw1.jpeg");
}

And now on to homework 3...