/ p5 / re-code 11

Inspiré par « Research and Teaching in Art and Science » by Vladimir Bonačić // Computer Graphics and Art Vol. 2, No. 3 pp. 4-8

// This sketch is part of the ReCode Project - http://recodeproject.com

////////////////////////////////////////////////////////////////////////
// //
// "Orbits of Galois Fields" by Vladimir Bonačić //
// //
// ( vareractive version ) //
// //
////////////////////////////////////////////////////////////////////////

// (c) Martin Schneider 2012

// Creative Commons license CC BY-SA 3.0

// These patterns are displayed using 32 x 32 dots.
// They visualize objects from abstract algebra, 
// which are known as orbits of galois fields.

// Source:
// "Research and Teaching in Art and Science" by Vladimir Bonačić
// Computer Graphics and Art Vol. 2, No. 3 pp. 4-8

// See also:
// "Kinetic Art: Application of Abstract Algebra to Objects with Computer-
// Controlled Flashing Lights and Sound Combinations" by Vladimir Bonačić
// Leonardo, Vol. 7, No. 3
// Note: This paper is quite mathematical at times.
// The implementation however is actually really simple.


///////////////////////// varERACTIVE VERSION /////////////////////////

// - Use space to flip through the presets
// - Use the mouse to explore the orbits


////////////////////////////////////////////////////////////////////////

var n = 5; // bits per dimension
var d = 1<<n; // cells per dimension
var led = 20; // size of the dot
var w = led * d + 1; // screen size

var preset = [1087, 1157];
var pick = 0;
var p = preset[pick];

var debug = true;
var i0;
var bg=10;


function setup() {
 createCanvas(w, w);
 ellipseMode(CORNER); 
 noStroke();
}


function draw() {
 
 // adding some afterglow
 fill(bg, 30); 
 rect(0, 0, w, w); 
 fill(255 - bg);

 // use mouse coordinates to get initial cell
 var x = mouseX/led & (d-1);
 var y = mouseY/led & (d-1);
 var i = y * d + x;

 // create empty field
 //boolean[] field = new boolean[d*d];
 
 var field = [d*d];

 // find all cells in the orbit
 i0 = d*d;
 while (!field[i]) {
 i0 = min(i, i0);
 field[i] = true;
 i *= 2; 
 if (i >= d*d ) i ^= p;
 } 

 // draw display
 for (i = 0; i < d*d; i++) {
 if (field[i]) {
 ellipse(led * (i % d), led * floor(i / d), led, led);
 }
 }
}


function keyPressed() {
 switch(key) {
 // switch between presets
 case ' ': pick = (pick + 1) % preset.length; p = preset[pick]; break;
 // next pattern
 case '+': p = (p + 1) | d; break; 
 // previous pattern
 case '-': p = (p - 1) | d; break;
 // toggle debugging
 case 'd': debug = !debug; break;
 // switch background color
 case 'b': bg = 255 - bg; break;
 default: return;
 }
}