Construct2_3d ›

Construct2

click to generate new pattern

See /f0blog/useless-supercollider-class-no-2/

//
//  construct2.js
//
//  Copyright (c) 2001 Fredrik Olofsson Musikproduktion. All rights reserved.
//
//  2001 original code in java
//  010502 max patch (rev.011004)
//  040920 ported to processing
//  060227 3d version for processing
//  071201 ported to sc (RedConstruct)
//  200326 ported to p5js

//----------------------------------------------------------------------------------------------

let c;
let i, step_X, step_Y;
let x= 10;
let y= 10;
let dx= 1;
let dy= 1;
let v= 1;
let m= 1;
let col= 4;
let row= 3;
let mov_X= 95;
let mov_Y= 95;
let max_X;
let max_Y;

function setup() {
  let d= new Date().getMilliseconds();
  randomSeed(d);

  let div= select('#sketch');
  let cnv= createCanvas(div.width, div.height);
  cnv.parent('sketch');

  frameRate(60);  //frameRate(30);
  max_X= width;
  max_Y= height;
  background(255);
  nytt();
}

function mouseClicked() {
  background(255);                         //rensa skärmen
  nytt();                                  //nya värden
}

function keyTyped() {
  if(key==' ') {
    background(255);                         //rensa skärmen
    nytt();                                  //nya värden
  }
}

function draw() {
  if (int(random(1881))==0) {                //lite då och då
    background(255);                         //rensa skärmen
    nytt();                                  //nya värden
  }
  stroke(c, 55, 66);
  i++;
  drawFO();                                  //rita punkt(er)

  x= x + (dx * step_X);                      //flytta position x
  if (x < 1)                                 //om vänster kant
  dx= int(random(2));           //vänd eller stå still i x-led
  else if (x > (max_X / col))                //om höger kant
  dx= 0 - int(random(2));       //vänd eller stå still i x-led

  y= y + (dy * step_Y);                      //flytta position y
  if (y < 1)                                 //om övre kanten
  dy= int(random(2));           //vänd eller stå still i y-led
  else if (y > (max_Y / row))                //om nedre kanten
  dy= 0 - int(random(2));       //vänd eller stå still i y-led

  switch (v) {
    case 1:
    variant1();
    break;
    case 2:
    variant2();
    break;
    case 3:
    variant3();
    break;
    case 4:
    variant4();
    break;
    case 5:
    variant5();
    break;
    case 6:
    variant6();
    break;
  }
}

//----------------------------------------------------------------------------------------------
function variant1() {                            //1. slumpa riktningar individuellt
  if (int(random(100)) >= mov_X)
  slumpaRiktningX();
  if (int(random(100)) >= mov_Y)
  slumpaRiktningY();
}
function variant2() {                            //2. slumpa alltid riktningar tillsammans
  slumpaRiktningXY();
}
function variant3() {                            //3. slumpa riktningar individuellt
  if (int(random(100)) >= mov_X)
  if (dx == 0 && x >= 1)                   //dra åt vänster
  dx= -1;
  else
  dx= 0;
  if (int(random(100)) >= mov_Y)
  if (dy == 0 && y >= 1)                 //dra uppåt
  dy= -1;
  else
  dy= 0;
}
function variant4() {                            //4. slumpa riktningar individuellt
  if (int(random(100)) >= mov_X)
  if (dx == 0 && x >= 1)                   //dra åt vänster
  dx= -1;
  else
  dx= 0;
  if (int(random(100)) >= mov_Y)
  if (dy == 0 && y <= max_Y / row)       //dra nedåt
  dy= 1;
  else
  dy= 0;
}
function variant5() {                            //5. slumpa riktningar individuellt
  if (int(random(100)) >= mov_X)
  if (dx == 0 && x <= max_X / col)         //dra åt höger
  dx= 1;
  else
  dx= 0;
  if (int(random(100)) >= mov_Y)
  if (dy == 0 && y >= 1)                 //dra uppåt
  dy= -1;
  else
  dy= 0;
}
function variant6() {                            //6. slumpa riktningar individuellt
  if (int(random(100)) >= mov_X)
  if (dx == 0 && x <= max_X / col)         //dra åt höger
  dx= 1;
  else
  dx= 0;
  if (int(random(100)) >= mov_Y)
  if (dy == 0 && y <= max_Y / row)       //dra nedåt
  dy= 1;
  else
  dy= 0;
}

//----------------------------------------------------------------------------------------------
function nytt() {
  v= int(random(6)) + 1;
  m= int(random(16)) + 1;
  col= int(random(12)) + 1;
  row= int(random(7)) + 1;
  step_X= 1;
  //step_X= int(random(2)) + 1;
  step_Y= 1;
  //step_Y= int(random(2)) + 1;
  slumpaRiktningXY();
  c= int(random(255));
}

