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