Re-maps a number from one range to another

Hi guys,
I think that’s my first post here, so hello to everybody.

I’m used to enjoy Processing and it has an handy function : map().
It allows to re-maps a number from one range to another.

Before trying to add it by myself in a project (which shouldn’t be quite difficult) , I wonder if Gdevelopp has already something similar maybe in expressions ?

Thanks in advance :slight_smile: :slight_smile:

This is the explanation of this function and also the source code :

variable2 = map(variable1, min1, max1, min2, max2);

The function establishes a proportion between two ranges of values:

min1 : min2 = max1 : max2

you can read it as: min1 is to min2 as max1 is to max2.
variable1 stores a value between the first range min1~max1.
variable2 gets a value between the second range min2~max2.

This is the equation the function solves for the programmer:

variable2 = min2+(max2-min2)*((variable1-min1)/(max1-min1))

This is the Java code behind the Processing map() function:

static public final float map(float value, 
                              float istart, 
                              float istop, 
                              float ostart, 
                              float ostop) {
    return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));

Welcome. There could be an easier way but the closest I can see is a combination of normalize() fed into a lerp()

1 Like