fractalkitty looking for math? fractalkitty.com

optical illusions

OL_squares

#trig#interactive#animation

let n =21;//number of squares
let s=1;//animation flag
let t=0;//time
function setup() {
  createCanvas(400, 400);
  angleMode(DEGREES);
  noStroke();
}
function draw() {
  background(255);
  if (s===0){//animation time step
    t+=0.5
  }
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      if ((i % 2 === 0 && j % 2 === 0) || (i % 2 === 1 && j % 2 === 1)) {
        c = 255;//color based on even/odd
      } else {
        c = 0;
      }
      fill(255-c)
      rectMode(CORNER);//background rectangles drawn from corners
      w =width / n;
      x=i * w
      y=j * w
      rect(x, y, w);//background rectangles
      fill(c);
      rectMode(RADIUS);//small rectangles drawn in radius mode
      if ((i > 2) & (j > 2) && i < n - 3 && j < n - 3 ) {
        if (i < n / 2&&i!=floor(n/2)) {
          rect(x + w/2 + (n / 3) * sin( t+45),y + w/2 + (n / 3) * cos( t+45),w / 7);
          rect(x + w/2 + (n / 3) * sin( t+225),y + w/2 + (n / 3) * cos( t+225),w / 7);
        }else if(i===floor(n/2)){
          if(j>floor(n/2)){
            rect(x + w/4,y + w/4,w/7);
            rect((i+1) * w - w/4, j*w + w/4,w/7);
          }else{
            rect(x + w/4,(j+1)*w - w/4,w / 7);
            rect((i+1) * w - w/4,(j+1)*w - w/4,w/7);
          }    
                 } 
        else {
          rect(x + w/2 + (n / 3) * sin(t-45),y + w/2 + (n/3) * cos(t-45),w/7);
          rect(x + w/2 + (n / 3) * sin( t -225),y +w/2 +(n/3) * cos(t-225),w/7);
        }
      }
    }
  }
}

function mousePressed(){//animate with click
  if(s===0){
     s=1
     }else{
    s=0
  }
}