Javascript Shift or Splice Question

jersmi's picture

This js works fine as is, just continues to fill the queue until potential fps death.... I am trying to insert a shift (or splice) so I can limit the queue size for the "points" array. I am having trouble and now my brain stopped working.... Help?

{
   if(reset || result.points == undefined)
      result.points = new Array;
   if(draw)
   {
      var point = new Array;
      point[0] = inputX;
      point[1] = inputY;
      point[2] = inputZ;
      point[3] = lineWidth;
      transformPoint(point, xTheta*Math.PI/180, yTheta*Math.PI/180, zTheta*Math.PI/180);
      result.points.push(point);
 
   }   

fsk's picture
Re: Javascript Shift or Splice Question

{
    if(reset || result.points == undefined)
        result.points = new Array;
    if(draw)
    {
        var point = new Array;
        point[0] = inputX;
        point[1] = inputY;
        point[2] = inputZ;
        point[3] = lineWidth;
        transformPoint(point, xTheta*Math.PI/180, yTheta*Math.PI/180, zTheta*Math.PI/180);
        result.points.push(point);
        if(result.points.length>lengthLimit)result.points.shift();
    } 
}

something like this?

jersmi's picture
Re: Javascript Shift or Splice Question

Thanks very much, fsk.

Instead of:
if(result.points.length>lengthLimit)result.points.shift();

I had:

if(points.length>lengthLimit)result.points.shift();

sigh...

gtoledo3's picture
Re: Javascript Shift or Splice Question

I think you might ALSO be able to do something like:

       if(result.points.length>lengthLimit)result.points.splice(0,result.points.length-lengthLimit);
 
return result;

To get the count to back down after it's increased... I don't know, I didn't test it, because it's just a snippet.

Uhh, somewhere around here I have tons of compositions that do a queue with a splice function, that smokris introduced me to, which is basically:

function (__structure Queue) main (__number Value[3], __index Size)
{
   var result = new Object();
   _Queue.push([Value[0], Value[1], Value[2]]);
     if (_Queue.length > Size) _Queue.splice(0,_Queue.length-Size);           result.Queue = _Queue;
   return result;
}

jersmi's picture
Re: Javascript Shift or Splice Question

Hey, thanks, George. Yeah, that splice method Smokris added to the dev js queue example -- I yanked it out of one of your comps along the way and have it on hand. Now that the syntax is in order with this one it's easy to swap out shift, splice, etc.