1

Processing has a function map that

Re-maps a number from one range to another.

For example

int ans = map(5, 0, 10, 0, 100);

ans will be 50 as 5 is halfway between 0 and 10 and halfway between 0 and 100 is 50.

If there is no built in function how would I write my own one?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Nathan Smith
  • 41
  • 1
  • 4
  • 2
    I don't get exactly what it should do. Please explain. The example is also not enlightening, and I don't know whether more do anything or not – Paul Stelian Apr 28 '17 at 14:51
  • 1
    Could you please elaborate a little more? It is very unclear to me right now what you want to do. – NathanOliver Apr 28 '17 at 14:51
  • the first function is the value to be mapped the second and third are the max and min numbers the fourth and fifth numbers a what the answer should be between. I beleive the finction calculates what perscentige val is of 2 - 1 and then what that erscentige is of 5 - 4 – Nathan Smith Apr 28 '17 at 14:55
  • so map(5, 0, 10, 0, 100) will be fifty as 5 is halfway between 0 and 10 and halfway between 0 and 100 is 50 – Nathan Smith Apr 28 '17 at 14:56
  • The Processing thing you're using isn't exactly common knowledge in the C++ world (or elsewhere). For the future, please keep in mind that people won't know what you are talking about if you don't provide enough context or useful links. Or incorrect capitalisation :) – Christian Hackl Apr 28 '17 at 14:57
  • Okay. So you want to convert the number `X` in the range `[a, b]` to a number `Y` that is in the range `[c, d]` where `Y` is in the same "location" as `X` correct? There is no built in function so you will have to write your own or find one that someone else already wrote. – NathanOliver Apr 28 '17 at 14:58
  • 1
    It should be a one-liner, no? – Christian Hackl Apr 28 '17 at 14:59
  • yes, thanks for the answer I will try and write my own and post it here when done thanks. – Nathan Smith Apr 28 '17 at 15:00
  • what do you mean by linier? – Nathan Smith Apr 28 '17 at 15:01
  • 6
    My interpretation of the question is a duplicate of http://stackoverflow.com/questions/3451553/value-remapping – François Andrieux Apr 28 '17 at 15:03
  • @NathanSmith: That the function can be implemented by yourself in one short line. It sounds like super simple math. – Christian Hackl Apr 28 '17 at 17:20
  • Processing is open source. Just look up how they implemented the `map()` function. – Kevin Workman May 01 '17 at 14:59
  • Welcome to Stack Overflow! Please [edit] your question to show [the code you have so far](http://whathaveyoutried.com). You should include at least an outline (but preferably a [mcve]) of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight May 02 '17 at 10:07
  • 1
    Possible duplicate of [Value Remapping](https://stackoverflow.com/questions/3451553/value-remapping) – Pharap Apr 20 '19 at 20:22

1 Answers1

5

Processing is open source, and you can view the source for the map() function here.

Specifically, this is the line you care about:

float outgoing =
  start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
Pharap
  • 3,826
  • 5
  • 37
  • 51
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107