Tuesday, January 13, 2009

Second Homework Assignment

I have finished the second homework assignment due January 16th. I was able to create my own implementations of the line function, the circle function and created a web-like design on the corner of the applet box. The following screen capture shows all three.

The Wikipedia articles were extremely helpful for writing out these programs.

I may try out the anti-aliasing bonus later, but I figured I might as well post this finished work now.

Here is the code.

//Programmed by Brett J. Young
//Creates a house

import processing.core.*;
public class Driver extends PApplet{

public void setup(){
size(250,300);
background(0,100,210);
}

public void draw(){

stroke(255);
myLine(20,230,100,120);
stroke(100);
myWeb(135,7);
stroke(0);
myCircle(150,150,20);
}

public void myCircle(int x0, int y0, int rad){
int f = 1 - rad;
int dx = 1;
int dy = -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 if(f >= 0){
y--;
dy += 2;
f += dy;
}
x++;
dx += 2;
f += dx;

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);

}
}

public void myWeb(int size, int step){
for(int i = 0; i < size; i+=step){
int q = size - i;
myLine(0,q,i,0);
}
}

public void myLine(int xa, int ya, int xb, int yb){
int x0 = xa;
int x1 = xb;
int y0 = ya;
int y1 = yb;

boolean steep = (Math.abs(y1 - y0) > Math.abs(x1 - x0));
if(steep){
//swap(x0, y0)
x0 = ya;
y0 = xa;
//swap (x1,y1)
x1 = yb;
y1 = xb;
if(x0>x1){
//swap(x0,x1)
x0 = yb;
x1 = ya;
//swap (y0,y1)
y0 = xb;
y1 = xa;
}
}
else if(x0>x1){
//swap(x0,x1)
x0 = xb;
x1 = xa;
//swap (y0,y1)
y0 = yb;
y1 = ya;
}

int deltax = x1 - x0;
int deltay = Math.abs(y0 - y1);

double error = 0;
double deltaerror = (Double)(1.0 * deltay) / deltax;

int ystep;

int y = y0;

if(y0 < y1){
ystep = 1;
}else{
ystep = -1;
}

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

error += deltaerror;

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


}

}

No comments:

Post a Comment