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.
IndexSuggestions & BugsSoftware,  Documentation,  Website Suggestions › map(value, fromLow, fromHigh, toLow, toHigh)
Page Index Toggle Pages: 1
map(value, fromLow, fromHigh, toLow, toHigh) (Read 2168 times)
map(value, fromLow, fromHigh, toLow, toHigh)
Oct 23rd, 2006, 9:01pm
 
I find myself continually rewriting:

Code:

float map(float value, float fromLow, float fromHigh, float toLow, float toHigh) {
return (value - fromLow) / fromHigh * (toHigh - toLow) + toLow;
}


It would be nice to have it more readily available (a la lerp()).
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #1 - Oct 23rd, 2006, 9:34pm
 
in fact, this has already been added for the next release, and is tentatively called relerp(). you can also undo a lerp() using the unlerp() function that appeared in 0119.

all that remains is making sure these are the names we want, and a final decision on the order of parameters. lerp uses:
float value = lerp(float start, float stop, float amount);
and then unlerp:
float amount = unlerp(float start, float stop, float value);
so relerp currently looks like:
float value = relerp(float fromStart, float fromStop, float value, float toStart, float toStop);

to do it again, we should've put the value in front as the first parameter for lerp().
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #2 - Oct 23rd, 2006, 11:34pm
 
Hey just to insert my 2 cents, I think 'map' or 'remap' would make more sense, since it's really a different than lerp, and the parameters you give it are different. relerp seems a little confusing.
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #3 - Oct 24th, 2006, 2:02am
 
well, i'm open to suggestions. we have three functions. first is lerp(), then a second that does the opposite thing (now called unlerp), then a third (which is being suggested here, and i currently have called relerp) which is identical to calling lerp() on the value produced by unlerp().

i'm concerned about "the opposite of lerp() is map()" not making any sense, though it would allow us to simply overload map() with two versions, one that takes the incoming low and high values, and a second that takes both incoming and outgoing ranges (as being suggested in the initial post).

// now called unlerp(), gives normalized value
// between 0 and 1 of "value" between fromLow and fromHigh
float map(float value, float fromLow, float fromHigh)

// remaps into another range
float map(float value, float fromLow, float fromHigh, float toLow, float toHigh)

with any luck, perhaps such functions are provided by other APIs and we can use their syntax..
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #4 - Oct 25th, 2006, 5:08pm
 
fry wrote on Oct 24th, 2006, 2:02am:
i'm concerned about "the opposite of lerp() is map()" not making any sense


Suggestion:  The problem isn't the name "lerp", it's the name "unlerp".  (the comments in the above code even suggest as much)  What "unlerp" really is is "normalize", and if it had that name it might be more obvious that kmcdonald's map() function is simply:

 return lerp(normalize(value,fromLow,fromHigh), toLow, toHigh);

Because as it is now it would read:

 return lerp(unlerp(value,fromLow,fromHigh), toLow, toHigh);

which seems less intuitive.  In short, even though "unlerp" is a logical opposite naming for the opposite functioning of "lerp", perhaps there is another word that better describes the math that's being performed.
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #5 - Oct 25th, 2006, 10:51pm
 
eh, thanks for explaining the math to me, though i don't think that's the problem. Wink

so instead, does it make sense to tell people "the opposite of lerp() is normalize()?" we have a function called lerp, and we need to add two cousins. my concern is how the function is taught, or when you're looking through the reference that does the opposite thing as your lerp() function, how would you find it.

i don't like "unlerp" or "relerp", which is why i said i'm open to suggestions, and neither function has been announced officially in revisions.txt or added to the reference.

so we have two suggestions so far, map() and normalize(). normalize() is of course what is technically happening, but it also means we'd need a third name for the function that was initially proposed. and i'm not nuts about having three functions, named lerp(), normalize(), and map(), that do almost the exact same thing but sound like they have nothing to do with one another.
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #6 - Oct 25th, 2006, 11:42pm
 
Apologies -- any "smarter-than-thou" tone was unintentional.

I was just wondering if the third name "map" is necessary at all.  Because it's still just a lerp, only with an unnormalized "t".  So, would it make sense to just overload lerp for the 5 param version?

You could then similarly overload the reverse operation (unlerp or normalize or whatever) with a 5 param version.  For example, unlerp(80,50,100) = 0.6, unlerp(80,50,100,500,1000) = 800.
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #7 - Oct 26th, 2006, 2:35am
 
Thanks Ben -- I'm just glad it's going to be included Smiley

This is how I imagine the troika:

[a,b]->[0,1] unlerp(x,a,b)
[a,b]->[c,d] relerp(a,b,x,c,d)
[0,1]->[c,d] lerp(x,c,d)

I think the main question is whether you want to rename lerp or not. If so, I see:

[a,b]->[0,1] mapToUnit(x,a,b)
[a,b]->[c,d] map(a,b,x,c,d)
[0,1]->[c,d] mapFromUnit(x,c,d)

If changing lerp's name is not an option... lerp/unlerp makes sense, but "relerp" makes me think of repeating the lerp operation. Maybe scaledlerp?

The only other place I know of a mapish function is max/msp; the object has inlets ordered [x a b c d]... but I think that's more max's style than any deep consideration.
Re: map(value, fromLow, fromHigh, toLow, toHigh)
Reply #8 - Nov 6th, 2006, 2:22pm
 
k, it's implemented in 0120 as map() same as you wrote it, and norm(value, start, stop). we're still deciding on whether this is the proper syntax, so it's not been documented yet, but you can try it and see how it feels.
Page Index Toggle Pages: 1