It takes a value in one range and returns it in another. If you have data on a scale that goes from -5 -> 5 but you need a value in the range 0 -> 255 (to use as a colour for instance) then you can use map to do the maths of that conversion for you (the maths isn't difficult but Processing saves you the trouble).
Let's say I wanted to draw a circle inside a box and control the circle position using the mouse over the whole window. If I just give the mouse position to ellipse() then that'll draw outside the box so instead I can use map():
- void setup() {
- size(640, 480);
- ellipseMode(CENTER);
- }
- void draw() {
- background(150);
- // our mouseX can go from 0 -> 640 and our
- // rectangle lies between 50 and 250
- float x = map(mouseX, 0, 640, 50, 250);
-
- // our mouseX can go from 0 -> 480 and our
- // rectangle lies between 200 and 400
- float y = map(mouseY, 0, 480, 200, 400);
-
- // draw the rectangle
- rect(50, 200, 200, 200);
-
- // draw the ellipse using the position we
- // worked out with map()
- ellipse(x, y, 10, 10);
- }