Thursday, January 22, 2009

Assignment 3: Bezier Curves






Assignment 3 involves implementing a parametric line function, a quadratic Bezier curve implementation, and a cubic Bezier curve implementation.

void setup(){
size(200,200);
background(0);
stroke(255);
}
void draw(){
parLine(10,10,190,50);
quadBesCurve(10,60,150,60,100,100);
cubBesCurve(10,90, 40, 120, 130, 90, 140, 140);
}
//Draws a line parametrically

void parLine(int x1, int y1, int x2, int y2){
for (float t=0;t<1;t=t+.001){
point(x1+t*(x2-x1),y1+t*(y2-21));
}
}

//Draws a quadratic Bezier curve

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

//Draws a cubic Bezier curve

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

Monday, January 12, 2009

Assignment 2

1. The first part of Assignment 2 is to write a function in processing that draws a straight line from one point to another using only point commands, creating a line by drawing to individual pixels using Bresenham's line algorithm.







Source:

void setup(){
size(150,150);
background(0);
stroke(255);
}
void draw(){
linePix(10,10,10,100);
linePix(20,10,110,10);
linePix(20,20,40,100);
linePix(30,20,100,100);
linePix(40,20,100,40);
}
void linePix(int x0, int y0, int x1, int y1){
int dy=y1-y0;
int dx=x1-x0;
int xstep, ystep;


if (dy < 0){
dy=-dy;
ystep=-1;
}
else {
ystep=1;
}
if (dx < 0){
dx=-dx;
xstep=-1;
}
else{
xstep=1;
}
point(x0,y0);
if (dx > dy){
int e = 2*dy-dx;
while (x1!=x0){
if (e > 0){
y0=y0+ystep;
e=e-2*dx;
}
x0=x0+xstep;
e=e+2*dy;
point(x0,y0);
}
}
else{
int e=2*dx-dy;
while (y0!=y1){
if (e > =0){
x0=x0+xstep;
e=e-2*dy;
}
y0=y0+ystep;
e=e+2*dx;
point(x0,y0);
}
}

}



2. The second part of the assignment is to create a function in processing that takes in a number of lines as a parameter and creates a "web" consisting of that number of straight lines.









Source:

void setup(){
size(150,150);
background(0);
stroke(255);
}
void draw(){
drawWeb(10);
}
void drawWeb(int numLines){
int xAxis=numLines;
int yAxis=1;

for (int i=0;i < numLines;i++){
line(10,(xAxis+1)*10,10+yAxis*10,10);
xAxis--;
yAxis++;
}
}


3. Draw a circle when given a point and a radius using only point commands. I wrote 2: one when giving the center of the circle and one when giving the upper left coordinate of the bounding rectangle.










void setup(){
size(150,150);
background(0);
stroke(255);
}
void draw(){
circlePixCenter(75,75,20);
circlePixUpperLeft(75,75,20);

}
void circlePixCenter(int x0, int y0, int radius){
int f = 1-radius;
int ddFx=1;
int ddFy=-2*radius;
int x=0;
int y=radius;

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

while(x < y){
if (f > =0){
y--;
ddFy+=2;
f+=ddFy;
}
x++;
ddFx+=2;
f+=ddFx;
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 circlePixUpperLeft(int x1, int y1, int radius){
int f = 1-radius;
int ddFx=1;
int ddFy=-2*radius;
int x=0;
int y=radius;
int x0=x1+radius;
int y0=y1+radius;
point(x0,y0+radius);
point(x0, y0-radius);
point(x0+radius, y0);
point(x0-radius, y0);

while(x < y){
if (f > =0){
y--;
ddFy+=2;
f+=ddFy;
}
x++;
ddFx+=2;
f+=ddFx;
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);

}
}

Thursday, January 8, 2009

Assignment 1


This is my first assignment and first attempt using Processing.

Source Code:

size(400,400);
background(70,200,200);
noStroke();
fill(235,235,50);
//head
rect(100,50,200,200);

//neck
rect(100, 250,140, 80);

//Hair
triangle(100,30,100,50,110,50);
triangle(110,50,130,50,120,30);
triangle(130,50,150,50,140,30);
triangle(150,50,170,50,160,30);
triangle(170,50,190,50,180,30);
triangle(190,50,210,50,200,30);
triangle(210,50,230,50,220,30);
triangle(230,50,250,50,240,30);
triangle(250,50,270,50,260,30);
triangle(270,50,290,50,280,30);
triangle(290,50,300,50,300,30);
stroke(0);

//Outline of head
line(100,30, 100, 330);
line(300,30,300,250);
line(240,250,240,330);
line(100,30,110,50);
line(120,30,130,50);
line(140,30,150,50);
line(160,30,170,50);
line(180,30,190,50);
line(200,30,210,50);
line(220,30,230,50);
line(240,30,250,50);
line(260,30,270,50);
line(280,30,290,50);
line(120,30,110,50);
line(140,30,130,50);
line(160,30,150,50);
line(180,30,170,50);
line(200,30,190,50);
line(220,30,210,50);
line(240,30,230,50);
line(260,30,250,50);
line(280,30,270,50);
line(300,30,290,50);
line(100,330,240,330);

//Mouth
stroke(0);
ellipseMode(CORNER);
arc(120,220,180,60,0,3*PI/4);
line(147,268,142,273);

//Eyes
fill(255);
ellipse(260,137,65,60);
ellipse(190,135,80,65);
fill(0);
ellipse(225,160,5,5);
ellipse(295, 160, 5,5);

//Nose
fill(235,235,50);
arc(250,190,40,20,1.5*PI,PI);

//Ear
arc(85,220,30,25,1*PI/8,7*PI/4);
arc(90,225,20,15,PI,7*PI/4);