A Flash Developer Resource Site

# Thread: [RESOLVED] Linear Interpolation function?

1. ## [RESOLVED] Linear Interpolation function?

Is there a function in AS3 where it interpolates between 2 values as if it were 0-100? For the After Effects users, it's the linear(val1, val2, 0, 100) I think, where it would "normalize" values 1 and 2 and interpolate as if it were 0-100.

I have 2 circles that are acting as the tape in a cassette tape music player, and as it plays, one spool shrinks as the other grows. It's easy to get the scaling, but I need it to start at scale = .5 and end at 1.

If I just add and subtract values, it'll grow past 1 or get incorrect size results.

Code:
```function changeTape(timeE:Event):void {
var percentPlayed:Number = (channel.position / sound.length);

left_tape.scaleX = 1 - percentPlayed;
left_tape.scaleY = left_tape.scaleX;

right_tape.scaleX = percentPlayed;
right_tape.scaleY = right_tape.scaleX
}```  Reply With Quote

2. Point.interpolate will do that for you - I believe it takes two Points and the scalar between 0-1, and returns a new Point.  Reply With Quote

3. I'll give it a go. Thanks   Reply With Quote

4. I know I'm a little late, but I found this post while I was trying to solve this same problem. Unimpressed with Point.interpolate, I hand crafted this function in AS2 to do the trick. It's actually based on the after effects expression. I hope someone somewhere finds this useful. Enjoy:

// Start of Linear Interpolation Function
//The following is a function that transforms a given value to map onto a different, appropriate scale (interpolation). You can start with any scale and map a given number linearly onto any other number scale. It's pretty robust. It also includes the option to round the number or return a consistently displayed decimal form.

_global.transformvalue = function(InputVariable, OriginalLow, OriginalHigh, NewLow, NewHigh, RoundIt)
{
_global.OutputValue = NewLow + ( ( (InputVariable-OriginalLow) *NewHigh ) /(OriginalHigh-OriginalLow) ); // Transforms "InputVariable" into "_global.OutputValue" via linear interpolation (described above) ...The equation was found with some elbow grease and algebra...HOORAY FOR ALGEBRA!

// That's really the meat of the function; the rest is just cherries on top.

// Now it's going to check to see what the RoundIt variable is set to
//If RoundIt = "Round", then it only returns whole integer numbers
if(RoundIt == "Round"){_global.OutputValue = Math.round(_global.OutputValue);} //Rounds OutputValue
//If RoundIt = "Decimal", this function (by default) does not round the return value.
//However, I wanted to make the function add "fake" zeroes at the end of everything so the number always appeared to display at least 2 decimal places out...even if it really ends (EX: 4.00, OR 4.10)
if(RoundIt == "Decimal" && (OutputValue>=1 || OutputValue==0) && (Math.round(_global.OutputValue)==_global.OutputVa lue) ){_global.OutputValue +=".00";} //Adds a "fake" ".00" after all whole/integer numbers
if(RoundIt == "Decimal" && (Math.round(_global.OutputValue*10)/10)==_global.OutputValue){_global.OutputValue +="0";} //Adds a "fake" "0" after decimals that don't end in zero...(ex: 1.6)

return _global.OutputValue; // Returns _global.OutputValue (so it can be used to define a variable outside of the function...or even outside of this Scene!)

}

// End of Linear Interpolation Function  Reply With Quote

5. Someone somewhere might find that useful, but not here in the AS3 forum.

Go ahead and port it to AS3, and post it properly in [code] tags.

What's with all the untyped parameters and use of _global? That wasn't necessary even back in the dark ages of AS2. You're also returning a Number sometimes and a String sometimes. Don't do that. Have your interpolation always return a number, and have a separate formatting function if you need a pretty string.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 