> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.M-mUSd76jFe/rev.359
 * 
 * authors: 
 *   Filip Goldefus

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



int scrWidth = 300;
int scrHeight = 200;
int rad = 20;
int spc = 4;
int side = 20;
int backgroundColor = 255;
int dx = 5, dy = 5;

void setup() {
  size(scrWidth, scrHeight, P2D);
  smooth();
  background(backgroundColor);
  
  drawCircles(scrWidth/2);
  drawDices(scrWidth/2-4);
}

void draw() {
  if (mouseX<scrWidth/2) {
    if (mouseY < scrHeight - ((scrHeight-dx) % (rad+spc))) {
        interactiveCircles();
    }
  } else {
    interactiveDices();
  }
}

void drawCircles(int endX) {
  int y = dy+(-rad/2-spc);
  int x = dx+(-rad/2-spc);
  int posX = 0;
  int posY = 0;
  while (y<height-(rad+spc)) {
    y += rad + spc;
    while (x<endX-(rad+spc)) {
      x += rad + spc;
      drawCircle(x,y,rad,255, 255, 255);
      posX++;
    }
    posY++;
    x = dx+(-rad/2-spc);
  }
}

void drawCircle(int x, int y, int diam, int colR, int colG, int colB) {
    smooth();  
    stroke(0);
    strokeWeight(3);
    fill(colR, colG, colB);
    ellipse(x,y,diam,diam);

    float dX = random(-4,4);
    float dY = random(-4,4);

    strokeWeight(4);
    stroke(0);
    point(x+dX,y+dY);
}

void interactiveCircles() {
  int posX = (mouseX-dx) % (rad+spc);
  int pX = (mouseX-posX);
  int posY = (mouseY-dy) % (rad+spc);
  int pY = (mouseY-posY);
  if (overCircle(posX-rad/2, posY-rad/2, rad)) {
    noStroke();
    fill(backgroundColor);
    rect(pX-2,pY-2,rad+4,rad+4);
    drawCircle(pX+rad/2, pY+rad/2, rad, 255, 255, 255);
  }
}
 
boolean overCircle(int x, int y, int diameter) 
{
  if(sqrt(sq(x) + sq(y)) < diameter/2 ) {
    return true;
  } else {
    return false;
  }
}

void drawDices(int beginX) {
  int y = 0+dy;
  int x = beginX+dx;
  while (y+side+spc<height) {
    while (x+side+spc<width) {
      drawDiceFace(x,y,round(random(0.5,6.5)));
      x += side + spc;
    }
    y += side + spc;
    x = beginX+dx;
  }
}

void drawDiceFace(int x, int y, int n) {
  smooth();
  stroke(0);
  strokeWeight(3);
  rect(x, y, side-1, side-1, 3);
  strokeWeight(4);
  switch (n) {
    case 1:
      point(x+floor(side/2), y+floor(side/2));
      break;
    case 2:
      point(x+floor(side/3), y+floor(side/3));
      point(x+floor(side*2/3), y+floor(side*2/3));
      break;
    case 3:
      point(x+floor(side/3), y+floor(side/3));
      point(x+floor(side/2), y+floor(side/2));
      point(x+side-floor(side/3), y+side-floor(side/3));
      break;
    case 4:
      point(x+floor(side/3), y+floor(side/3));
      point(x+floor(side/3), y+side-floor(side/3));
      point(x+side-floor(side/3), y+floor(side/3));
      point(x+side-floor(side/3), y+side-floor(side/3));
      break;
    case 5:
      point(x+floor(side/3), y+floor(side/3));
      point(x+floor(side/3), y+side-floor(side/3));
      point(x+floor(side/2), y+floor(side/2));
      point(x+side-floor(side/3), y+floor(side/3));
      point(x+side-floor(side/3), y+side-floor(side/3));
      break;
    case 6:
      point(x+floor(side/3), y+floor(side/3));
      point(x+floor(side/3), y+side-floor(side/3));
      point(x+floor(side/3), y+floor(side/2));
      point(x+side-floor(side/3), y+floor(side/2));
      point(x+side-floor(side/3), y+floor(side/3));
      point(x+side-floor(side/3), y+side-floor(side/3));
      break;
  }
}

void interactiveDices() {
  int posX = (mouseX-dx) % (side+spc);
  int pX = (mouseX-posX);
  int posY = (mouseY-dy) % (side+spc);
  int pY = (mouseY-posY);
  
  if (mouseX<scrWidth-((scrWidth/2+4) % (side+spc))  && mouseY<scrHeight-(scrHeight % (side+spc))) {
    noStroke();
    fill(backgroundColor);
    rect(pX,pY-1,side+4,side+3);
    drawDiceFace(pX+2, pY, round(random(0.5,6.5)));
  }
}