Microsoft Small Basic

Program Listing: JDR564-0
' Spring 0.1
' Copyright (c) 2012 Nonki Takahashi. All rights reserved.
'
' History:
' 2012/10/13 Created.
'
' Timer Event Mod
' JDR564-0

GraphicsWindow.Title = "Spring 0.1"

scale = 3139 ' [dot/m]

l = .05 ' spring length [m]
k = 20 ' spring constant [N/m]
m = .01 ' mass of weight [kg]
d = .01 ' diameter of weight [m]
g = 9.8 ' acceleration of gravity [m/(s^2)]
xs = .1 ' stationary point from left [m]
ys = 0 ' stationary point from top [m]

x0 = xs - d / 2
y0 = ys + l
y = y0 + .05

v0 = 0
v = v0

fps = 25
time = Math.Round(1000/fps)
dt = .05

InitWeight()

Timer.Tick = Animation
Timer.Interval = time

GraphicsWindow.TextInput = PauseOrExit

Sub Animation

ShowWeight()

dv = (m*g - (y - y0) * k) * dt
v = v + dv

dy = (2*v + dv) * dt/2
y = y + dy

EndSub

Sub InitWeight

GraphicsWindow.PenColor = "DimGray"
spring = Shapes.AddLine(0,0 0,l*scale)
Shapes.Move(spring xs*scale,ys*scale)

GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Black"
weight = Shapes.AddEllipse(d*scale, d*scale)
Shapes.Move(weight 0,-100)

EndSub

Sub ShowWeight

Shapes.Move(weight x0*scale, y*scale)
Shapes.Move(spring xs*scale, ((y - y0)/2 + ys) * scale)
Shapes.Zoom(spring 1, (y - y0 + l) / l)
Shapes.SetOpacity( spring 100*l / (y - y0 + l) )

EndSub

Sub PauseOrExit

If GraphicsWindow.LastText = " " Then

If isPaused Then
isPaused = "False"
Timer.Resume()
Else
isPaused = "True"
Timer.Pause()
EndIf

Else

Sound.PlayClick()
Program.End()

EndIf

EndSub