23
23
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SimpleMath.git"
24
24
25
25
26
- def map_range (x , in_min , in_max , out_min , out_max ):
26
+ def map_range (
27
+ x : float , in_min : float , in_max : float , out_min : float , out_max : float
28
+ ) -> float :
27
29
"""
28
30
Maps a number from one range to another. Somewhat similar to the Arduino ``map()`` function, but
29
31
returns a floating point result, and constrains the output value to be between ``out_min`` and ``out_max``.
32
+ If ``in_min`` is greater than ``in_max`` or ``out_min`` is greater than ``out_max``, the corresponding
33
+ range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.
30
34
31
- :return: Returns value mapped to new range
35
+ :param float in_min: Start value of input range.
36
+ :param float in_max: End value of input range.
37
+ :param float out_min: Start value of output range.
38
+ :param float out_max: End value of output range.
39
+ :return: Returns value mapped to new range.
32
40
:rtype: float
33
41
"""
34
42
in_range = in_max - in_min
@@ -46,14 +54,17 @@ def map_range(x, in_min, in_max, out_min, out_max):
46
54
return min (max (mapped , out_max ), out_min )
47
55
48
56
49
- def constrain (x , out_min , out_max ) :
50
- """Constrains ``x`` to be within the inclusive range ``[ out_min, out_max]`` .
57
+ def constrain (x : float , out_min : float , out_max : float ) -> float :
58
+ """Constrains ``x`` to be within the inclusive range [`` out_min``, `` out_max``] .
51
59
Sometimes called ``clip`` or ``clamp`` in other libraries.
52
60
``out_min`` should be less than or equal to ``out_max``.
53
61
If ``x`` is less than ``out_min``, return ``out_min``.
54
62
If ``x`` is greater than ``out_max``, return ``out_max``.
55
63
Otherwise just return ``x``.
56
64
57
- :return: Returns value constrained to given range
65
+ :param float out_min: Lower bound of output range.
66
+ :param float out_max: Upper bound of output range.
67
+ :return: Returns value constrained to given range.
68
+ :rtype: float
58
69
"""
59
70
return max (out_min , min (x , out_max ))
0 commit comments