Skip to content

Instantly share code, notes, and snippets.

@zer0her0
Created August 20, 2014 19:45
Show Gist options
  • Save zer0her0/1dbfc53a734def9f79a9 to your computer and use it in GitHub Desktop.
Save zer0her0/1dbfc53a734def9f79a9 to your computer and use it in GitHub Desktop.
Automatic Mondarian
/*
int N = 2; //Number of recursive steps
// Draw a Mondarian-inspired image using recursion
void piet(int x0, int y0, int x1, int y1, int N) {
if (N == 0) {
// Base case -- draw a colorful rectangle with a thick black border
int sw = 3; //this is the stroke width for the rectangle's border
color c[] = { #ff0000, #00ff00, #0000ff, #ffff00, #ffffff}; //Mondarian color palatte
fill(c[int(random(c.length))]);
strokeWeight(sw);
rect (x0,y0,x1-x0-sw,y1-y0-sw);
} else {
//Recursive step -- recursively break the current rectangle into 4 new random rectangles
int i = int(random(x0,x1)); //Pick a random x coordinate inside the current rectangle
int j = int(random(y0,y1)); //Pick a random y coordinate inside the current rectangle
piet(x0,y0,i,j,N-1); // Recurse on upper left rectangle
piet(i,y0,x1,j,N-1); // Recurse on upper right rectangle
piet(x0,j,i,y1,N-1); // Recurse on lower left rectangle
piet(i,j,x1,y1,N-1); // Recurse on lower right rectangle
}
}
// keep draw() here to continue looping while waiting for keys
void draw() {
}
//Draw a new image each time a key is pressed
void keyPressed() {
piet(1,1,400,400, N );
}
//Draw the first image
void setup() {
size(400,400);
piet(1,1,400,400, N);
}
*/
/*
var colors = ['#cc0000', '#ffc118', '#0d0a51', 'none', 'none', 'none'];
var backgroundColor = '#cdd7b5';
s.rect(0, 0, width, height).attr({fill: backgroundColor})
// adapted from https://raw.githubusercontent.com/odewahn/codebox_recursion_examples/master/mondrian.pde
var n = 2;
// Draw a Mondarian-inspired image using recursion
function piet(x0, y0, x1, y1, n) {
if (n == 0) {
var sw = 3;
s.rect(x0, y0, x1 - x0 - sw, y1 - y0 - sw).attr({fill: colors[getRandomInt(0, colors.length - 1)], strokeWidth: sw, stroke: '#000000'});
} else {
var i = getRandomInt(x0, x1);
var j = getRandomInt(y0, y1);
piet(x0, y0, i, j, n - 1); // Recurse on upper left rectangle
piet(i, y0, x1, j, n - 1); // Recurse on upper right rectangle
piet(x0, j,i,y1, n - 1); // Recurse on lower left rectangle
piet(i, j, x1, y1, n - 1); // Recurse on lower right rectangle
}
}
piet(1, 1, width, height, n);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment