Scaled Mapping a Value

Hello again, I have this value from -10 to 10 that I want to map to -725 to 725. However, the mapped value should be -600 when the value is -5, or the value is -300 when the mapped value is -2. I would like for this to work with every value from -725 to 725 but cannot figure out how. Right now I am just using generic mapping.

Hi,

You need to use conditions :

float mapMyValue(float value) {
  // Specific cases
  if (value == -5) {
    return -600;
  } else if (value == -2) {
    return -300;
  }
  
  // In any other cases
  return map(value, -10, 10, -725, 725);
}
1 Like

yeah.

But when -5 and -2 are only EXAMPLES for a non linear mapping see also How do I lerp colors on a curve? (not linear interpolation) - #4 by Chrisir

Chrisir

Yeah, I meant nonlinear mapping. I also looked at your example, but how would I control how the value is mapped because I need the value to be mapped on a much steeper curve. For example, when the value is 4, the mapped value is 550-600, and when the value is 2, the value is 350-400 and when the value is 3 the mapped value is 450-500, but when the value is 1 it is 300-350.

Ok, it was not that clear :slight_smile:

If you want non linear mapping, you should take a look at this useful website : https://easings.net/

It provides a lot of easing functions that you can use to map values non linearly. Try to find a curve that fits your needs :wink:

2 Likes

Hello @josephh, I went to desmos and plotted some points to make a line that shows what I want from the nonlinear mapping. Then, I went to the easings website and chose the easeInOutSine, and tried to convert it into java code. Is this right?

public float map2 (float x) {
  return -(cos(PI * x) - 1) / 2;
}
1 Like

Yes it’s exactly that!

Note that in Processing you are not forced to use public in front of everything.

In regular Java where you have multiple files and packages it’s useful to put restriction access but Processing is a special case because you don’t have the notion of packages. And when you don’t put the public identifier by default it’s accessible in the whole package.

(I also don’t put a space between the function name and the parenthesis but it’s a personal preference :wink: )

/**
 * easeInOutSine mapping function
 * [0, 1] -> [0, 1]
 */
float map2(float x) {
  return -(cos(PI * x) - 1) / 2;
}

Anyway have fun !

Thank you, and one more thing. How would you set the minimum and maximum value to map? Because when I implemented this is works fine for positive values but anything below 0 gives me an error.

If you have negative values, you need to remap them to the interval [0, 1] to be able to pass it to the mapping function :

float value = -5;

// -10 is arbitrary, it's the minimum negative value
float mapNormalized = map(value, -10, 10, 0, 1);

float mappedValue = map2(mapNormalized);

You can also clamp it to [0, 1], so negative values are going to be 0 :

float value = -5;

float clampedValue = clamp(value, 0, 1); // Here = 0

float mappedValue = map2(clampedValue);

Another example How do I lerp colors on a curve? (not linear interpolation) - #3 by Chrisir