Learn creative coding writing simple programs

45. Convert distances into colors or widths

The map() function is very useful when you want to convert numbers from one range to another range. When do we want to do that? well, we use different ranges of numbers for different things. For example the sin() function returns values between -1 and 1. The valid X positions for drawing on our screen might be between 0 and 400. The right values for RGB colors usually are between 0 and 255.
Sometimes we want to make the color depend on a distance, or maybe the width depend on a speed. You can link any two concepts that come into your mind in your program. Using map() is the easiest way to convert values from one range to another.

Tags: map, range, numbers, software tutorial, education

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```