## 2D Linear Interpolation Of Sprites

If you need a way for a creature to go from node to node in your 2D world at any angle, or need a patrol path then this simple code is for you. I have functions for 1D as well as 2D to do this. Now commonly when you google up code for linear interpolation, you tend to see this function, which is one dimensional:

Vertex = (X_Start * (1 - mu) + X_End * mu)

where mu is anywhere between 0 and 1. 0 is the starting point and 1 is the ending point and anywhere in between is the sprite moving from that point to the other. However I feel it goes beyond that. First of all, how can you determine the speed of how fast the sprite is moving from that point to the other accurately? Or how bout this. How bout the 2D version of the code! Its hard to find and I have managed to pull off all these unanswered questions and also will be blending it in my Bosskillers game I'm currently working on. This code is in VB6, however its very easy to convert to VB.Net, C#, and C++ or any other language you are working with. Enjoy the code:

vb Code:
`Option Explicit Private Type Vector2D        X As Single    Y As Single End Type Private Const PI As Single = 3.14159265358979 'Atn(1) * 4 Private Vector As Vector2D Private Starting(3) As Vector2DPrivate Ending(3) As Vector2D Private Running As BooleanPrivate mu As SinglePrivate mu2 As Vector2DPrivate Speed As SinglePrivate Current_Path As Long Private Function Linear_Interpolation_1D(ByVal Vertex As Single, ByVal X_Start As Single, ByVal X_End As Single, ByRef mu As Single, ByVal Speed) As Boolean     If (X_End >= X_Start) Then        mu = mu + Convert_Speed_To_MU(Speed, X_Start, X_End)    Else        mu = mu + Convert_Speed_To_MU(-Speed, X_Start, X_End)    End If        If mu <= 0 Then mu = 0    If mu >= 1 Then        mu = 1        Linear_Interpolation_1D = True    End If       Vertex = (X_Start * (1 - mu) + X_End * mu) End Function Private Function Linear_Interpolation_2D(ByRef Vector As Vector2D, ByVal X_Start As Single, ByVal Y_Start As Single, ByVal X_End As Single, ByVal Y_End As Single, ByRef mu As Vector2D, ByVal Speed As Single) As Boolean        Dim Radian As Single        Radian = Get_Radian(X_Start, Y_Start, X_End, Y_End)        mu.X = mu.X + Convert_Speed_To_MU(Speed, X_Start, X_End) * Cos(Radian)    mu.Y = mu.Y + Convert_Speed_To_MU(Speed, Y_Start, Y_End) * Sin(Radian)        If mu.X <= 0 Then mu.X = 0    If mu.X >= 1 Then mu.X = 1     If mu.Y <= 0 Then mu.Y = 0    If mu.Y >= 1 Then mu.Y = 1        If mu.X = 1 Or mu.Y = 1 Then Linear_Interpolation_2D = True        Vector.X = (X_Start * (1 - mu.X) + X_End * mu.X)    Vector.Y = (Y_Start * (1 - mu.Y) + Y_End * mu.Y)    End Function Private Function Convert_Speed_To_MU(ByVal Speed As Single, ByVal X_Start As Single, ByVal X_End As Single) As Single        If (X_End - X_Start) <> 0 Then Convert_Speed_To_MU = Speed / (X_End - X_Start) End Function Private Function Get_Radian(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single     Dim DX As Single, DY As Single    Dim Angle As Single         DX = X2 - X1        DY = Y2 - Y1                Angle = 0         If DX = 0 Then            If DY = 0 Then                Angle = 0            ElseIf DY > 0 Then                Angle = PI / 2            Else                Angle = PI * 3 / 2            End If        ElseIf DY = 0 Then            If DX > 0 Then                Angle = 0            Else                Angle = PI            End If        Else            If DX < 0 Then                Angle = Atn(DY / DX) + PI            ElseIf DY < 0 Then                Angle = Atn(DY / DX) + (2 * PI)            Else                Angle = Atn(DY / DX)            End If        End If        Get_Radian = Angle End Function Private Function Draw_Rectangle(ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single, ByVal Color As Long)     Me.Line (X, Y)-(X + Width, Y + Height), Color, B    End Function Private Sub Form_Load()        Me.Show    Me.ScaleMode = vbPixels    Me.BackColor = RGB(0, 0, 0)    Me.AutoRedraw = True        Starting(0).X = 10: Starting(0).Y = 10: Ending(0).X = 100: Ending(0).Y = 10    Starting(1).X = 100: Starting(1).Y = 10: Ending(1).X = 100: Ending(1).Y = 100    Starting(2).X = 100: Starting(2).Y = 100: Ending(2).X = 10: Ending(2).Y = 100    Starting(3).X = 10: Starting(3).Y = 100: Ending(3).X = 10: Ending(3).Y = 10        Vector.X = Starting(0).X: Vector.Y = Starting(0).Y        Running = True        Do While Running = True        Cls        If Linear_Interpolation_2D(Vector, Starting(Current_Path).X, Starting(Current_Path).Y, Ending(Current_Path).X, Ending(Current_Path).Y, mu2, 1) = True Then            If Current_Path <> UBound(Starting) Then                Current_Path = Current_Path + 1                mu2.X = 0                mu2.Y = 0            Else                Current_Path = UBound(Starting)                                    'To have it repeat, Comment out the line above and use these instead:                    'Current_Path = 0                    'mu2.X = 0                    'mu2.Y = 0            End If        End If                    Draw_Rectangle Vector.X, Vector.Y, 20, 20, RGB(255, 255, 255)        DoEvents    Loop        End Sub Private Sub Form_Unload(Cancel As Integer)     Running = False    Unload Me End Sub`