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

//	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;
    }
  }

Output:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.