function slumpaRiktningXY() {
  while ((dx==0) && (dy==0)) {               //kolla att inte båda riktningar blir 0
    dx= int(random(2));
    dy= int(random(2));
  }
}
function slumpaRiktningX() {
  dx= int(random(2));
}
function slumpaRiktningY() {
  dy= int(random(2));
}

//----------------------------------------------------------------------------------------------
function drawFO() {
  let i, j, iCol, jRow;
  for (i= 0; i < col; i++) {
    for (j = 0; j < row; j++) {
      iCol= round((max_X / col) * i);
      jRow= round((max_Y / row) * j);
      switch (m) {                           //kolla mapping
        case 1:
        mapping1(i, j, iCol, jRow);
        break;
        case 2:
        mapping2(i, j, iCol, jRow);
        break;
        case 3:
        mapping3(i, j, iCol, jRow);
        break;
        case 4:
        mapping4(i, j, iCol, jRow);
        break;
        case 5:
        mapping5(i, j, iCol, jRow);
        break;
        case 6:
        mapping6(i, j, iCol, jRow);
        break;
        case 7:
        mapping7(i, j, iCol, jRow);
        break;
        case 8:
        mapping8(i, j, iCol, jRow);
        break;
        case 9:
        mapping9(i, j, iCol, jRow);
        break;
        case 10:
        mapping10(i, j, iCol, jRow);
        break;
        case 11:
        mapping11(i, j, iCol, jRow);
        break;
        case 12:
        mapping12(i, j, iCol, jRow);
        break;
        case 13:
        mapping13(i, j, iCol, jRow);
        break;
        case 14:
        mapping14(i, j, iCol, jRow);
        break;
        case 15:
        mapping15(i, j, iCol, jRow);
        break;
        case 16:
        mapping16(i, j, iCol, jRow);
        break;
      }
    }
  }
}

//----------------------------------------------------------------------------------------------
function mapping1(i, j, iCol, jRow) {    //no flip
  point(x + iCol, y + jRow);
}
function mapping2(i, j, iCol, jRow) {    //flip all x
  point(mirrorX(x) + iCol, y + jRow);
}
function mapping3(i, j, iCol, jRow) {    //flip all y
  point(x + iCol, mirrorY(y) + jRow);
}
function mapping4(i, j, iCol, jRow) {    //flip all xy
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
}
//--
function mapping5(i, j, iCol, jRow) {    //flip odd col x
  if (i % 2 == 1)
  point(mirrorX(x) + iCol, y + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping6(i, j, iCol, jRow) {    //flip odd col y
  if (i % 2 == 1)
  point(x + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping7(i, j, iCol, jRow) {    //flip odd col xy
  if (i % 2 == 1)
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
//--
function mapping8(i, j, iCol, jRow) {    //flip odd row x
  if (j % 2 == 1)
  point(mirrorX(x) + iCol, y + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping9(i, j, iCol, jRow) {    //flip odd row y
  if (j % 2 == 1)
  point(x + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping10(i, j, iCol, jRow) {    //flip odd row xy
  if (j % 2 == 1)
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
//--
function mapping11(i, j, iCol, jRow) {    //flip odd col & even row x, flip even col & odd row x
  if (i % 2 == 1 && j % 2 == 0)
  point(mirrorX(x) + iCol, y + jRow);
  else if (i % 2 == 0 && j % 2 == 1)
  point(mirrorX(x) + iCol, y + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping12(i, j, iCol, jRow) {    //flip odd col & even row y, flip even col & odd row y
  if (i % 2 == 1 && j % 2 == 0)
  point(x + iCol, mirrorY(y) + jRow);
  else if (i % 2 == 0 && j % 2 == 1)
  point(x + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping13(i, j, iCol, jRow) {    //flip odd col & even row xy, flip even col & odd row xy
  if (i % 2 == 1 && j % 2 == 0)
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
  else if (i % 2 == 0 && j % 2 == 1)
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
//--
function mapping14(i, j, iCol, jRow) {    //flip even col & even row x, flip odd col & odd row x
  if (i % 2 == 0 && j % 2 == 0)
  point(mirrorX(x) + iCol, y + jRow);
  else if (i % 2 == 1 && j % 2 == 1)
  point(mirrorX(x) + iCol, y + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping15(i, j, iCol, jRow) {    //flip even col & even row y, flip odd col & odd row y
  if (i % 2 == 0 && j % 2 == 0)
  point(x + iCol, mirrorY(y) + jRow);
  else if (i % 2 == 1 && j % 2 == 1)
  point(x + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}
function mapping16(i, j, iCol, jRow) {    //flip even col & even row xy, flip odd col & odd row xy
  if (i % 2 == 0 && j % 2 == 0)
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
  else if (i % 2 == 1 && j % 2 == 1)
  point(mirrorX(x) + iCol, mirrorY(y) + jRow);
  else
  point(x + iCol, y + jRow);
}

//----------------------------------------------------------------------------------------------
function mirrorX(ox) {
  return round((max_X / col) - ox);
}
function mirrorY(oy) {
  return round((max_Y / row) - oy);
}