Built-in interpolation functions

  • I’m curious if you think there is value in adding interpolation functions to DrawBot, similar to lerp(), norm(), and map() in Processing/p5.js.

    I understand it’s not good to bloat the initial state, and perhaps this is better handled by an external package. (Or maybe there is already a built-in way to deal with this that I don’t know about?)

    Interpolation plays such a big part in so many of my scripts and those I create with my students, and I have cut and pasted the code below into so many of my DrawBot scripts that I felt moved to write this post and ask.

    In case you are interested, I am happy to contribute the code I use. It uses the same syntax as Processing, except that remap() is used to avoid conflict with the Python map() function.

    Thank you, as always

    # Released by DJR under the BSD license 
    def lerp(start, stop, amt):
    	Return the interpolation factor (between 0 and 1) of a VALUE between START and STOP.
    	return float(amt-start) / float(stop-start)
    def norm(value, start, stop):
    	Interpolate using a value between 0 and 1
    	See also: https://processing.org/reference/norm_.html
    	return start + (stop-start) * value
    def remap(value, start1, stop1, start2, stop2, withinBounds=False):
        Re-maps a number from one range to another.
        factor = lerp(start1, stop1, value)
        if withinBounds:
            if factor < 0: factor = 0
            if factor > 1: factor = 1
        return norm(factor, start2, stop2)

  • admin

    could you move this topic to a github issue?


Log in to reply