adafruit_simplemath
¶
Math utility functions
Author(s): Dan Halbert, James Carr
Implementation Notes¶
Software and Dependencies:
Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases
- adafruit_simplemath.constrain(x: float, out_min: float, out_max: float) → float¶
Constrains
x
to be within the inclusive range [out_min
,out_max
]. Sometimes calledclip
orclamp
in other libraries.out_min
should be less than or equal toout_max
. Ifx
is less thanout_min
, returnout_min
. Ifx
is greater thanout_max
, returnout_max
. Otherwise just returnx
. Ifmax_value
is less thanmin_value
, they will be swapped.
- adafruit_simplemath.map_range(x: float, in_min: float, in_max: float, out_min: float, out_max: float) → float¶
Maps a number from one range to another. Somewhat similar to the Arduino
map()
function, but returns a floating point result, and constrains the output value to be betweenout_min
andout_max
. Ifin_min
is greater thanin_max
orout_min
is greater thanout_max
, the corresponding range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.See also
map_unconstrained_range()
from adafruit_simplemath import map_range percent = 23 screen_width = 320 # or board.DISPLAY.width x = map_range(percent, 0, 100, 0, screen_width - 1) print("X position", percent, "% from the left of screen is", x)
- adafruit_simplemath.map_unconstrained_range(x: float, in_min: float, in_max: float, out_min: float, out_max: float) → float¶
Maps a number from one range to another. Somewhat similar to the Arduino
map()
function, but returns a floating point result, and does not constrain the output value to be betweenout_min
andout_max
. Ifin_min
is greater thanin_max
orout_min
is greater thanout_max
, the corresponding range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.See also
map_range()
from adafruit_simplemath import map_unconstrained_range celsius = -20 fahrenheit = map_unconstrained_range(celsius, 0, 100, 32, 212) print(celsius, "degress Celsius =", fahrenheit, "degrees Fahrenheit")