If you’ve ever used Processing you’ve probably used the ‘map’ function. From the Processing Reference section it reads: “Re-maps a number from one range to another.”
Unfortunately the function doesn’t exist in Flash, so I rewrote it. Why is this function useful? Lets say your mouse can move within the bounds of your stage from 0 to 550 pixels along its x-axis. You want a box to move back and forth from 100 to 200 pixels according to the position of the mouse. So when your mouse is halfway across the stage your box should be halfway between 100 and 200 pixels. You would simply use:
Easy as that!
Usage:
map(value, low1, high1, low2, high2);
low2 and high2 default to 0 and 1, respectively so you don’t have to put them in if you just want to normalize your value.
And super compressed (less readable, less space, works the same):
trackback URL for this post: http://lab.joelgillman.com/archives/87_map-function/trackback
© 2010 jgillman -¦- Go WordPress! -¦- RSS 2.0 Feed
4 Comments
This is great, thanks for sharing it. Should be a huge time saver.
Thanks Joel I am going to use this today!
Just came across this on Lawrie Cape’s blog, and as I said there, it’s brilliant! So simple and yet so powerful. Cheers for rewriting it.
Very helpful, thanks! One of the things I enjoy about Processing is the ease of methods such as map().
One Trackback/Pingback
[...] On brillaint function I’ve used in practically all of my projects since finding it, is Joel Gillman’s Map Function. [...]
Post a Comment