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:

// 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 pList = new List();
    List indexList = new List();

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

    int counter = 0;

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

        if (w.x == 0 & amp; & w.y == 0 & amp; & 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 & lt;= 1){
            x++;
        }
        else if (choice & lt;= 3){
            x--;
        }
        else if (choice & lt;= 5){
            y++;
        }
        else if (choice & lt;= 7){
            y--;
        }
        else if (choice & lt;= 9){
            z--;
        }
        else if (choice & lt;= 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 & lt; 0 || z == upperBound){
            x = 0;
            y = 0;
            z = 0;
        }

        return choice;
    }
}
Output:

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124