Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Create a random Path on a checkerboard

  • Create a random Path on a checkerboard

    Posted by Daniele Bonazza on October 11, 2024 at 10:36 am

    Hi,

    i’ve wrote an expression to create a random path on a checkerboard (a path formed by segments of fixed length with angles of 90°, like the one in the picture). What I would like to do now is a path that never goes back: if the last segment “goes from left to right” the next one should go up, down or right but not left.

    Can someone help me?

    here’s my expression:

    l = 50 // segment length

    n = 50 // segments

    p = [0,0]; //start point

    right = [l,0]; //directions

    left = [-l,0];

    up = [0,-l];

    down = [0,l];

    dir = [right, left, up, down]; //array of the four directions

    pointsArray = [p]; // my path

    for (i=1; i <= n; i++) {

    seedRandom(i,1);

    d = Math.floor(random(0,4));

    p = p + dir[d];

    pointsArray.push(p);

    }

    createPath(points = pointsArray, inTangents = [], outTangents = [], isClosed = false)

    Daniele Bonazza replied 4 months ago 2 Members · 5 Replies
  • 5 Replies
  • Andrei Popa

    October 11, 2024 at 12:47 pm

    Should the segments touch the path? Can you, for example, have a cross-roads made up with this segments?
    If yes, you will have cases where the segments would go inside a dead end.

    LE.
    I don’t think they will overlap if we do not allow overlap. I will give this some thought and come back with the result.

  • Daniele Bonazza

    October 11, 2024 at 12:54 pm

    This is the second step I would like to try: a path without any crossroad. But for now it would be ok if i can make a path that doesn’t come back.

  • Andrei Popa

    October 11, 2024 at 1:39 pm

    I think a path without crossroads is actually easier to make than one without overlapping.

    For your final purpose, do you need one without crossroads? Or maybe both?

  • Andrei Popa

    October 11, 2024 at 2:38 pm

    Here is a solution that I think works for no overlapping of segments.

    You can modify segmentLenght and segmentNumber to what you need. You can also linke these to slider controllers for changing then easier.

    var segmentLength = 10;
    var segmentNumber = 100;
    seedRandom(index, 1);
    var lastIndex = 0;
    Array.prototype.customIndexOf = function (element) {
    for (var i = 0; i < this.length; i++) {
    if (this[i][0] === element[0] && this[i][1] === element[1]) {
    return i;
    }
    }
    return -1;
    };
    function nextPoint() {
    var crtPoint = points[lastIndex];
    var movement = randomStr(freeDirections[lastIndex]);
    freeDirections[lastIndex] = freeDirections[lastIndex].replace(movement, "");
    doMovement(crtPoint, movement);
    }
    function doMovement(point, movement) {
    var newPoint = point + move[movement];
    myPath.push(newPoint);
    if (points.customIndexOf(newPoint) == -1) {
    lastIndex = points.length;
    points.push(newPoint);
    } else {
    lastIndex = points.customIndexOf(newPoint);
    }
    if (freeDirections[lastIndex] == undefined) {
    freeDirections[lastIndex] = "dulr".replace(opposite[movement], "");
    } else {
    freeDirections[lastIndex] = freeDirections[lastIndex].replace(opposite[movement], "");
    }
    }
    function randomStr(str) {
    return str[Math.floor(Math.random() * str.length)];
    }
    var move = {
    "d": [0, 1],
    "u": [0, -1],
    "l": [-1, 0],
    "r": [1, 0]
    }
    var opposite = {
    "d": "u",
    "u": "d",
    "l": "r",
    "r": "l"
    }
    //initialize the myPath
    //first point has all the directions possible
    freeDirections = ["dulr"];
    //first point is [0,0], this is paired in indexes with the freeDirections array
    var points = [[0, 0]];
    //the points through which we went so far
    var myPath = [[0, 0]];
    for (var i = 0; i < segmentNumber; i++) {
    nextPoint();
    }
    createPath(myPath.map(x => x * segmentLength), [], [], false);
  • Daniele Bonazza

    October 11, 2024 at 3:09 pm

    Thank you very much, Andrei

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy