Microsoft Small Basic

Program Listing: ZTM557
GraphicsWindow.Title = "Cubic Brezier Curves [jibba jabba]"

duration = 1.5 'seconds
timeStep = 15 'ms
t = 1 / (duration * 1000 / timeStep) 't is a value between 0 and 1

radius = 10
ball = Shapes.AddEllipse(radius * 2, radius * 2)

heading = Shapes.AddText("Drag & Drop the Red Dots for Different Curves")
Shapes.Move(heading 10 10)

GraphicsWindow.BrushColor = "Red"
For i = 1 To 4
p[i] = Shapes.AddEllipse(radius * 2, radius * 2)
EndFor
GraphicsWindow.BrushColor = "White"

_x = "1=120;2=35;3=220;4=220"
_y = "1=160;2=200;3=260;4=40"

For i = 1 To 4
Shapes.Move(p[i], _x[i] -radius, _y[i] - radius)
EndFor

For i = 1 To 3
GraphicsWindow.DrawLine(_x[i] _y[i] _x[i+1] _y[i+1])
EndFor

AnimateBrezier()

GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.MouseUp = OnMouseUp

While 0=0
mseX = GraphicsWindow.MouseX
mseY = GraphicsWindow.MouseY

If mouseDown Then
For i = 1 To 4
If mseX >= Shapes.GetLeft(p[i]) And mseX <= Shapes.GetLeft(p[i]) + radius *2 Then
If mseY >= Shapes.GetTop(p[i]) And mseY <= Shapes.GetTop(p[i]) + radius *2 Then
select = i
EndIf
EndIf
EndFor
mouseDown = "False"
EndIf

If select <> "" And Mouse.IsLeftButtonDown Then
Shapes.Move(p[select], mseX - radius, mseY - radius)
ElseIf mouseUp Then
_x[select] = Shapes.GetLeft(p[select]) + radius
_y[select] = Shapes.GetTop(p[select]) + radius
GraphicsWindow.FillRectangle(0 0 GraphicsWindow.Width GraphicsWindow.Height)
For i = 1 To 3
GraphicsWindow.DrawLine(_x[i] _y[i] _x[i+1] _y[i+1])
EndFor
select = ""
AnimateBrezier()
mouseUp = "False"
mouseDown = "False"
EndIf

Program.Delay(15)
EndWhile


Sub OnMouseDown
mouseDown = "True"
EndSub

Sub OnMouseUp
mouseUp = "True"
EndSub

Sub AnimateBrezier
x = _x[1]
y = _y[1]

For i = 0 To 1 Step t
iT_3 = Math.Power((1 - i), 3)
iT_2 = Math.Power((1 - i), 2)
t_2 = Math.Power(i, 2)
t_3 = Math.Power(i, 3)

priorX = x
priorY = y

x = _x[1] * iT_3 + _x[2] * 3 * iT_2 * i + _x[3] * 3 * (1-i) * t_2 + _x[4] * t_3
y = _y[1] * iT_3 + _y[2] * 3 * iT_2 * i + _y[3] * 3 * (1-i) * t_2 + _y[4] * t_3

GraphicsWindow.DrawLine(priorX priorY x y)
Shapes.Move(ball x-radius y-radius)

Program.Delay(timeStep)
EndFor
EndSub