Random Walker Spiral using Probabilities

Hi Everyone,

I am working on an algorithm to create a random walker using probabilities. I want to have a controlled random spiral shape. Like this:

IMG_2183

I have create a random walker class first. When i am trying to make it turn, it doesn’t work anymore.

Here is my code:

Walker[] s = new Walker [1];

void setup(){
  size (400,400);
  background(0);

  smooth();
  
  for (int i = 0; i < s.length; i++){
    s[i]  = new Walker (0,0);
  }
  
  frameRate(120);
}

void draw(){
  
  for (int i = 0; i < s.length; i++){
    s[i].display();
    s[i].step();
  }
}

class Walker{
  
  float x,y;
  color c;
  int diam;
  float xdir, ydir;
  
  Walker(float _x, float _y){
    x = _x+5;
    y = _y+5;
    c = 255;
    diam = 5;
  }
  
  void display(){
    translate (width/2, height/2);
    fill(c,10);
    noStroke();
    ellipseMode(CENTER);
    ellipse (x,y,diam,diam);
  }
  
  void step(){
    
    float r = random(1);
    
    //1
    if (x > 0 || x < width/2 && y > 0 || y < height/2){
      
      if (abs(x)>abs(y)){
        if (r < 0.1){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      } else if (abs(y)>abs(x)){
        if(r < 0.1){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      }
    }
 
    //2
    if (x < 0 || x > -width/2 && y > 0 || y < height/2){
      
      if (abs(y)>abs(x)){
        if(r < 0.1){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.7){
          y++;
        } else {
          y--;
        }
      } else if (abs(x)>abs(y)){
        if(r < 0.1){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.5){
          y++;
        } else {
          y--;
        }
      }
    }
    
    //3
    if (x < 0 || x > -width/2 && y < 0 || y > -height/2){
      
      if (abs(x)>abs(y)){
        if (r < 0.3){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.5){
          y++;
        } else {
          y--;
        }
      } else if (abs(y)>abs(x)){
        if (r < 0.5){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.7){
          y++;
        } else {
          y--;
        }
      }
    }
    
    //4
    if (x > 0 || x < width/2 && y < 0 || y > -height/2){
      
      if (abs(y)>abs(x)){
        if (r < 0.5){
          x++;
        } else if (r < 0.6){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      } else if (abs(x)>abs(y)){
        if (r < 0.3){
          x++;
        } else if (r < 0.4){
          x--;
        } else if (r < 0.9){
          y++;
        } else {
          y--;
        }
      }
    }
    
    x = constrain (x,-width/2,width/2);
    y = constrain (y,-height/2, height/2);
  }
}

Can someone help me please?

Thank you,

Rémy

Here’s my approach…

float a = 0;
float d = 5;
float x, y, px = 0, py=0;

void setup(){
  size(800,800);
  stroke(255);
  background(0);
}

void draw(){

  translate(400,400);
  
  float r = random(0,0.5);
  x = (r+d)*a * cos(a);
  y = (r+d)*a * sin(a);
  a += 0.05;
  
  line(px, py, x, y);
  px = x;
  py = y;
}

I don’t know if that jives with what you want though.

2 Likes

I haven’t tested the code, but would something like this work?

float r = 0;
PVector position;
float radius = 1;

void setup() {
  size(600, 600);

  background(255);
  stroke(0);
  
  position = new PVector(width/2, height/2);

  strokeWeight(2);
}

void draw() {
  float ran = random(100); // Pick a random number out of 100
  if(ran > 10) { // If it's more than ten, turn left (90% chance)
    r+= 0.1/radius;
  } else {
    r-= 0.1/radius; // Otherwise turn the other way
  }
  
  position.add(PVector.fromAngle(r)); // Update the position based on the angle

  point(position.x, position.y);
  radius+=0.01; // Increase the radius
}
1 Like

Hi @Sarah, Hi @TfGuy44,

Thank you for your fast answers. Your proposals are really interesting and i have to say that i thought to use an angle or/and the cos & sin fonctions but wanted to try a more accidental manner using probabilities.

My aim is not to create a spiral with a shaky effect but more a spiral shape from a random behavior (don’t know if i make myself clear here, sorry if it is not the case).

In my drawing, i tried to describe the different parts of the sketch where a walker change his route under the influence of probabilities using the abs() values of x & y.

Does that make sens to you? I might not taking the easiest way to solve that problem, i don’t know…

Thank you,

Rémy

Any one?

Thank you,

Rémy

So from what I understand, you want a walker that has a higher chance of going in one direction that the other, making it move in a spiral motion?

1 Like

Hi @Sarah

Yes, exactly. That is why i used the probabilities regarding where the walker is in space.

Thank you,

Rémy

This is how I would do it:

PVector position;
PVector velocity = new PVector(1,0);

void setup() {
  size(800, 800);
  stroke(255);
  background(0);
  position = new PVector(width/2, height/2);
  strokeWeight(2);
}

void draw() {
  velocity.add(-velocity.y/10,velocity.x/10); // Apply perpendicular vector
  position.add(velocity); // Update position
  point(position.x, position.y);
}

It adds a perpendicular vector to the velocity, which makes it slightly turn.

1 Like

Really interesting the PVector solution. I will run with that.
Just being curious, do you know what was wrong in my code?

Thank you @Sarah