Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions RANDOMLY DISTRIBUTE INSIDE CIRCLE

  • RANDOMLY DISTRIBUTE INSIDE CIRCLE

    Posted by Saulo Dal pozzo on March 19, 2025 at 9:13 am

    Hello!

    I’m trying to randomly distribute several (a lot of) layers inside the area of a circle of radius “r” (controlled by a slider). I have this:

    const r = 3000; // AT 3000 (>2200) JUST SO IT DOESN’T CRASH

    seedRandom(index,true);

    var center = [width/2,height/2];

    var pos = [0,0];

    var dist = length(pos,center); // BY DEFAULT IT’S AT 2200

    while (dist > r) pos = random([width,height]);

    pos

    But as soon as the radius get smaller than the length, After Effects crashes… What am I doing wrong?

    Saulo Dal pozzo replied 1 month, 1 week ago 2 Members · 3 Replies
  • 3 Replies
  • Saulo Dal pozzo

    March 19, 2025 at 9:57 am

    Found another thread that helped me get the distribution in the image (I also needed to avoid the area of the screen)

    The expression ended up like this:

    seedRandom(index,true);

    var center = [width/2,height/2];

    var r = center[0]+thisComp.layer(“NULL “).effect(“RADIUS”)(“Slider”);

    var pX = random(center[0]-r,center[0]+r);

    if (pX > 0 && pX < width){

    pY = center[1];

    while (pY > 0 && pY < height) pY = random(center[1]-r,center[1]+r);

    }else{

    pY = random(center[1]-r,center[1]+r);

    }

    var pos = [pX, pY];

    if (length(center,pos)>r){

    pos = center + r*(pos-center)/length(center,pos);

    }

    pos

    Now I’ll try to find a way to avoid the layers from peeking in at the screen based on their size. I also feel that it’s not that evenly distributed, but it’s fine for now

  • Dan Ebberts

    March 19, 2025 at 7:07 pm

    I think the whole problem gets simpler if you switch from rectangular coordinates to polar coordinates (radius and angle). Then a circular random distribution centered around the comp center becomes something like this:

    c = [thisComp.width,thisComp.height]/2; // center
    rMax = 500; // max radius
    seedRandom(index,true);
    a = random(Math.PI*2);
    r = random(rMax);
    x = r*Math.cos(a);
    y = r*Math.sin(a);
    c + [x,y]

    It gets more complicated if you have a rectangular exclusion zone in the center of the field, but by using a little trig, you can avoid the while loop that can hang AE:

    c = [thisComp.width,thisComp.height]/2; // center
    w = 400; // width of rectangular exclusion area
    h = 300; // height of rectangular exclusion area
    rMax = 500; // max radius (must be big enough to enclose the rectangle)
    seedRandom(index,true);
    a = random(Math.PI/2);
    aMid = Math.atan2(h/2,w/2);
    if (a < aMid){
    rMin = (w/2)/Math.cos(a);
    }else{
    rMin = (h/2)/Math.cos(Math.PI/2 - a);
    }
    r = random(rMin,rMax);
    x = r*Math.cos(a);
    y = r*Math.sin(a);
    n = Math.floor(random(4));
    if (n == 1){
    x = -x;
    }else if (n == 2){
    x = -x;
    y = -y;
    }else if (n == 3){
    y = -y;
    }
    c + [x,y]
  • Saulo Dal pozzo

    March 20, 2025 at 8:20 am

    Thanks Dan! I still have trouble with all the Math functions, so generally I avoid them. I’ll try to make sense of what you did 😉

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