Random walking example using strange attractors.

Creative coding work that draws a night view of a city.


A creative coding animation with strange attractors.

It's a creative coding animation made with the 'Processing'.

I got an idea about a random walk that does not use random() but uses strange attractors. I used the De Jong Attractors in this code. And I made some animation.

This code does not display any images on the screen but generates image files for animation. You can make an animation with these files.







 

The 'Processing' code example.

Please feel free to use this example code under the terms of the GPL. To see other works based on my code is my pleasure. And my honor.

// Crosstown Traffic.
// @author @deconbatch
// @version 0.1
// Processing 3.2.1
// 2018.12.23

private class Route {

  float pA, pB, pC, pD;
  float prevXInit, prevYInit;
  float noiseStart;
  float pSize;
  float hueBase;

  Route(float hueBase) {

    pA         = random(-1.0, 1.0);
    pB         = random(-1.0, 1.0);
    pC         = random(-1.0, 1.0);
    pD         = random(-1.0, 1.0);
    prevXInit  = random(-2.0, 2.0);
    prevYInit  = random(-2.0, 2.0);
    noiseStart = random(100.0);
    pSize      = 6.0;

    this.hueBase = hueBase;

  }

  private void drawVehicle(int routeFrom, int routeTo) {

    float prevX     = prevXInit;
    float prevY     = prevYInit;
    float drawPrevX = 0.0;
    float drawPrevY = 0.0;
    
    for (int particleCnt = 0; particleCnt < routeTo; ++particleCnt) {

      // De Jong Attractors
      float currX = sin(TWO_PI * pA * prevY) - cos(TWO_PI * pB * prevX);
      float currY = sin(TWO_PI * pC * prevX) - cos(TWO_PI * pD * prevY);

      // random walk with pSize step
      float drawCurrX = drawPrevX + currX / abs(currX) * (abs(currX) < 1.0 ? 0.0 : 1.0) * pSize;
      float drawCurrY = drawPrevY + currY / abs(currY) * (abs(currY) < 1.0 ? 0.0 : 1.0) * pSize;

      if (particleCnt >= routeFrom) {
        float noiseParam = noiseStart + particleCnt * 0.001;
        fill(
             (hueBase + 60.0 + noise(noiseParam) * 30.0) % 360.0,
             20.0 + noise(noiseParam + 100.0) * 80.0,
             10.0 + noise(noiseParam + 200.0) * 15.0,
             100.0
             );
        ellipse(drawPrevX, drawPrevY, pSize, pSize);
      }

      prevX     = currX;
      prevY     = currY;
      drawPrevX = drawCurrX;
      drawPrevY = drawCurrY;

    }
  }
  
}

void setup() {

  size(720, 720);
  colorMode(HSB, 360, 100, 100, 100);
  imageMode(CENTER);
  rectMode(CENTER);
  smooth();
  noLoop();
  noStroke();
  noFill();

}

void draw() {

  int   frameCntMax    = 24 * 6;
  int   particleCntMax = 24 * 6 * 3;
  int   routeCntMax    = 26;
  float frameDiv       = particleCntMax / frameCntMax;
  float hueBase        = random(360);

  Route rt[]            = new Route[routeCntMax];
  int   vehicleStart[]  = new int[routeCntMax]; 
  int   vehicleLength[] = new int[routeCntMax]; 
  float vehicleSpeed[]  = new float[routeCntMax]; 

  // draw route map on PImage
  translate(width / 2, height / 2);
  blendMode(SCREEN);
  noStroke();
  background(0.0, 0.0, 0.0, 100.0);

  for(int routeCnt = 0; routeCnt < routeCntMax; ++routeCnt) {

    rotate(TWO_PI / routeCntMax);
    
    rt[routeCnt] = new Route(hueBase + map(routeCnt, 0, routeCntMax, 0.0, 60.0));
    rt[routeCnt].drawVehicle(0, particleCntMax);

    vehicleStart[routeCnt]  = floor(random(particleCntMax));
    vehicleLength[routeCnt] = floor(random(frameDiv) * 3.0 + 5.0);
    vehicleSpeed[routeCnt]  = random(-0.8, 0.8);
    
  }

  PImage routeMap = get();

  // vehicle animation
  for (int frameCnt = 0; frameCnt < frameCntMax; ++frameCnt) {

    blendMode(SCREEN);
    noStroke();
    background(0.0, 0.0, 0.0, 100.0);
    image(routeMap, 0.0, 0.0);

    for(int routeCnt = 0; routeCnt < routeCntMax; ++routeCnt) {

      rotate(TWO_PI / routeCntMax);

      float fromPoint = vehicleStart[routeCnt] + frameCnt * frameDiv * vehicleSpeed[routeCnt];
      fromPoint += (fromPoint <= 0.0) ? particleCntMax : 0.0;
      fromPoint -= (fromPoint > particleCntMax) ? particleCntMax : 0.0;
      rt[routeCnt].drawVehicle(floor(fromPoint), floor(fromPoint + vehicleLength[routeCnt]));

    }

    blendMode(BLEND);
    casing(hueBase);
    
    saveFrame("frames/" + String.format("%04d", frameCnt) + ".png");
    
  }

  exit();

}

/**
 * casing : draw fancy casing
 * @param  hueBase : casing color.
 */
private void casing(float hueBase) {
  
    fill(0.0, 0.0, 0.0, 0.0);
    strokeWeight(40.0);
    stroke(hueBase, 60.0, 20.0, 100.0);
    rect(0.0, 0.0, width, height);
    strokeWeight(30.0);
    stroke(0.0, 0.0, 95.0, 100.0);
    rect(0.0, 0.0, width, height);
    
}

/*
Copyright (C) 2018- deconbatch

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/



 

Next Post Previous Post
No Comment
Add Comment
comment url