void setup()
{
size(300,300);
noLoop();
}

void draw()
{
drawLine(60, 70, 30, 200);

drawWeb(50);
drawCircle(50, 50, 20);
}

void drawLine(int x0, int y0, int x1, int y1)
{
int temp = 0;
boolean steep = abs(y1 - y0) > abs(x1 - x0);
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 deltax = x1 - x0;
int deltay = abs(y1 - y0);
float error = 0;
float deltaerr = (float)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 += deltaerr;
if(error >= .5)
{
y += ystep;
error--;
}
}
}

void drawWeb(int n)
{
for(int i = 0; i < n; i++){
line(0, n-i, i, 0);
}
}

void drawCircle(int x0, int y0, int radius)
{
int f = 1 - radius;
int ddF_x = 1;
int ddF_y = -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--;
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);
}
}

0 comments: