/ p5 / re-code 05

Basé sur la « Diamond Theory » de Steven H Cullinane

/* 
Part of the ReCode Project (http://recodeproject.com)
Based on "Diamond Theory" by Steven H Cullinane
Originally published in "Computer Graphics and Art" v2n1, 1977
Copyright (c) 2013 Radames Ajna - OSI/MIT license (http://recodeproject/license).
*/

/* @pjs pauseOnBlur="true"; */

var bits = [
 0, 1, 0, 1, 
 2, 3, 2, 3, 
 0, 1, 0, 1, 
 2, 3, 2, 3
];

var lsize = 10;

function setup() {
 createCanvas(int(lsize*4+100)*5, int(lsize*4+100)*5);
 background(255);
 smooth();
 redraw();
 noStroke();
}

function draw() { 
 background(255);
 for (var i=0; i<10; i++) {
 for (var j=0; j<10; j++) {
 randss();
 push();
 translate(i*(width/lsize)+lsize*3, j*(height/lsize)+lsize*3);
 drawTriangle(bits);
 pop();
 }
 }
 randss();
 if (frameCount>50) {
 noLoop();
 }
}

function drawTriangle(bits) {
 push();
 translate(-lsize*4/2, -lsize*4/2);
 for (var x=0;x<4;x++) {
 for (var y=0;y<4;y++) {
 var i = x + y*4;
 // pushStyle();
 fill(0);
 push();
 translate(x*lsize, y*lsize);
 beginShape(TRIANGLES);
 switch(bits[i]) {
 case 0:
 // bottom right
 //triangle(lsize, lsize, 0, lsize, lsize, 0);
 vertex(lsize, lsize);
 vertex(0, lsize);
 vertex(lsize, 0);
 break;
 case 1:
 ///bottom left
 //triangle(0, 0, 0, lsize, lsize, lsize);
 vertex(0, 0);
 vertex(0, lsize);
 vertex(lsize, lsize);
 break;
 case 2:
 //top right
 //triangle(0, 0, lsize, 0, lsize, lsize);
 vertex(0, 0);
 vertex(lsize, 0);
 vertex(lsize, lsize);
 break;
 case 3:
 //top left
 //triangle(0, 0, lsize, 0, 0, lsize);
 vertex(0, 0);
 vertex(lsize, 0);
 vertex(0, lsize);
 break;
 }
 endShape(CLOSE);
 //popStyle();
 pop();
 }
 }
 pop();
}
/*leva l1 para l2 */
function transposeRow(bits, r0, r1) {
 var temp = [4];
 //temp[i] = new int[4];
 for (var x=0;x<4;x++) {
 //keep the values from row r0 on temp
 var i = x + r0*4;
 temp[x] = bits[i];
 // put on row r0 values from row r1
 var i2 = x + r1*4;
 bits[i] = bits[i2];
 //put on row r1 values from r0 i.e. from temp
 bits[i2] = temp[x];
 }
}

function transposeColumn(bits, c0, c1) {
 var temp = [4];
 for (var y=0;y<4;y++) {
 //keep the values from column c1 on temp
 var i = c0 + y*4;
 temp[y] = bits[i];
 //put on column c0 values from column c1
 var i2 = c1 + y*4;
 bits[i] = bits[i2];
 //put on column c1 values from column c0 i.e. temp
 bits[i2] = temp[y];
 }
}

function transposeQuadrant(bits, q0, q1) {
 var temp = [4];
 //given a q0 - quadrant 0,1,2,3 
 // |0|1|
 // |2|3|
 //returns x,y initial coordinates 
 // |00|10|20|30|
 // |01|11|21|31|
 // |02|12|22|32|
 // |03|13|23|33| 
 var x0q0 = (q0%2)*2;
 var y0q0;
 if (q0>1) {
 y0q0 =2;
 } else {
 y0q0 =0;
 }
 var x0q1 = (q1%2)*2;
 var y0q1;
 if (q1>1) {
 y0q1 =2;
 } else {
 y0q1 =0;
 }
 var c=0; 
 for (var i=0;i<2;i++) {
 for (var j=0;j<2;j++) {
 temp[c] = bits[x0q0+i + (y0q0+j)*4];
 bits[x0q0+i + (y0q0+j)*4] = bits[x0q1+i + (y0q1+j)*4];
 bits[x0q1+i + (y0q1+j)*4] = temp[c];
 c++;
 }
 }
}

function randss() {
 transposeRow(bits, int(random(0, 4)), int(random(0, 4)));
 transposeColumn(bits, int(random(0, 4)), int(random(0, 4)));
 transposeQuadrant(bits, int(random(0, 4)), int(random(0, 4)));
}

function mousePressed() {
 randss();
 redraw();
}