lab.joelgillman.com

Sketches, Code Bites and Toys




Map Function
2008-09-29

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:

myBox.x = map(mouseX, 0, 550, 100, 200);

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.

function map(value:Number,
             low1:Number,
             high1:Number,
             low2:Number = 0,
             high2:Number = 1):Number {
    //if the value and the 1st range low are equal to
    // the new value must be low2
    if (value == low1) {
        return low2;
    }
   
    //normalize both sets to a 0-? range
    var range1 = high1 - low1;
    var range2 = high2 - low2;
   
    //normalize the value to the new normalized range
    var result = value - low1;
   
    //define the range as a percentage (0.0 to 1.0)
    var ratio = result / range1;
   
    //find the value in the new normalized-range
    result = ratio * range2;
   
    //un-normalize the value in the new range
    result += low2;
   
    return result;
}

And super compressed (less readable, less space, works the same):

function map(v:Number, a:Number, b:Number, x:Number = 0, y:Number = 1):Number {
    return (v == a) ? x : (v - a) * (y - x) / (b - a) + x;
}


Share
Tags: , , , , , ,

trackback URL for this post: http://lab.joelgillman.com/archives/87_map-function/trackback

comments as feed

4 Comments

  1. Lawrie wrote:

    This is great, thanks for sharing it. Should be a huge time saver.

    Thursday, December 4, 2008 at 10:49 am | Permalink
  2. Trygabee wrote:

    Thanks Joel I am going to use this today!

    Thursday, April 23, 2009 at 3:28 pm | Permalink
  3. MichaelJW wrote:

    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.

    Sunday, May 17, 2009 at 7:55 am | Permalink
  4. JGB wrote:

    Very helpful, thanks! One of the things I enjoy about Processing is the ease of methods such as map().

    Wednesday, June 17, 2009 at 1:46 pm | Permalink

One Trackback/Pingback

  1. AS3 - Amazing Map Function. | The Lawrie Cape Blog on Saturday, May 9, 2009 at 11:17 am

    [...] On brillaint function I’ve used in practically all of my projects since finding it, is Joel Gillman’s Map Function. [...]

Post a Comment

Your email is never published nor shared.
Required fields are marked *
*
*



© 2010 jgillman -¦- Go WordPress! -¦- RSS 2.0 Feed