Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Linear Interpolation

edited April 2016 in Examples Posts: 20

Hey! I recently discovered that lua lacks a pre-wrote function for Linear interpolation. So I decided to write my own!

function lerp(pos1, pos2, perc)
    return (1-perc)*pos1 + perc*pos2 -- Linear Interpolation
end

This could be used in so many ways! From animations, to just trying to get that sweet spot on the screen. This is sure to come in handy!

EX:


-- Linear Interpolation -- Use this function to perform your initial setup function setup() print("Hello World!") t=1 end function lerp(pos1, pos2, perc) return (1-perc)*pos1 + perc*pos2 -- Linear Interpolation end -- This function gets called once every frame function draw() -- This sets a dark background color fontSize(50) strokeWidth(5) background(255, 0, 221, 255) if t > 1 then t=0 end t = t + 0.01 -- This sets the line thickness text("Purple rain", lerp(0, WIDTH, t), lerp(0, HEIGHT, t)) fill(255, 255, 255, 255) -- Do your drawing here end

Comments

  • AnatolyAnatoly Mod
    Posts: 830

    But shouldn't the rain go down? Interesting Gravity crisis... Any ways, good way, I learned something new ;)

  • AnatolyAnatoly Mod
    edited April 2016 Posts: 830

    For a reason, codes break up for me,

    t=0
    function lerp(pos1, pos2, perc)
    return perc*(pos1+pos2) -- Linear Interpolation
    end function draw()
    fontSize(50) background(255, 0, 221, 255) if t > 1 then t=0 end t = t + 0.02 for x = 1, 10 do for y = 1, 10 do
    text("

  • Posts: 20

    Weird

  • dave1707dave1707 Mod
    Posts: 7,675

    @LuaLlama Have you looked at the tween functions. Those can do what you did and they have a lot more functionality.

    displayMode(FULLSCREEN)
    
    function setup()
        sp={x=0,y=0}
        tween(2,sp,{x=WIDTH,y=HEIGHT},{loop=tween.loop.forever})
        fontSize(50)
    end
    
    function draw()
        background(255, 0, 221, 255)
        fill(255)
        text("Purple Rain",sp.x,sp.y)
    end
    
  • Posts: 20

    I'll look at that Dave, thanks!

Sign In or Register to comment.