> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.HE0NPTnYdHZ/rev.746
 * 
 * authors: 
 *   Daniele Olmisani

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/**
 * Quasicrystals by Combining Waves
 * by Ricard Marxer
 * ported to Processing from:
 * http://mainisusuallyafunction.blogspot.com/2011/10/quasicrystals-as-sums-of-waves-in-plane.html)
 *
 * Render quaiscrystals by
 * combining planar waves in different directions
 */
 
int waveCount = 7;  // The number of waves to combine
float waveFreq = 0.4;  // The frequency of the waves
float waveOffsetInc = 0.01;  // The speed at which the waves slide
 
float waveOffset = 0.0;  // The initial offset of the waves

float[] preSin = new float[waveCount];
float[] preCos = new float[waveCount];
float[][] values = new float[waveCount][400][400];

void setup() {
  size(400, 400);
  smooth();
  
  float deltaAng = PI / waveCount;
  for(int m=0; m<waveCount; m++) {
    //float ang = m*deltaAng;
    //preSin[m] = sin(m*deltaAng );
    //preCos[m] = cos(m*deltaAng );       
    
    preSin[m] = waveFreq*sin(m*deltaAng );
    preCos[m] = waveFreq*cos(m*deltaAng );       
  }
  
    for(int i=0; i<400; i++) {
        for(int j=0; j<400; j++) {
            for(int m=0; m<waveCount; m++) {
                values[m][i][j] = (cos(preCos[m]*i + preSin[m]*j));
            }
       }    
    }
}
 
void draw() {
    
  background(255);
 
  for (int i=0; i<width; i++) {
    for (int j=0; j<height; j++) {
        
      float c = 0;
      for (int m=0; m<waveCount; m++) {
        
        //float kx = preCos[m];
        //float ky = preSin[m];
 
        //c += (wave(kx, ky, i, j, waveOffset) + 1) / 2;
        //c += (cos(waveFreq*(preCos[m] *i + preSin[m] *j) + waveOffset)  + 1) / 2;
        //c += (cos((preCos[m] *i + preSin[m] *j) + waveOffset)  + 1) / 2;
        c += ((values[m][i][j] + waveOffset)  + 1) / 2;
        
        
      }
      
 
      float v = c % 1;
      int k = int(c - v);
      float w = map((k & 1) == 0 ? v : 1.0 - v, 0, 1, 0, 255);
 
      set(i, j, color(w));
    }
  }
 
  waveOffset += 0.2;
}
 
 
float wave(float kx, float ky, float x, float y, float waveOffset) {
  return cos(waveFreq*(kx*x + ky*y) + waveOffset);
}