Skip to content

Instantly share code, notes, and snippets.

@dean-shaff
Created October 25, 2015 16:35
Show Gist options
  • Save dean-shaff/e4d1e574a720ae0cd2de to your computer and use it in GitHub Desktop.
Save dean-shaff/e4d1e574a720ae0cd2de to your computer and use it in GitHub Desktop.
Random Squares computer art, originally by Bill Kolomyjec
/*
Random Squares, originally by Bill Kolomyjec
*/
int max_sqrs = 9 ;
int large_sqr_dim = 7;
int sqr_dim ; //= width / 7 ;
int init_sqr_dim ; // = sqr_dim / 5 ;
boolean zero_sqr = false;
void setup(){
size(560,560);
background(255);
sqr_dim = width/large_sqr_dim ;
init_sqr_dim = sqr_dim / 5 ;
float xcur;
float ycur;
int cur_num_sqr ;
for (int x = 0; x < large_sqr_dim; x++){
for (int y =0; y < large_sqr_dim; y++){
xcur = x*sqr_dim ;
ycur = y*sqr_dim ;
cur_num_sqr = make_pyramid(xcur, ycur);
println(cur_num_sqr);
if (cur_num_sqr == 1 || cur_num_sqr == 0){
zero_sqr = true;
}
}
}
}
void draw(){
}
int make_pyramid(float x_pos, float y_pos){
float init_sqrx = int(random(init_sqr_dim, sqr_dim - 2*init_sqr_dim));
float init_sqry = int(random(init_sqr_dim, sqr_dim - 2*init_sqr_dim));
int num_sqr = int(random(1,max_sqrs));
while (num_sqr == 1 || num_sqr == 0) {
if (zero_sqr == false){
break ;
}
else if (zero_sqr == true){
num_sqr = int(random(max_sqrs));
}
}
int i = 1 ;
float diffx = init_sqrx / (float) num_sqr ;
float diffy = init_sqry / (float) num_sqr ;
while (i < num_sqr){
float next_sqrx = init_sqrx - i*diffx;
float next_sqry = init_sqry - i*diffy;
//println(next_sqrx, next_sqry);
float next_dim = init_sqr_dim + i*(sqr_dim - init_sqr_dim)/num_sqr ;
//println(next_dim);
noFill();
strokeWeight(1.5);
rect(x_pos + next_sqrx,y_pos + next_sqry, next_dim, next_dim);
i ++;
}
strokeWeight(1.5);
rect(x_pos, y_pos, sqr_dim, sqr_dim);
rect(x_pos + init_sqrx, y_pos + init_sqry, init_sqr_dim, init_sqr_dim);
return num_sqr;
}
void keyPressed(){
if (key == 's'){
PImage saveimg = createImage(width, height, RGB);
saveimg.loadPixels();
loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int loc = x + y*width;
saveimg.pixels[loc] = pixels[loc];
}
}
saveimg.save("final.png");
println("Image saved");
updatePixels();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment