
Homework #2
For this assignment we experimented with Bresenham's line algorithm and the midpoint circle algorithm. We then could either use the line algorithm or Processing's line function to draw a web. This is what I created!!!
Source code:
void setup() {
size(400, 400);
background(0);
int x0, x1, y0, y1;
}
void myLine(int x0, int x1, int y0, int y1) {
int changeX = x1-x0;
int changeY = y1-y0;
float bresenham = (float)changeY / changeX;
float error = 0;
int y = y0;
for(int x = x0; x <= x1; x++) {
point(x,y);
error = error + bresenham;
if(abs(error) >= 0.5) {
y = y + 1;
error = error - 1.0;
}
}
}
void drawWeb() {
stroke(255, 0, 128);
line(0, 100, 10, 0);
line(0, 90, 20, 0);
line(0, 80, 30, 0);
line(0, 70, 40, 0);
line(0, 60, 50, 0);
line(0, 50, 60, 0);
line(0, 40, 70, 0);
line(0, 30, 80, 0);
line(0, 20, 90, 0);
line(0, 10, 100, 0);
}
void myCircle(int x0, int y0, int radius) {
int f = 1 - radius;
int fxx = 1;
int fyy = -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--;
fyy += 2;
f += fyy;
}
x++;
fxx += 2;
f += fxx;
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 draw() {
//Changes the stroke of my line to Pink
stroke(255, 0, 128);
//Draws myLine (x0<=x1, y0<=y1)
myLine(150, 300, 100, 300);
//Draws myWeb
drawWeb();
//Draws myCirle
myCircle(190, 290, 100);
}

No comments:
Post a Comment