Javascript Min/Max

jersmi's picture

More js, little stuff. How can I output min/max out of this array?

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

I saw cybero's page has methods, but still can't get it: http://www.cybero.co.uk/QuartzComposerJavaScriptGuide-Eng/index.html

cybero's picture
Re: Javascript Min/Max

Depends if you are trying to use the MAX_NUM, MIN_NUM or the Math max and min. See attached example of a minimum, maximum value reading of Mouse positions. [Math.max & Math.min] . These are not read into an array, nor queued into a structure in this example.

PreviewAttachmentSize
MousePositionMathMax_MathMin_Value.qtz7.53 KB

jersmi's picture
Re: Javascript Min/Max

Thanks, cybero. It is the array/queue that's throwing me.

cybero wrote:
MAX_NUM, MIN_NUM or the Math max and min.
What's the difference?

cybero's picture
Re: Javascript Min/Max

OK, basically MAX_VALUE [not MAX_NUM, my bad] sets a ceiling above which any value will be read as infinite. It is set to a static property , a value of 1.7976931348623157e+308 . MIN_VALUE sets a floor below which any other value is zero. Also a static property. These can be used to set conditions. They are the lowest and the highest numbers in Javascript. Useful in an if..else setup, for instance.

I think that you need the more applicable Math.max and Math.min. Math.max returns the highest number in an array, whilst Math.min returns the lowest number in an array.

The primary question I've got , is in what way are you going to collect your data from the structure resulting from your Queue patch. Looking at the code you posted, your queueing two inputs, which two inputs could be scripted as variables.

In regards of obtaining the max / min of the inputs I guess , given your setting of outputs for min 0 and min 1 you don't want to combine the two inputs , but process them as separate streams of data.

Interestingly enough, the _Queue function results in a structure that can have a size of 1 , yet output three members despite having only two inputs, because you set a 'dummy' zero in the Queue array.

jersmi's picture
Re: Javascript Min/Max

Good info, cybero. Thanks again. Have to think about it for my situation.

Here's my comp. Included with the comp is a test .plist file for the Value Historian, in case it doesn't load.

I want the min and max values to define the bounding box around the quad polygon. I have two methods producing the same results, one using javascript, one using an Iterator without a renderer. I don't know why I am seeing these bounding box results.


Question 2: How can I truncate strings displaying numbers? The math expression patch "leaks" using floor(a * 100)/100

PreviewAttachmentSize
StructMinMax.qtz94.25 KB
want.jpg
want.jpg5.84 KB
is.jpg
is.jpg8.25 KB

jersmi's picture
Re: Javascript Min/Max

EDIT: I revised both recent posts.

cybero wrote:
...you don't want to combine the two inputs , but process them as separate streams of data.
Point taken. This js method separates the data, but as mentioned I don't know understand the results right now. Attached pics show min and max output. Why does the max out show a null structure?

var _Mins = []
var _Maxs = []
 
function (__structure min, __structure max) main (__structure Points)
{
   var result = new Object()
 
   if (Points != null) {   
      for (var i=0; i < Points.length; ++i) {
      m = 0
 
      // Why doesn't this work?
      x = Points[0][0]
      y = Points[0][1]
 
      _Mins[m++] = [Math.min(x, Points[i][0]), Math.min(y, Points[i][1])];
      _Maxs[m++] = [Math.max(x, Points[i][0]), Math.max(y, Points[i][1])];
 
 
      }
   }   
   result.min = _Mins
   result.max = _Maxs
   return result
 
}
PreviewAttachmentSize
minOutput.jpg
minOutput.jpg12.19 KB
maxOutput.jpg
maxOutput.jpg13.23 KB

jersmi's picture
Re: Javascript Min/Max

And finally, question 3: why does the quad in the comp draw with five points when the queue is set to four members?

cybero's picture
Re: Javascript Min/Max

just picked up on this and the other post - only got back online having been 'off site' for most of today.

cybero's picture
Re: Javascript Min/Max

I think of it being a little like doing origami, a rectangular sheet of paper. It seems to fold.

You've got a sample of x,y co-ordinates.

They can seem to cross over each other.

Just try feeding the points as vertices to a Mesh Creator, it isn't just the Quad Structure patch that seems to have a non - quadrilateral, regular or irregular shape.

It seems to be partially the result of the rapid change and swap over of mouse input / position , which positional data is not recorded to necessarily make a quad like shape in the first instance. Using the Value Historian kind of freezes that into position. So too does taking a snapshot.

The way it looks to me is that the quad is folding over itself.

PreviewAttachmentSize
StructMinMax_00.png
StructMinMax_00.png9.78 KB

jersmi's picture
Re: Javascript Min/Max

Okay, I see the points folding over, makes sense. That's a nice origami reference. :)

It's the javascript min/max (see above) that I can't get yet. The rollovers showing the structure values show that the min/max is not calculating correctly.

I thought the js for min/max would deal with queue size without caring about order. Maybe the order is mucking it up?

jersmi's picture
Re: Javascript Min/Max

Progress, thanks to Achim and the QC mailing list.

PreviewAttachmentSize
StructMinMax_YES2.qtz37.64 KB