Structure Square Series Inwards

Alireza Sarabchi


Based on: Structure Square Series Inwards by Roger Coqart, 1976

Category: direct


Description:

No description provided This sketch is running in the browser.






/* 
Part of the ReCode Project (http://recodeproject.com)
Based on "Structure Square Series Inwards" by Roger Coqart
Originally published in "Computer Graphics and Art" v1n3, 1976
Copyright (c) 2022 Alireza Sarabchi - OSI/MIT license (http://recodeproject/license).
*/

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

let squareSize = 25;
let paddings = 25;
const columns = 15;

function setup() {
  createCanvas(400, 400);
  background(255);
  noFill();
  stroke(1);
  rectMode(CENTER);

  push();
  translate(paddings, paddings);
  for (let i = 0; i < 8; i++) {
    let x = i * squareSize;
    let factor = (i + 1) % 8;
    drawSquare(x, x, 20, 8 - (factor === 0 ? 8 : factor));
    for (let j = 14 - i; j > i; j--) {
      drawSquare(x, j * squareSize, 20, 8 - (factor === 0 ? 8 : factor));
      drawSquare(
        (14 - i) * squareSize,
        j * squareSize,
        20,
        8 - (factor === 0 ? 8 : factor)
      );
      drawSquare(j * squareSize, x, 20, 8 - (factor === 0 ? 8 : factor));
      drawSquare(
        j * squareSize,
        (14 - i) * squareSize,
        20,
        8 - (factor === 0 ? 8 : factor)
      );
    }
  }
  resetMatrix();
  pop();
}

function draw() {}

function drawSquare(x, y, size, lines = 0) {
  rect(x, y, size);
  let halfSize = size / 2;
  let quarterSize = size / 4;
  let position = 0;
  let prevPosition = 0;
  let selectedLines = [];

  while (lines > 0) {
    while (
      position === prevPosition ||
      selectedLines.indexOf(position) !== -1
    ) {
      position = Math.floor(random(1, 9));
    }
    selectedLines.push(position);
    prevPosition = position;
    switch (position) {
      case 1:
        line(x - halfSize, y, x + halfSize, y);
        break;
      case 2:
        line(x, y - halfSize, x, y + halfSize);
        break;
      case 3:
        line(x - halfSize, y - halfSize, x + halfSize, y + halfSize);
        break;
      case 4:
        line(x + halfSize, y - halfSize, x - halfSize, y + halfSize);
        break;
      case 5:
        line(x - halfSize, y, x, y - halfSize);
        break;
      case 6:
        line(x - halfSize, y, x, y + halfSize);
        break;
      case 7:
        line(x + halfSize, y, x, y - halfSize);
        break;
      case 8:
        line(x + halfSize, y, x, y + halfSize);
        break;
    }

    lines--;
  }
}