Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Normalize Vector Issue

  • Normalize Vector Issue

    Posted by Jamie Bradshaw on September 12, 2010 at 9:29 am

    Hi all.

    I have the following expression code on a Text Layer’s Source Text (for debugging purposes).

    It’s working fine except that I’m only getting different random values for each array (pArray1) when I normalize the vector (normalise = true)

    In other words, if I set the variable normalise to false then each of the pArray1 amounts come out the same.

    Any idea why?

    Thanks,
    Jamie


    seedRandom(index, true);

    parentType1 = [0, 0, 0];
    parentAmount1 = 4;
    minMax1 = [-1, 1];
    normalise1 = true;

    pArray1 = generateParents(parentType1, parentAmount1, minMax1, normalise1);

    str1 = "";
    for (n1 = 0; n1 < parentAmount1; n1++) {
    str1 += "Parent " + n1 + "\r";
    for (n2 = 0; n2 < parentType1.length; n2++) {
    str1 +=
    "\t" + "[" + n2 + "]: " + pArray1[n1][n2] + "\t" + "\r";

    }
    str1 += "\t" + "length: " + length(pArray1[n1]) + "\r" +
    "\r";
    }

    function generateParents(pType_1, pAmount_1, minMax_1, norm_1) {
    var pArray_1 = new Array();
    var pType_2 = new Array();
    for (n_2 = 0; n_2 < pAmount_1; n_2++) {
    for (n_1 = 0; n_1 < pType_1.length; n_1++) {
    pType_2[n_1] = random(minMax_1[0], minMax_1[1]);
    }
    if (norm_1) {
    pType_3 = normalize(pType_2);
    } else {
    pType_3 = pType_2;
    }
    pArray_1[n_2] = pType_3;
    }
    return(pArray_1);
    }

    JimJam•Graphics
    https://www.jimjamgraphics.com/

    Jamie Bradshaw replied 15 years, 8 months ago 1 Member · 1 Reply
  • 1 Reply
  • Jamie Bradshaw

    September 13, 2010 at 9:27 am

    Aha! Caught it!

    For anyone who might be vaguely interested it was because I needed to declare the var pArray_2 = new Array(); inside the first for loop. I’m guessing so that it could clear the previous info stored in the array which it wasn’t doing if I didn’t normalize the vector.

    Here is the working code with added comments to maybe make it clearer.


    seedRandom(index , true);

    // The variables
    parentType1 = [0, 0, 0]; //define the parent type, in this case a 3d vector
    parentAmount1 = 4; //define how many original parents
    minMax1 = [-1, 1]; //define the min and max range of random values
    normalise1 = true; //normalise the vector or not
    generations1 = 20; //how many generations or iterations to calculate

    pArray1 = generateParents(parentType1, parentAmount1, minMax1, normalise1);

    // generate the debug text so I can assess what's happening
    str1 = "";
    for (n1 = 0; n1 < parentAmount1; n1++) {
    str1 += "Parent " + n1 + "\r";
    for (n2 = 0; n2 < parentType1.length; n2++) {
    str1 +=
    "\t" + "[" + n2 + "]: " + pArray1[n1][n2] + "\t" + "\r";
    }
    str1 += "\t" + "length: " + length(pArray1[n1]) + "\r" +
    "\r";
    }

    // ***FUNCTIONS***

    // this function returns an array of original parents
    function generateParents(pType_1, pAmount_1, minMax_1, norm_1) {
    var pArray_1 = new Array();
    var pType_3 = new Array();
    for (n_2 = 0; n_2 < pAmount_1; n_2++) {
    var pType_2 = new Array();
    for (n_1 = 0; n_1 < pType_1.length; n_1++) {
    pType_2[n_1] = random(minMax_1[0], minMax_1[1]);
    }
    if (norm_1 == false) {
    pType_3 = pType_2;
    } else {
    pType_3 = normalize(pType_2);
    }
    pArray_1[n_2] = pType_3;
    }
    return(pArray_1);
    }

    JimJam•Graphics
    https://www.jimjamgraphics.com/

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