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:
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