1

How can I re-map a number from one range to another in pygame or python ?

eg: If I want to map to map x=25 with range 0-100 to a new range from 0-20.. the new value of x would be 5.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • You could look here for an idea of how to implement re-ranging: https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio – Doug Apr 21 '20 at 06:55

1 Answers1

0

Write a function which maps from a range [a, b] to a range [c, d]:

def maprange(x, a, b, c, d):
    w =  (x-a) / (b-a)
    y = c + w * (d-c)
    return y
y = maprange(25, 0, 100, 0, 20)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174