# Scaled Mapping a Value

**URL:** <https://discourse.processing.org/t/scaled-mapping-a-value/27564>\
**Category:** Coding Questions\
**Created:** [February 3, 2021, 1:15pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564 "2021-02-03T13:15:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 3, 2021, 1:15pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/1 "2021-02-03T13:15:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 3, 2021, 1:43pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/2 "2021-02-03T13:43:41Z")

</div>

Hi,

You need to use conditions :

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 3, 2021, 2:49pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/3 "2021-02-03T14:49:18Z")

</div>

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](https://discourse.processing.org/t/how-do-i-lerp-colors-on-a-curve-not-linear-interpolation/24815/4)

Chrisir

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 3, 2021, 8:41pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/4 "2021-02-03T20:41:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 3, 2021, 8:45pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/5 "2021-02-03T20:45:52Z")

</div>

Ok, it was not that clear 🙂

If you want non linear mapping, you should take a look at this useful website : [https://easings.net/](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 😉

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 3, 2021, 11:29pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/6 "2021-02-03T23:29:31Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 3, 2021, 11:45pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/7 "2021-02-03T23:45:51Z")

</div>

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 😉 )

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

```

Anyway have fun !

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 3, 2021, 11:53pm UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/8 "2021-02-03T23:53:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [February 4, 2021, 12:01am UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/9 "2021-02-04T00:01:51Z")

</div>

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 :

```auto
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` :

```auto
float value = -5;

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

float mappedValue = map2(clampedValue);

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 4, 2021, 6:39am UTC](https://discourse.processing.org/t/scaled-mapping-a-value/27564/11 "2021-02-04T06:39:00Z")

</div>

Another example [How do I lerp colors on a curve? (not linear interpolation) - #3 by Chrisir](https://discourse.processing.org/t/how-do-i-lerp-colors-on-a-curve-not-linear-interpolation/24815/3)
