Random Square 2

JohnC


Based on: Random Squares by Charles Csuri, 1976

Category: direct


Description:

Better version This sketch is running in the browser.






/* 
Part of the ReCode Project (http://recodeproject.com)
Based on "Random Squares" by Charles Csuri
Originally published in "Computer Graphics and Art" v1n2, 1976
Copyright (c) 2023 JohnC - OSI/MIT license (http://recodeproject/license).
*/

/* @pjs pauseOnBlur="true"; */

void setup(){
  size(600,600);
  background(255);
  rectMode(CENTER);
  noFill();
  strokeWeight(2);
  float avgMSqSz = 40;
  float avgSSqSz = 15;
  float avgLSqSz = 100;
  
  float margin = 50;
  
  int verticalMediumLineNo = floor(random(12,14));
  //vertical medium squares
  for(int i = 0; i < verticalMediumLineNo; i++){
    float sz = avgMSqSz * random(.9,1.1);
    float gap = (width - 2 * margin - avgMSqSz) / (verticalMediumLineNo - 1);
    float x = margin + avgMSqSz / 2 + i * gap;
    x += (i == 0 || i == verticalMediumLineNo - 1) ? random(-10,10) : random(-gap * 0.4, gap * 0.4);
    int numOfSq = floor(random(4,7));
    for (int j = 0; j < numOfSq; j ++){
      float y = random(margin + sz/2, height - (margin + sz/2));
      rect(x, y, sz, sz, 2);
    }
  }
  
  int horizontalMediumLineNo = floor(random(12,14));
  //horizontal medium squares
  for(int i = 0; i < horizontalMediumLineNo; i++){
    float sz = avgMSqSz * random(.9,1.1);
    float gap = (height - 2 * margin - avgMSqSz) / (horizontalMediumLineNo - 1);
    float y = margin + avgMSqSz / 2 + i * gap;
    y += (i == 0 || i == horizontalMediumLineNo - 1) ? random(-10,10) : random(-gap * 0.4, gap * 0.4);
    int numOfSq = floor(random(4,7));
    for (int j = 0; j < numOfSq; j ++){
      float x = random(margin + sz/2, width - (margin + sz/2));
      rect(x, y, sz, sz, 2);
    }
  }
  
  int verticalSmallLineNo = floor(random(16,18));
  //vertical small squares
  for(int i = 0; i < verticalSmallLineNo; i++){
    float sz = avgSSqSz * random(.9,1.1);
    float gap = (width - 2 * margin - avgSSqSz - avgMSqSz) / (verticalSmallLineNo - 1);
    float x = margin + avgSSqSz / 2  + avgMSqSz/2 + i * gap;
    x += (i == 0 || i == verticalSmallLineNo - 1) ? random(-2,2) : random(-gap * 0.4, gap * 0.4);
    int numOfSq = floor(random(10,16));
    for (int j = 0; j < numOfSq; j ++){
      float y = random(margin + sz/2 + avgMSqSz/2, height - (margin + sz/2 + avgMSqSz/2));
      rect(x, y, sz, sz, 2);
    }
  }
  
  int horizontalSmallLineNo = floor(random(16,18));
  //horizontal small squares
  for(int i = 0; i < horizontalSmallLineNo; i++){
    float sz = avgSSqSz * random(.9,1.1);
    float gap = (height - 2 * margin - avgSSqSz - avgMSqSz) / (horizontalSmallLineNo - 1);
    float y = margin + avgSSqSz / 2 + avgMSqSz/2 + i * gap;
    y += (i == 0 || i == horizontalSmallLineNo - 1) ? random(-2,2) : random(-gap * 0.4, gap * 0.4);
    int numOfSq = floor(random(10,16));
    for (int j = 0; j < numOfSq; j ++){
      float x = random(margin + sz/2 + avgMSqSz/2, width - (margin + sz/2 + avgMSqSz/2));
      rect(x, y, sz, sz, 2);
    }
  }
  
  int largeSquareNo = floor(random(3, 6));
  for (int i = 0; i < largeSquareNo; i ++){
    float sz = avgLSqSz * random(.9,1.1);
    float x = random(margin + sz/2, width - (margin + sz/2));
    float y = random(margin + sz/2, height - (margin + sz/2));
    rect(x, y, sz, sz, 2);
  }
}