Using map() and switch in Processing

on 18|12|2014
Filled under: Announcements

ScreenClip [6]The following short sketch illustrates the use of map() and switch to convert a variable from one range of values to another:

 

int num = 99;
int r = 0;
int g = 0;
int b = 0;

void setup() {
size (100, 100);
background(255);

float m = map(num, 0, 100, 0, 2);

switch(int(m)) {
case 0:
r = 24;
g = 114;
b = 201;
break;
case 1:
r = 32;
g = 227;
b = 53;
break;
case 2:
r = 55;
g = 55;
b = 55;
break;
}
}

void draw() {
fill(r,g,b);
ellipse(height/2, width/2, 10, 10);
}

Comments are closed.