How does the map() function in Processing work?

think of it this way: divide a range of 0 to 10 into 100 equal parts. (you will get 0.1 per part) now divide the range 0 to 100 into 100 equal parts (you will get 1 per part) so 0.1 in the range of 0 to 10 is equal to 1 in the range 0 to 100. if you want to find where 5 in the range of 0 to 10 belongs in the range of 0 to 100, divide 5 by the size of a 0 to 10 part and multiply that number by the size of a 0 to 100 part and you will have your answer! (50)

P.S. I know this isn't how it actually works, but I just thought I would give an example to clarify things.


The map() function is a useful shortcut and you won't regret the time spent at understanding it.
This is its syntax:

variable2 = map(variable1, min1, max1, min2, max2);

The function establishes a proportion between two ranges of values:

min1 : min2 = max1 : max2

you can read it as: min1 is to min2 as max1 is to max2.
variable1 stores a value between the first range min1~max1.
variable2 gets a value between the second range min2~max2.

This is the equation the function solves for the programmer:

variable2 = min2+(max2-min2)*((variable1-min1)/(max1-min1))

This is the Java code behind the Processing map() function:

static public final float map(float value, 
                              float istart, 
                              float istop, 
                              float ostart, 
                              float ostop) {
    return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}

Tags:

Processing