We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › simple() number conversion
Page Index Toggle Pages: 1
simple(?) number conversion (Read 222 times)
simple(?) number conversion
Jan 25th, 2009, 2:51pm
 
hello,

I have a set of numbers in an array to convert like this;
(from left to right)

400 = 0
300 = 0.2
200 = 0.4
100 = 0.6
0 = 0.8

actually this is y position of rect that will represent amplitude of a sound. so the progression must be from bottom to top, like all the way down, silent, all the way up, loud. but since Processing's cordination system is top to bottom, I need to do this conversion...

it seemed to me simple to do but as I'm totally dumb in math, I just can't find a formula to solve this...

any help will be very much appriciated.

thank you
Re: simple(?) number conversion
Reply #1 - Jan 25th, 2009, 3:06pm
 
I can show you the maths if you are curious, but the simpler way is to make Processing do the job for us:
Code:
void setup()
{
println(Map(400));
println(Map(300));
println(Map(200));
println(Map(100));
println(Map(0));
exit();
}

float Map(int value)
{
 return map(value, 0, 400, 0.8, 0);
}


Oh, I show the math too... Not really hard:
Code:
float Map(int value)
{
return 0.8 * (1 - value / 400.0);
}

I divide by 400 so the values go from 0 to 1.0, and I subtract the result from 1 to invert them (going from 1.0 to 0). Then I multiply the result by 0.8 (the max value you need).
Re: simple(?) number conversion
Reply #2 - Jan 25th, 2009, 6:58pm
 
thanks for the reply,

what I needed was the math formula,
and it helped me a lot and I can use this for other part too.

thanks again!!
Page Index Toggle Pages: 1