GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2015-06-26 00:04:51

eustace
Member
Registered: 2015-06-25
Posts: 1

a map() function

This is a simple bit of code, but it helps to have it pre-written and tested rather then typing it in when you need it and getting the sign wrong.  It takes a value, then maps that value from one range of real numbers to another range.  There is a check for division-by-zero, which returns zero.

Expand///Map(input, old_max, old_min, new_max, new_min)
 
var inputValue, oldMax, oldMin, newMax, newMin;
 
inputValue = argument0;
oldMax = argument1;
oldMin = argument2;
newMax = argument3;
newMin = argument4;
 
if (!(oldMax - oldMin = 0))
{
    return (((inputValue - oldMin)/(oldMax - oldMin))*(newMax - newMin)) + newMin;
}
else return 0;

Offline

#2 2015-06-26 16:36:59

xot
Administrator
Registered: 2007-08-18
Posts: 1,240

Re: a map() function

Thanks for posting this. Someone else has already submitted something like this, I think. I'll see how they compare, maybe combine them. I'll be making a couple of minor changes but this looks good.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB