I was hoping someone could help me working out some advanced data reformatting. What I'm hoping for is a function where I can input a value along with an array of key positions like so:
function remap(percentage:Number, keypoints:Array) { ...
The array would start with the minimum and end with the maximum point, with nested key points along the way. For example, I would input something like remap(0.25, [0:0,80:50,100:100] )
and the function would 'imagine' a spline curve graph from (0,0)-(100,100) with a key point of (80,50), then return the y value that is 25% along that graph.
Hopefully that's clear... Any ideas?
The equation for the Hermite Curve is this:
(via Wikipedia)
Where p(t) is the point on the curve at t (percent 0.0 to 1.0)
So, the equation in actionscript would be something like this:
You can see a basic demo here
Still, I am a bit concerned because you're example mentions 3 points (2 control points and one anchor point).
Cubic Curves(Hermite/Catmull-Rom/etc.) have 2 control points and 2 anchor points (equations at power of 3 - cubic)
If you only need one control point, you need to use a Quadratic Curve:
(Image from Adobe Actionscript 3 Documentation)
Cubic Curve:
Quadratic Curve:
(Animations from Wikipedia)
The Quadratic Equation is this:
Which would translate to:
And a bit of test code:
Also, am not clear what you mean by
The code snippets are the formulas written as code, but there are other ways to compute this.
We need to go from P0 to P1 and from P1 to P2. Since you're looking for a Flash/ActionScript solution, we can take advantage of the Point's interpolate() method. So we interpolate between P0 and P1 to get let's say P01 then from P1 to P2 to get P12 and the interpolation through all 3 points will be the interpolation between P01 and P12:
The code looks a bit backwards from what I wrote above because of how the actionscript interpolation is implemented: "The level of interpolation between the two points. Indicates where the new point will be, along the line between pt1 and pt2. If f=1, pt1 is returned; if f=0, pt2 is returned."
UPDATE
Further confused:
are you trying to simply get the y value of the current x along a series of lines (multiple points, 0 anchor points...straight lines) ?
If, so, you can do something like this:
Here's a quick sketch to illustrate the idea: Imagine a right angled triangle where the current line is the hypothenuse(ABC). Now imagine a vertical line from your mouse cursor splitting that triangle into two similar triangle(OO'). The small triangle has the same angles as the large one and it's sides are proportional. You use the ratio between AO and AB to divide AC by and obtain the length of OO' (the y position on the line for that x).
Here's the function:
And a quick demo:
Note that this works if the x values in your points array are in sorted ascending (e.g. your path goes only left to right)
A dirty hack that comes mind is to loop through the pairs of points and store Y values in a lookup table. The number of loops woud be the 'line detail'
Then again, this confuses me:
HTH