random walk 2D [8 choices]

Random Walk 1.1
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.1

private void RunScript(int seed, int time, ref object A)
  {
    List  pList = new List();

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

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

    A = pList;
  }

public class Walker
  {
    public int x;
    public int y;
    public int rnd;

    public Walker(){
      x = 0;
      y = 0;
      rnd = 0;
    }

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

    public int step(int rnd){
      int choice = rnd;
      if (choice == 0){
        x++;
      }
      else if( choice == 1){
        x--;
      }
      else if(choice == 2){
        y++;
      }
      else if(choice == 3){
        y--;
      }
      else if(choice == 4){
        x++;
        y++;
      }
      else if(choice == 5){
        x--;
        y++;
      }
      else if(choice == 6){
        x--;
        y--;
      }
      else if(choice == 7){
        x++;
        y--;
      }
      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.