'map()' function - as in mapping input range to output range 'helper' - example included

Let Sim Innovations know about your Air Manager experience and let us know about your dream feature addition

Moderators: russ, Ralph

Post Reply
Message
Author
User avatar
jph
Posts: 2853
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

'map()' function - as in mapping input range to output range 'helper' - example included

#1 Post by jph »

Hi @Corjan
I was looking for a 'map' function as per Arduino 'map() for lua / AM and could find nothing obvious so put this together.
Based on -
https://www.arduino.cc/reference/en/lan ... /math/map/

This is adapted from the Arduino map() and works fine and accurately as it is not restricted to integer as per arduino. It might be worth adding this or similar as a 'helper' ?
Handles negative values just fine.

edit - I have added an optional 'constrain' parameter which effectively does a var_cap on the input value if required.

Syntax
map(value, fromLow, fromHigh, toLow, toHigh, constrain)

Parameters
value: the number to map.
fromLow: the lower bound of the value’s current range.
fromHigh: the upper bound of the value’s current range.
toLow: the lower bound of the value’s target range.
toHigh: the upper bound of the value’s target range.
constrain (optional) if true then value is constrained within fromLow to fromHigh

Returns
The mapped value.

Code: Select all

-- Lua map function similar to Arduino 'map' function but fully 'float'
-- OPTIONAL 'constrain' parameter
-- JPH - Joe Hanley 2022
-- see https://www.arduino.cc/reference/en/language/functions/math/map/ for some notes
-- afaik lua will use floats as needed so the notes above regarding accuracy are moot. Joe
-- added optional parameter 'constrain' which will limit the value of the input to the uper and lower limits with var_cap

function map ( x, in_min, in_max, out_min,  out_max, constrain ) 
  	if constrain then x = var_cap(x,in_min,in_max) end
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
end

-- examples

--value = map(0,-1000,1000,0,2000)  -- another negative value example
-- value = map(0.5,0,1,-0.5,1)         -- example using negative values - as in convert 0 - 1 to a value from  -0.5 to 1
--value = map(4,0,51,51,0)          -- example to reverse a number order
-- value = map(0.5,0.162,0.748,-0.5,1)
value = map(0.7,0.178,0.743,-0.5,1,true) -- optional constrain value performs var_cap on x
print (value)
Joe

edit - add the optional 'constrain' for value to in_min in_max
Last edited by jph on Sat Oct 22, 2022 6:33 pm, edited 4 times in total.
Joe. CISSP, MSc.

User avatar
Keith Baxter
Posts: 4685
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: 'map()' function - as in mapping input range to output range 'helper' - example included

#2 Post by Keith Baxter »

Hi,

Simple math has worked for years. No need for this. IMHO
Far more important things for the SI guys to work on.

You can also use interpolate_linear() for complicated math. ;)

Keith
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
jph
Posts: 2853
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: 'map()' function - as in mapping input range to output range 'helper' - example included

#3 Post by jph »

what ?
There is nothing to work on. The work is done.
Also, interpolate linear is absolutely of no relevance here.
I think you are starting early. Perhaps more water ? ;)
Joe. CISSP, MSc.

User avatar
Keith Baxter
Posts: 4685
Joined: Wed Dec 20, 2017 11:00 am
Location: Botswana

Re: 'map()' function - as in mapping input range to output range 'helper' - example included

#4 Post by Keith Baxter »

jph wrote: Sat Oct 22, 2022 2:35 pm what ?
There is nothing to work on. The work is done.
Also, interpolate linear is absolutely of no relevance here.
I think you are starting early. Perhaps more water ? ;)
Ha Ha...

Try this if you math work not. I think it does what you want... ;)

Code: Select all

- Create interpolate settings in ascending order
local settings = { { -0.5 , 0  },
                   { 1, 1 } }
local var = interpolate_linear(settings, 0)

print(var)
Keith
Last edited by Keith Baxter on Sat Oct 22, 2022 7:55 pm, edited 1 time in total.
AMD RYZEN 9 5950X CPU, Corsair H80I cooler, ASUS TUF GAMING B550-PLUS AMD Ryzen Mother Board,  32Gb ram Corsair Vengeance 3000Mh, MSI GTX960 4G graphics card 

User avatar
jph
Posts: 2853
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: 'map()' function - as in mapping input range to output range 'helper' - example included

#5 Post by jph »

Keith,
you need to take more water with it. I mean it - seriously.
I have provided the full solution in the first post. I am not asking for 'help'. I certainly do not need help with math at any level.
It is a solution that could be used by others hence the suggestion to add the functionality as part of AM under the 'helper' functions.
The functionality is by passing parameters. Not pre-defined.
If you bother to read the post then you will see, as said, that the solution is provided and the suggestion is to add it to existing helpers.
Joe. CISSP, MSc.

Mickolodias
Posts: 69
Joined: Mon Sep 13, 2021 3:21 am

Re: 'map()' function - as in mapping input range to output range 'helper' - example included

#6 Post by Mickolodias »

Ah this is cool.
I've needed something like this a few times and mathed my way around it, which melts my brain every time.

Thanks for sharing!
I'm one of 'those' mac guys. (and I have no idea why I can't afford to eat)

User avatar
jph
Posts: 2853
Joined: Fri Apr 10, 2020 12:50 pm
Location: Somewhere over the rainbow..

Re: 'map()' function - as in mapping input range to output range 'helper' - example included

#7 Post by jph »

Mickolodias wrote: Sun Oct 23, 2022 9:32 am Ah this is cool.
I've needed something like this a few times and mathed my way around it, which melts my brain every time.

Thanks for sharing!
You are welcome. I use the Arduino ide version a lot. Very useful.
Joe. CISSP, MSc.

Post Reply