Lerp - Linear Interpolation
issueid=331 05-11-2011 22:12
Number of reported issues by dcromley: 12
Lerp - Linear Interpolation
I use my "Lerp" quite often. Would anybody else?

In my primary tBasicU file, I have the function "Lerp" which I use quite often. It is plain, ordinary linear interpolation. I actually thought that "Lerp" was a function common to other languages, but after searching for this post, it's not that common. But maybe it would be popular in the core? "type" below would probably be "double". As I just told Petr, I am often surprised at new (to me) features that I find in TB. Thanks. Dave

Function Lerp (Start as type, End as type, Val01) as type

Function = (1 - Val01) * Start + Val01 * End
Function = Start + Val01 * (End - Start)
End Function
Issue Details
Issue Number 331
Issue Type Feature
Project thinBasic
Category Core engine (thinCore.dll)
Status Implemented
Priority 10 - Lowest
Suggested Version 1.8.9
Implemented Version 1.9.1
Milestone thinBasic
Votes for this feature 0
Votes against this feature 0
Assigned Users (none)
Tags (none)

06-11-2011 10:29
thinBasic author
Hi Dave,

I will be happy to add it but can you please give me some reference about the formula of Lerp function?
If I add a new function I need to create help documentation so I need to explain it to all people using thinBasic

What are parameters? "Start", "End" and "Val01"
What is the meaning of function returned value?
Can you write a sinple example usage?

Thanks a lot

06-11-2011 13:40
Super Moderator
Hi Eros,

better to wait till Dave confirms it, but I think the LERP function should do Linear Interpolation.

That means:
  • Start = x0
  • End = x1
  • Value = amount of interpolation

For example, if x0 = 3 and x1 = 5, then
  • for amount of interpolation 0, the function returns x0 (3 in this case)
  • for amount of interpolation 1, the function returns x1 (5 in this case)
  • for amount of interpolation 0.5, the function returns 50% blend between the values, that is x0 + amount of interpolation * (x1 - x0) = 3 + 0.5 * (5 - 3) = 4

It is quite useful function, maybe it could go to the Math module.


06-11-2011 14:02
thinBasic author
Thanks Petr.

Because there can be different methods of interpolation depending on different data sets, and in future we can develop different thinBasic functions, it is better to find a correct function name and not just LERP. Which one are we referring? Interpolation by approximation?
What about if we develop an interpolation function working on data sets?


06-11-2011 22:15
Petr's post was good.
I had wanted to give the link:
And yes, the "Math" module would be more appropriate.

I thought there were more references than I found today, so it exists, but far from prevalent. A few references:

Lerp: Calculates a number between two numbers at a specific increment. The amt parameter is the amount to interpolate between [value1 and value2] where amt=0.0 returns first amount, 0.1 is very near the first amount, 0.5 is half-way in between, etc. The lerp function is convenient for creating motion along a straight path and for drawing dotted lines.
float lerp(float value1, float value2, float amt)

T Lerp( T& val1, T& val2, float u)
{ return (1 - u) * val1 + u * val2; }

"The Lerp function takes a start and end argument, and a floating point number which you can think of as being the percentage complete of the movement. Usually this argument is based on time. So at time=0.0 Lerp will return the first number, at 1.0 it will return the 2nd number, and for numbers less than 1.0 it will return some number in-between."

static function Lerp(start : float, end : float, value : float) : float
{ return ((1.0 - value) * start) + (value * end); }

lerp () method
public static function lerp(pct:Number, min:Number, max:Number):Number
Performs linear interpolation, finding the value that is pct percent between min and max.
Parameters percent:Number; min:Number; max:Number
Returns: Number

Example: To put 7 points evenly spaced from .3 to .9 along the x-axis:
For i = 0 to 6
  TBGL_Point lerp(.3, .9, i/6), 0, 0
next i
And Eros has the important point, "it's important to find a correct function name". As shown above, "Lerp" is used, but I don't know if there would be any conflicts with other usage.

Thanks for your consideration, Dave

[Wow, I just noticed that the axiis.org reference has the parameters in a different order. That's not good.]

07-11-2011 18:42
Super Moderator
Hi Dave,

for the example you give, the feature brought up by Eros would be even better - working on data sets.
That would allow interpolating whole series of data in array using single command for example.

Eros is right about the proper name - the "lerp" and other short names are not very explanative, and they pose high risk of collision with user variable names and such.


08-11-2011 00:41
.. correct function name ..
I agree with you both.
I think this is best tabled (heldup) until further notice.

I must confess I don't understand "working on datasets".
Would this be interpolation of vectors/arrays?

13-11-2011 11:59
thinBasic author
It will be present in next release

15-11-2011 05:57
' There were 2 versions of Lerp on the internet --
' some with the fraction first, and some last.
' You choose.  Or take a poll.
' I will use it either way.  
' Here are 2 examples in a program.  Regards, Dave

  Uses "UI"

Sub TBMain()
  Local hwnd, i As Long, x, y As Single
  hWnd = Canvas_Window "Test Canvas Window", 0, 0, 700, 200
  Canvas_Attach hWnd, 0, %FALSE                             
  For i = 100 To 600 Step 100
    Canvas_Line( (i, 50), (i, 150) )
  Next i                      
  ' use Lerp to put 7 circles between 200 and 300, including 200 and 300
  For i = 0 To 6
    x = Lerp1(i/6, 200,300)
    Canvas_Circle( x, 100, 3)
  Next i
  ' use Lerp to put 7 circles between 400 and 500, NOT including 400 and 500
  For i = 1 To 7
    x = Lerp1(i/8, 400,500)
    Canvas_Circle( x, 100, 3)
  Next i
  Canvas_Input("Any key to exit")
End Sub

' Lerp1 has the fraction as the first parameter
' Returns val1 if fraction=0; val2 if fraction=1; 
' the linear interpolation for other values.
Function Lerp1(fraction As Single, val1 As Single, val2 As Single) As Single
  Function = (1-fraction)*val1 + fraction*val2
End Function

' Lerp2 has the fraction as the last parameter
' Returns val1 if fraction=0; val2 if fraction=1; 
' the linear interpolation for other values.
Function Lerp2(val1 As Single, val2 As Single, fraction As Single) As Single
  Function = (1-fraction)*val1 + fraction*val2
End Function

15-11-2011 22:26
thinBasic author
I will create both

18-11-2011 18:58
(Having Beta I like Lerp and Lerp2.
(I'll use Lerp2). I hope they will be popular.

You need to change the Help for Lerp2:
Lerp: n = Lerp(pStart, pEnd, Amount)
Lerp2: n = Lerp(pStart, pEnd, Amount)

The following shows that Lerp2 help should be
n = Lerp2(Amount, pStart, pEnd)

  Uses "Console", "Math"
  PrintL  Lerp(.5, .6, .7) ' .57 is 70% from .5 to .6
  PrintL Lerp2(.5, .6, .7) ' .65 is 50% from .6 to .7
Thanks again, Dave

19-11-2011 10:49
thinBasic author
Will be present in next beta update.

+ Reply