Android Code Snippet MapRange Mapping A-B to C-D

SubName: Map a range of numbers from A-B to C-D and return the mapped value.
Description: I use B4R a lot and that package includes a function called MapRange. B4A doesn't have a version of MapRange and I couldn't find one on the forum anywhere, so I found the formula online and I put together this little sub. Using MapRange you can map any range of numbers to another different range of numbers. For example 0 to 100 can be mapped to -50 to 50, 25 to 75, 2000 to 5000, -300 to -150 or whatever number range you need to return a value from. Your original number range can be any range you wish and not just 0 to 100 like above.

If you need decimal results, just change these Ints to Floats.
B4X:
'Map the value from the 'From' range A and B to the 'To' range C and D.
Sub MapRange(Value As Int, FromLow As Int, FromHigh As Int, ToLow As Int, ToHigh As Int) As Int
    Return ToLow + (ToHigh - ToLow) * ((Value - FromLow) / (FromHigh - FromLow))
End Sub

Tags: maprange, map, range, from, to
 
Last edited:

wonder

Expert
Licensed User
Longtime User
I was actually working on something like this!!! What a coincidence!!! I'll try to post my code later... :)
 

wonder

Expert
Licensed User
Longtime User
Hello again!!! I'm sorry I didn't reply earlier, I actually got some new 4x4 shoes for my car this week. ;)
This code was written with a very specific purpose and it's still under development and subject to change... so yeah...

B4X:
'Example:
'========================================================
'AddRange(angleList, 355, 5, 0, 360)
'   will split the [355..5] degree range into two
'   and add them to the 'angleList' as pairs of doubles
'   >>> angleList = [(355, 360), (0, 5)]
'
'AddRange(angleList, 3, 45, 0, 360)
'   will merge the [3..45] range with the existing [0..5]
'   >>> angleList = [(355, 360), (0, 45)]
'
'AddRange(angleList, 90, 180, 0, 360)
'   will add this range as the third element of the list
'   >>> angleList = [(355, 360), (0, 5), (90, 180)]
'========================================================

Sub AddRange(lst As List, a As Double, b As Double, lowest As Double, highest As Double) As Boolean
    If a > highest Or b > highest Or a < lowest Or b < lowest Then Return False
    '--------------------------------------------------------------------------
    Dim i As Int
    If a > b Then
        i = lst.Size - 1
        Do While i >= 0
            Dim range() = lst.Get(i) As Double
            If (a >= range(0) And a <= range(1)) Or (range(0) >= a And range(0) <= highest) Then
                a = Min(a, range(0))
                lst.RemoveAt(i)
            End If
            i = i - 1
        Loop
        lst.Add(Array As Double(a, highest))
        '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        i = lst.Size - 1
        Do While i >= 0
            Dim range() = lst.Get(i) As Double
            If (b >= range(0) And b <= range(1)) Or (range(1) >= lowest And range(1) <= b) Then
                b = Max(b, range(1))
                lst.RemoveAt(i)
            End If
            i = i - 1
        Loop
        lst.Add(Array As Double(lowest, b))
        '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Else
        i = lst.Size - 1
        Do While i >= 0
            Dim range() = lst.Get(i) As Double
            If (a >= range(0) And a <= range(1)) Or (b >= range(0) And b <= range(1)) _
            Or (range(0) >= a And range(0) <= b) Or (range(1) >= a And range(1) <= b) Then
                a = Min(a, range(0))
                b = Max(b, range(1))
                lst.RemoveAt(i)
            End If
            i = i - 1
        Loop
        lst.Add(Array As Double(a, b))
    End If
    Return True
End Sub
 
Last edited:
Top