random walk 3D [boundary + clusters] 2.0

Random Walk 1.4.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.4.1
 private void RunScript(int seed, int time, ref object A, ref object B, ref object C, ref object D, ref object E)
  {
    List <Point3d> pList = new List<Point3d>();
    List <Point3d> pListEx = new List<Point3d>();

    List <int> xList = new List<int>();
    List <int> indexList = new List<int>();

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

    int counter = 0;
    indexList.Add(counter);

    for(int i = 0; i < time; i++){
      int rnd = random.Next(0, 6);
      w.step(rnd);

      if(w.boundRebounceCount < 100){
        pListEx.Add(w.pos());
      }
      else{
        pList.Add(w.pos());
      }
      counter++;


      if(indexList.Count() == 1){
        indexList[0] = counter;
      }

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



    A = pList;
    B = xList;
    C = counter;
    D = indexList;
    E = pListEx;

  }

  // <Custom additional code> 
  public class Walker
  {
    public double x;
    public double y;
    public double z;
    public int bound;
    public int upperBound;
    public int rnd;
    public int boundRebounceCount;
    public int explosionNum;

    //Constructor that takes arguments.
    public Walker(){
      x = 1.0;
      y = 1.0;
      z = 1.0;
      bound = 10;
      upperBound = 100;
      rnd = 0;
      boundRebounceCount = 100;
    }

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

    public int step(int rnd){

      int choice = rnd;
      int explosionNum = 0;

      //GROWING UP

      if(boundRebounceCount == 100){
        if (choice <= 0){
          x++;
        }
        else if(choice <= 1){
          x--;
        }
        else if(choice <= 2){
          y++;
        }
        else if(choice <= 3){ y--; } else if(z > 5 && choice <= 4){
          z--;
        }

        else if(choice <= 5){ z = z + 3; } if (x >= bound || x <= -bound){ boundRebounceCount = explosionNum; } if (y >= bound || y <= -bound){
          boundRebounceCount = explosionNum;
        }
        if (z <= 0 || z >= upperBound){
          boundRebounceCount = explosionNum;
        }
      }

      //EXPLODING
      double factor = 1;



      if(boundRebounceCount == 99){
        x = 0;
        y = 0;
        z = 1;
        boundRebounceCount = 100;
      }

      else if(boundRebounceCount < 99){ if(x >= -bound && choice == 0){
          x = x - factor;
          boundRebounceCount++;
        }
        else if(choice == 0){
          x = x + factor;
          boundRebounceCount++;
        }

        if(x <= bound && choice == 1){ x = x + factor; boundRebounceCount++; } else if(choice == 1){ x = x - factor; boundRebounceCount++; } if(y >= -bound && choice == 2){
          y = y - factor;
          boundRebounceCount++;
        }
        else if(choice == 2){
          y = y + factor;
          boundRebounceCount++;
        }

        if(y <= bound && choice == 3){
          y = y + factor;
          boundRebounceCount++;
        }
        else if(choice == 3){
          y = y - factor;
          boundRebounceCount++;
        }

        if(z <= upperBound && choice == 4){ z = z + factor; boundRebounceCount++; } else if(choice == 4){ z = z - factor; boundRebounceCount++; } if(z >= 10 && choice == 5){
          z = z - factor;
          boundRebounceCount++;
        }
        else if(choice == 5){
          z = z + factor;
          boundRebounceCount++;
        }
      }


      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.