random walk 3D [boundary + color]


Random Walk 1.3
Inspired by “The Nature of Code” by Daniel Shiffman and it’s python translator Jake Hebbert.
Using C# in Grasshopper for Rhinoceros

[code language=”csharp”]
// Philipp Siedler
// Daniel Shiffman "The Nature of Code"
// Random Walk 1.3

private void RunScript(int seed, int time, ref object A, ref object B)
{
List <Point3d> pList = new List<Point3d>();
List <int> indexList = new List<int>();

Walker w = new Walker();
Random random = new Random(seed);

int counter = 0;

for(int i = 0; i < time; i++){
int rnd = random.Next(0, 15);
w.step(rnd);
pList.Add(w.pos());
counter++;

if(w.x == 0 && w.y == 0 && w.z == 0){
indexList.Add(counter);
counter = 0;
}
}

A = pList;
B = indexList;
}
public class Walker
{
public int x;
public int y;
public int z;
public int bound;
public int upperBound;
public int rnd;

public Walker(){
x = 0;
y = 0;
z = 0;
bound = 15;
upperBound = 150;
rnd = 0;
}

public Point3d pos(){
Point3d posPt = new Point3d(x, y, z);
return posPt;
}

public int step(int rnd){
int choice = rnd;

if (choice <= 1){
x++;
}
else if(choice <= 3){
x–;
}
else if(choice <= 5){
y++;
}
else if(choice <= 7){
y–;
}
else if(choice <= 9){
z–;
}
else if(choice <= 14){
z++;
}

if (x == bound || x == -bound){
x = 0;
y = 0;
z = 0;
}
if (y == bound || y == -bound){
x = 0;
y = 0;
z = 0;
}
if (z < 0 || z == upperBound){
x = 0;
y = 0;
z = 0;
}

return choice;
}
}
[/code]

Output:

[youtube https://www.youtube.com/watch?v=I87lR-lOeXw?rel=0&controls=0&showinfo=0&w=560&h=315]

[youtube https://www.youtube.com/watch?v=joar1FXb5fw?rel=0&controls=0&showinfo=0&w=560&h=315]

Leave a Reply