Pointing Squares


  void setup()
  {
    size(500,500);
    smooth();
    noCursor();
    noLoop(); // Only draw when the mouse moves
  }
  
  // Since the graphics only change when the mouse moves, this keeps the CPU
  // from overworking
  void mouseMoved()
  {
    redraw();
  }
  
  void draw() 
  {
    float leftMargin = 20;
    float topMargin = 20;
    float nbrCols = 20;
    float nbrRows = 20;
    float cellWidth = (width-leftMargin*2)/nbrCols;
    float cellHeight = (height-topMargin*2)/nbrRows;
  
    background(255);
    strokeWeight(cellWidth/2);
    strokeCap(SQUARE);
    for (int y = 0; y < nbrRows; ++y) {
      for (int x = 0; x < nbrCols; ++x) {
        float px = leftMargin + x*cellWidth + cellWidth/2;
        float py = topMargin + y*cellHeight + cellHeight/2;
        float angle = atan2(mouseY-py,mouseX-px);
        pushMatrix();
        translate(px, py);
        rotate(angle);
        line(0,0,cellWidth/2,0);
        popMatrix();
      }
    }
  }