Skip to content

Reference: map1d & p5map

Martin Prout edited this page Aug 21, 2015 · 3 revisions

Examples

size(200, 200)
value = 25
m = map1d(value, (0..100), (0..width))
ellipse(m, 200, 10, 10)

value = 110
m = map1d(value, (0..100), (-20..-10))
puts m  # Prints "-9.0"

def setup
  size(200, 200)
  no_stroke
end

def draw
  background(204)
  x1 = map1d(mouse_x, (0..width), (50..150))
  ellipse(x1, 75, 50, 50)  
  x2 = map1d(mouse_x, (0..width), (0..200))
  ellipse(x2, 125, 50, 50)  
end

or using p5map

def setup
  size(200, 200)
  no_stroke
end

def draw
  background(204)
  x1 = p5map(mouse_x, 0, width, 50, 150)
  ellipse(x1, 75, 50, 50)  
  x2 = p5map(mouse_x, 0, width, 0, 200)
  ellipse(x2, 125, 50, 50)  
end

Description Re-maps a number from one range (linear) to another (linear).

In the first example above, the number 25 is converted from a value in the range of 0 to 100 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.

Syntax

map1d(value, (start1..stop1), (start2..stop2))

Where value is the value to be translated, and (start1..stop1) is the current range, and (start2..stop2) is the target range.