## 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:
1. Option Explicit
2.
3. Private Type Vector2D
4.
5.     X As Single
6.     Y As Single
7.
8. End Type
9.
10. Private Const PI As Single = 3.14159265358979 'Atn(1) * 4
11.
12. Private Vector As Vector2D
13.
14. Private Starting(3) As Vector2D
15. Private Ending(3) As Vector2D
16.
17. Private Running As Boolean
18. Private mu As Single
19. Private mu2 As Vector2D
20. Private Speed As Single
21. Private Current_Path As Long
22.
23. 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
24.
25.     If (X_End >= X_Start) Then
26.         mu = mu + Convert_Speed_To_MU(Speed, X_Start, X_End)
27.     Else
28.         mu = mu + Convert_Speed_To_MU(-Speed, X_Start, X_End)
29.     End If
30.
31.     If mu <= 0 Then mu = 0
32.     If mu >= 1 Then
33.         mu = 1
34.         Linear_Interpolation_1D = True
35.     End If
36.
37.    Vertex = (X_Start * (1 - mu) + X_End * mu)
38.
39. End Function
40.
41. 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
42.
44.
46.
47.     mu.X = mu.X + Convert_Speed_To_MU(Speed, X_Start, X_End) * Cos(Radian)
48.     mu.Y = mu.Y + Convert_Speed_To_MU(Speed, Y_Start, Y_End) * Sin(Radian)
49.
50.     If mu.X <= 0 Then mu.X = 0
51.     If mu.X >= 1 Then mu.X = 1
52.
53.     If mu.Y <= 0 Then mu.Y = 0
54.     If mu.Y >= 1 Then mu.Y = 1
55.
56.     If mu.X = 1 Or mu.Y = 1 Then Linear_Interpolation_2D = True
57.
58.     Vector.X = (X_Start * (1 - mu.X) + X_End * mu.X)
59.     Vector.Y = (Y_Start * (1 - mu.Y) + Y_End * mu.Y)
60.
61. End Function
62.
63. Private Function Convert_Speed_To_MU(ByVal Speed As Single, ByVal X_Start As Single, ByVal X_End As Single) As Single
64.
65.     If (X_End - X_Start) <> 0 Then Convert_Speed_To_MU = Speed / (X_End - X_Start)
66.
67. End Function
68.
69. Private Function Get_Radian(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
70.
71.     Dim DX As Single, DY As Single
72.     Dim Angle As Single
73.
74.         DX = X2 - X1
75.         DY = Y2 - Y1
76.
77.         Angle = 0
78.
79.         If DX = 0 Then
80.             If DY = 0 Then
81.                 Angle = 0
82.             ElseIf DY > 0 Then
83.                 Angle = PI / 2
84.             Else
85.                 Angle = PI * 3 / 2
86.             End If
87.         ElseIf DY = 0 Then
88.             If DX > 0 Then
89.                 Angle = 0
90.             Else
91.                 Angle = PI
92.             End If
93.         Else
94.             If DX < 0 Then
95.                 Angle = Atn(DY / DX) + PI
96.             ElseIf DY < 0 Then
97.                 Angle = Atn(DY / DX) + (2 * PI)
98.             Else
99.                 Angle = Atn(DY / DX)
100.             End If
101.         End If
103.
104. End Function
105.
106. Private Function Draw_Rectangle(ByVal X As Single, ByVal Y As Single, ByVal Width As Single, ByVal Height As Single, ByVal Color As Long)
107.
108.     Me.Line (X, Y)-(X + Width, Y + Height), Color, B
109.
110. End Function
111.
113.
114.     Me.Show
115.     Me.ScaleMode = vbPixels
116.     Me.BackColor = RGB(0, 0, 0)
117.     Me.AutoRedraw = True
118.
119.     Starting(0).X = 10: Starting(0).Y = 10: Ending(0).X = 100: Ending(0).Y = 10
120.     Starting(1).X = 100: Starting(1).Y = 10: Ending(1).X = 100: Ending(1).Y = 100
121.     Starting(2).X = 100: Starting(2).Y = 100: Ending(2).X = 10: Ending(2).Y = 100
122.     Starting(3).X = 10: Starting(3).Y = 100: Ending(3).X = 10: Ending(3).Y = 10
123.
124.     Vector.X = Starting(0).X: Vector.Y = Starting(0).Y
125.
126.     Running = True
127.
128.     Do While Running = True
129.         Cls
130.         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
131.             If Current_Path <> UBound(Starting) Then
132.                 Current_Path = Current_Path + 1
133.                 mu2.X = 0
134.                 mu2.Y = 0
135.             Else
136.                 Current_Path = UBound(Starting)
137.
138.                 'To have it repeat, Comment out the line above and use these instead:
139.                     'Current_Path = 0
140.                     'mu2.X = 0
141.                     'mu2.Y = 0
142.             End If
143.         End If
144.
145.         Draw_Rectangle Vector.X, Vector.Y, 20, 20, RGB(255, 255, 255)
146.         DoEvents
147.     Loop
148.
149. End Sub
150.
151. Private Sub Form_Unload(Cancel As Integer)
152.
153.     Running = False