Microsoft Small Basic

Program Listing: JLF545-1
' Time-Velocity Graph
' Version 0.3
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-07-26
' Program ID JLF545-1
'
title = "Time-Velocity Graph"
GraphicsWindow.Title = title
showDelta = "False"
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.PenColor = "#CCCCCC"
delta = 10
x0 = 0
y0 = 0
DrawGrid()
GraphicsWindow.PenColor = "#999999"
x0 = 50
delta = 100
DrawGridAndValue()
GraphicsWindow.PenColor = "#333333"
y0 = y0 + 400
delta = 1000
DrawGrid()
x1 = x0 + 200
y1 = y0 - 300
GraphicsWindow.PenColor = "#000000"
GraphicsWindow.DrawLine(x0, y0, x1, y1)
x2 = x1 + 100
y2 = y1
GraphicsWindow.DrawLine(x1, y1, x2, y2)
x3 = x2 + 200
y3 = y0
GraphicsWindow.DrawLine(x2, y2, x3, y3)
GraphicsWindow.BrushColor = "#333333"
GraphicsWindow.DrawText(6, 80, "[m/s]")
GraphicsWindow.DrawText(gw - 20, gh - 24, "[s]")
GraphicsWindow.BrushColor = "#CCCCCC"
GraphicsWindow.FillRectangle(0, 0, gw, 70)
GraphicsWindow.DrawLine(20, 60, 500 + 20, 60)
GraphicsWindow.BrushColor = "#000000"
For tx = 0 To 500 Step 5
If Math.Remainder(tx, 50) = 0 Then
h = 8
GraphicsWindow.DrawText(tx + 16, 36, tx / 50)
Else
h = 4
EndIf
GraphicsWindow.DrawLine(tx + 20, 60 - h, tx + 20, 60)
EndFor
GraphicsWindow.DrawText(550 + 10, 36, "[m]")
Turtle.Show()
Turtle.Speed = 8
Turtle.PenUp()
Turtle.MoveTo(20, 20)
a = Math.Floor(Turtle.Angle / 10) * 10
For a = a To 90 Step 10
Turtle.Angle = a
Program.Delay(50)
EndFor
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Green"
size = 8
point = Shapes.AddEllipse(size, size)
d = 0 ' distance [m]
dt = 0.02 ' [s]
ty = 20
GraphicsWindow.BrushColor = "#66000000"
If showDelta Then
tEnd = 1.1
Else
tEnd = 5
EndIf
For t = 0 To tEnd Step dt
Ft()
ConvertTVtoXY()
Shapes.Move(point, x - size / 2, y - size / 2)
If 0 < t Then
d = d + (vLast + v) * dt / 2
tx = d * 50 + 20
Turtle.MoveTo(tx, ty)
If showDelta And 1 <= tLast Then
GraphicsWindow.BrushColor = "#99000000"
EndIf
If yLast > y Then
yMin = y
GraphicsWindow.FillTriangle(xLast, yMin, x, yMin, xLast, yLast)
ElseIf yLast < y Then
yMin = yLast
GraphicsWindow.FillTriangle(xLast, yMin, x, yMin, x, y)
EndIf
GraphicsWindow.FillRectangle(xLast, yMin, x - xLast, y0 - yMin)
EndIf
tLast = t
vLast = v
xLast = x
yLast = y
Program.Delay(dt * 1000)
EndFor
Sub Ft
' param t - time [s]
' return v - velocity [m/s]
If 0 < t And t < 2 Then
v = t / 2 * 3
GraphicsWindow.Title = title + " - uniform accelerated motion"
ElseIf 2 <= t And t < 3 Then
v = 3
GraphicsWindow.Title = title + " - uniform motion"
ElseIf 3 <= t And t < 5 Then
v = 3 - (t - 3) / 2 * 3
GraphicsWindow.Title = title + " - uniform accelerated motion"
Else
v = 0
GraphicsWindow.Title = title + " - uniform motion"
EndIf
EndSub
Sub ConvertTVtoXY
' param t, v - time, velocity
' param x, y - graphics window coordinate
x = x0 + t * 100
y = y0 - v * 100
EndSub
Sub DrawGrid
For x = x0 To gw Step delta
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = y0 To gh Step delta
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub DrawGridAndValue
GraphicsWindow.BrushColor = "#000000"
value = 0
For x = x0 To gw Step delta
GraphicsWindow.DrawLine(x, 0, x, gh)
GraphicsWindow.DrawText(x - 10, gh - 24, value)
value = value + 1
EndFor
value = 4
For y = y0 To gh Step delta
GraphicsWindow.DrawLine(0, y, gw, y)
GraphicsWindow.DrawText(40, y + 4, value)
value = value - 1
EndFor
EndSub