Non linear ofMap?

Hi,
I’m trying to set speed with a knob ranging from 1ms to 7secs using ofMap but would like the mapping not to be linear but more logarithmic. Is this possible?
Hope the question makes sense, hard to explain

thanks
Alex

1 Like

you could use a curve function to get a different mapping, ie:

float map(float in, float inMin, float inMax, float outMin, float outMax, float shaper){
// (1) convert to pct (0-1)
float pct = ofMap (in, inMin, inMax, 0, 1, true);
// raise this number to a power
pct = powf(pct, shaper);
float out = ofMap(pct, 0,1, outMin, outMax, true);
return out;
}

if shaper is 1, you get linear output like ofMap. If not, you can shape the output. shaper > 1 means you’ll have non linear steps weighted towards the start. If shaper is < 1 and > 0 you can get non linear range weighted towards the end. There are plenty of other shaper functions that take a pct (a number between 0 and 1) and output a pct, which might help.

4 Likes

Hi Zach,
Thanks for the info, this makes it so much easier!!!
Cheers
alex

I just used this block of code for a project. I was wondering if this could be implemented in ofMath in the release of OF? I could add it and send a pull request if you want - I think I’m going to use it a lot and it could come in use for others as well.

I feel like it is worth mentioning ofxEasing here as a quick way of getting a variety of non-linear mappings.

3 Likes

Oh this looks handy, didn’t know about it!