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 Vector2D
Private Ending(3) As Vector2D
Private Running As Boolean
Private mu As Single
Private mu2 As Vector2D
Private Speed As Single
Private 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