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);
}
}
No comments:
Post a Comment