Skip to content

Instantly share code, notes, and snippets.

@jkwok91
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkwok91/5078bbe0e3b3bcb4fe21 to your computer and use it in GitHub Desktop.
Save jkwok91/5078bbe0e3b3bcb4fe21 to your computer and use it in GitHub Desktop.
many a month later, my attempt to rotate a line results in a sparsely filled circle
int w = 300; // width
int l = 300; // length (inaptly named because h is already taken)
int h = w/2;
int k = l/2;
int r = 100;
int xx = -1*r;
int yy;
int cnum = 7;
color myColor = getColor(cnum);
void setup() {
size(w, l);
background(0);
smooth();
}
void draw() {
//circl(h,k,r); //h,k,r
if (xx < r) {
int xx0 = h-xx;
int xx1 = h+xx;
int yy0 = (int) pow((pow(r, 2) - pow((xx0-h), 2)), .5)+k;
int yy1 = -1*((int) pow((pow(r, 2) - pow((xx1-h), 2)), .5))+k;
drawLine(xx0, yy0, xx1, yy1);
xx++;
} else {
xx = -1*r;
cnum = (cnum+5)%17;
myColor = getColor(cnum);
}
}
/*
resources:
http://krazydad.com/tutorials/makecolors.php
*/
color getColor(int i) {
int center = 128;
int widthh = 205;
double frequency = .3;
int red = (int)Math.ceil(Math.sin(frequency*i+0) * widthh + center);
int green = (int)Math.ceil(Math.sin(frequency*i+2) * widthh + center);
int blue = (int)Math.ceil(Math.sin(frequency*i+4) * widthh + center);
return color(red,green,blue);
}
void drawLine(int x0, int y0, int x1, int y1) {
int start, end;
loadPixels();
int dx = x1-x0;
int dy = y1-y0;
float slope = float(dy)/dx;
int incr = (slope > 0) ? 1 : -1;
boolean steep = (abs(slope)>1);
int error = 0;
if (dx == 0) {
start = Math.min(y0,y1);
end = Math.max(y0,y1);
for (int i = start; i < end; i++) {
pixels[i*width+x0] = myColor;
}
} else if (!steep) {
start = Math.min(x0,x1);
end = Math.max(x0,x1);
int y = (x0 == start) ? y0 : y1;
for (int i = start; i < end; i++) {
pixels[y*width+i] = myColor;
error += abs(dy);
if (error >= abs(dx)) {
y+=incr;
error-=abs(dx);
}
}
} else if (steep) {
start = Math.min(y0,y1);
end = Math.max(y0,y1);
int x = (y0 == start) ? x0 : x1;
for (int i = start; i < end; i++) {
pixels[i*width+x] = myColor;
error += abs(dx);
if (error >= abs(dy)) {
x+=incr;
error-=abs(dy);
}
}
}
updatePixels();
}
void mousePressed() {
noLoop();
}
void mouseReleased() {
loop();
}
void circl(int h, int k, int r) {
loadPixels();
int y;
for (int x = (h-r); x < (h+r); x++) {
y = (int) pow((pow(r, 2) - pow((x-h), 2)), .5)+k;
pixels[y*width+x] = myColor;
y = -1*((int) pow((pow(r, 2) - pow((x-h), 2)), .5))+k;
pixels[y*width+x] = myColor;
}
updatePixels();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment