-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|690|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> Variables


 
Pakz
Created : 12 November 2017
 
Language : Monkey-X

Lerp function (Linear Interpolation)

Return number between low and high

Flash example of this code

Latest Update

I noticed this lerp function yesterday while watching a episode from The Coding Train. Here they used the lerp function to create a morphing effect.(#circlemorphing - see this twitter hasktag for example animated gifs)

Lerp I think is short for Linear Interpolation. The Lerp function basically takes a input value from 0.0 to 1.0 and returns a value based on the from low to high.

So Lerp(0,5,0,10) returns 5
Lerp(0.0,20,30) returns 20

I think maybe this linear interpolation is quite useful. I created a patrolling example here. It probably can be used to make a lot more.

I am not that experienced with math so I was quite happy to learn this one.

 

Comments


Sunday, 12 November 2017, 07:58
GfK
Yeah it means linear interpolation. Unity has Mathf.lerp() which does this. Also has built in functionality to lerp between vector2/3/4s, quaternions, colours and materials.

Handy stuff to use for transitioning gradually from one state of affairs to another.
Sunday, 12 November 2017, 08:19
Jayenkai
Personally I tend to put the percentage at the end, so it’s From, To, Perc... Oh, and I use percentages instead of 0-1.0
I’ve had this kind of function in my past few Frameworks, but haven’t yet added it to my latest, mostly because I simply haven’t really used it all that much.
It’s great for mixing colours, but then you’re better off making an RGB specific version just for that purpose.

Instead I tend to use my smooth-transition thing of From=From-((From-To)/ApproxFrames), which starts the transition off fast, then smooths off towards the end.
Smooth scrolling, score "ticking up" and more, are all done with that.

I’ve become very reliant on it, but oddly it doesn’t seem to be working very well in c++... hmmm..
Sunday, 12 November 2017, 09:39
Pakz
I noticed that Monkey2 has a Blend option that can be used after a color command.

color = New Color(0.5,0.5,0.5).Blend(New Color(1.0,0,0),.5)

I quite like that.

I want to spend more time googling for math functions and their uses. It seems I am still in the dark ages with these things.