remap()#

Re-maps a number from one range to another.

Examples#

example picture for remap()
1def setup():
2    py5.size(200, 200)
3    value = 0.5
4    # remap 0.5 to be half of the width
5    m = py5.remap(value, 0, 1, 0, py5.width)
6    py5.ellipse(m, 100, 10, 10)
1def setup():
2    value = 110
3    m = py5.remap(value, 0, 100, -20, -10)
4    py5.println(m)  # Prints "-9.0"
 1def setup():
 2    py5.size(200, 200)
 3    py5.no_stroke()
 4
 5
 6def draw():
 7    py5.background(204)
 8    x1 = py5.remap(py5.mouse_x, 0, py5.width, 50, 150)
 9    py5.ellipse(x1, 75, 50, 50)
10    x2 = py5.remap(py5.mouse_x, 0, py5.width, 0, 200)
11    py5.ellipse(x2, 125, 50, 50)

Description#

Re-maps a number from one range to another.

In the first example, the number 0.5 is converted from a value in the range of 0 to 1 into a value that ranges from the left edge of the window (0) to the right edge (width).

As shown in the second example, numbers outside of the range are not clamped to the minimum and maximum parameters values, because out-of-range values are often intentional and useful. If that isn’t what you want, try pairing this function with constrain().

In Processing this functionality is provided by map() but was renamed in py5 because of a name conflict with a builtin Python function.

Syntax#

remap(value: Union[float, npt.NDArray], start1: Union[float, npt.NDArray], stop1: Union[float, npt.NDArray], start2: Union[float, npt.NDArray], stop2: Union[float, npt.NDArray]) -> Union[float, npt.NDArray]

Parameters#

  • start1: Union[float, npt.NDArray] - lower bound of the value’s current range

  • start2: Union[float, npt.NDArray] - lower bound of the value’s target range

  • stop1: Union[float, npt.NDArray] - upper bound of the value’s current range

  • stop2: Union[float, npt.NDArray] - upper bound of the value’s target range

  • value: Union[float, npt.NDArray] - the incoming value to be converted

Updated on February 26, 2022 13:22:44pm UTC