Microsoft Small Basic

Program Listing: NZR510-0
' Tennis Ball
' Copyright (c) 2013 Nonki Takahashi. All rights reserved.
'
' History:
' 0.2 29/05/2013 Correct calculation for delta y. (NZR510-0)
' 0.1 28/05/2013 Created. (NZR510)
' 0.0 28/05/2013 18:29:40 Shapes generated by Shapes 1.5.
'
GraphicsWindow.Title = "Tennis Ball"
GraphicsWindow.BackgroundColor = "SkyBlue"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
' initialize shapes
SB_Workaround()
Shapes_Init()
' draw grid
scale = 0.3 ' [px/mm] ... is about 1/10 scale
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
gi = 500 ' grid interval [mm]
DrawGrid()
' add shapes
angle = 0
iMin = 1
iMax = 3
Shapes_Add()
Console_Init()
Controls.ButtonClicked = OnButtonClicked
Sub OnButtonClicked
Controls.ButtonClicked = DoNothing
Console_Input()
Slope_Add()
as = Math.GetRadians(angleSlope) ' slope angle [radian]
rb = size / 2 * scale ' radius of ball [px]
y = gh - gw * Math.Tan(as) - rb * 2
vy = 0 ' velocity of y [px/s]
ay = g * Math.Power(Math.Sin(as), 2) * 1000 * scale ' acceleration of y [px/s^2]
angle = 0
CalcX()
Shapes_Move()
dy = 1
dt = 1 / 24 ' [sec]
dvy = ay * dt
Timer.Interval = dt * 1000 ' [ms]
Timer.Tick = OnTick
Timer.Resume()
EndSub
Sub DoNothing
OnButtonClicked = ""
EndSub
Sub OnTick
If gh - 2 * rb < y Then
Timer.Pause()
Controls.ButtonClicked = OnButtonClicked
Else
vy = vy + dvy
dy = (2 * vy + dvy) * dt / 2
y = y + dy
angle = angle - Math.GetDegrees((dy / Math.Sin(as)) / rb)
If angle < 0 Then
angle = angle + 360
EndIf
Shapes_Rotate()
CalcX()
Shapes_Move()
EndIf
EndSub
Sub CalcX
' param y - [px]
' param as - slope angle [radian]
' param rb - ball radiuse [px]
' return x - [px]
x = (gh - y - rb - rb / Math.Cos(as)) / Math.Tan(as) - rb
EndSub
Sub DrawGrid
' param gi - grid interval [mm]
GraphicsWindow.PenColor = "Gray"
For x = 0 To gw Step gi * scale
GraphicsWindow.DrawLine(x, 0, x, gh)
EndFor
For y = gh To 0 Step -gi * scale
GraphicsWindow.DrawLine(0, y, gw, y)
EndFor
EndSub
Sub Console_Init
weight = 57 ' weight of tennis ball [g] - not used
size = 66 ' size (diameter) of tennis ball [mm]
g = 9.8 ' acceleration of gravity [m/s^2]
If silverlight Then
Program.Delay(200)
EndIf
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(10, 14, "Angle =")
oAngle = Controls.AddTextBox(60, 10)
Controls.SetTextBoxText(oAngle, 5)
Controls.SetSize(oAngle, 50, 24)
GraphicsWindow.DrawText(120, 14, "[degree]")
Controls.AddButton("Start", 180, 10)
txt = "Ball Size = " + (size / 10) + " [cm]" + CRLF
txt = txt + "Grid Interval = " + (gi / 10) + " [cm]"
GraphicsWindow.DrawBoundText(400, 10, 200, txt)
EndSub
Sub Console_Input
' return angleSlope - angle of slope
angleSlope = Controls.GetTextBoxText(oAngle)
EndSub
Sub Slope_Add
' param angleSlope - angle of slope
If slope <> "" Then
Shapes.Remove(slope)
EndIf
x1 = 0
y1 = gh
x2 = gw
y2 = gh
x3 = gw
y3 = gh - gw * Math.Tan(Math.GetRadians(angleSlope))
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "DimGray"
slope = Shapes.AddTriangle(x1, y1, x2, y2, x3, y3)
EndSub
Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 240 ' x offset
shY = 14 ' y offset
shape = ""
shape[1] = "func=ell;x=0;y=0;width=66;height=66;bc=#CDC95A;pc=#999999;pw=3;"
shape[2] = "func=ell;x=44;y=11;width=22;height=44;bc=#CDC95A;pc=#999999;pw=3;"
shape[3] = "func=ell;x=0;y=11;width=22;height=44;bc=#CDC95A;pc=#999999;pw=3;"
EndSub
Sub Math_CartesianToPolar
' Math | convert cartesian coodinate to polar coordinate
' param x, y - cartesian coordinate
' return r, a - polar coordinate
r = Math.SquareRoot(x * x + y * y)
If x = 0 And y > 0 Then
a = 90 ' [degree]
ElseIf x = 0 And y < 0 Then
a = -90
Else
a = Math.ArcTan(y / x) * 180 / Math.Pi
EndIf
If x < 0 Then
a = a + 180
ElseIf x > 0 And y < 0 Then
a = a + 360
EndIf
EndSub
Sub SB_RotateWorkaround
' Small Basic | Rotate workaround for Silverlight
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shape[i]["func"] = "tri" Then
x1 = -Math.Floor(shape[i]["x3"] / 2)
y1 = -Math.Floor(shape[i]["y3"] / 2)
ElseIf shape[i]["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shape[i]["x1"] - shape[i]["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shape[i]["y1"] - shape[i]["y2"]) / 2)
EndIf
ox = x - x1
oy = y - y1
x = x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha) + ox
y = x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha) + oy
EndSub
Sub SB_Workaround
' Small Basic | Workaround for Silverlight
' returns silverlight - "True" if in remote
color = GraphicsWindow.GetPixel(0, 0)
If Text.GetLength(color) > 7 Then
silverlight = "True"
msWait = 300
Else
silverlight = "False"
EndIf
EndSub
Sub Shapes_Add
' Shapes | add shapes as shapes data
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - 1 if same scale
' return shWidth, shHeight - total size of shapes
' return shAngle - current angle of shapes
Stack.PushValue("local", i)
Shapes_CalcWidthAndHeight()
s = scale
For i = iMin To iMax
GraphicsWindow.PenWidth = shape[i]["pw"] * s
If shape[i]["pw"] > 0 Then
GraphicsWindow.PenColor = shape[i]["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text", shape[i]["func"]) Then
GraphicsWindow.BrushColor = shape[i]["bc"]
EndIf
If shape[i]["func"] = "rect" Then
shape[i]["obj"] = Shapes.AddRectangle(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "ell" Then
shape[i]["obj"] = Shapes.AddEllipse(shape[i]["width"]* s, shape[i]["height"] * s)
ElseIf shape[i]["func"] = "tri" Then
shape[i]["obj"] = Shapes.AddTriangle(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s, shape[i]["x3"] * s, shape[i]["y3"] * s)
ElseIf shape[i]["func"] = "line" Then
shape[i]["obj"] = Shapes.AddLine(shape[i]["x1"] * s, shape[i]["y1"] * s, shape[i]["x2"] * s, shape[i]["y2"] * s)
ElseIf shape[i]["func"] = "text" Then
If silverlight Then
fs = Math.Floor(shape[i]["fs"] * 0.9)
Else
fs = shape[i]["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shape[i]["fn"]
shape[i]["obj"] = Shapes.AddText(shape[i]["text"])
EndIf
If silverlight And shape[i]["func"] = "tri" Then
alpha = Math.GetRadians(shape[i]["angle"])
x1 = -Math.Floor(shape[i]["x3"] / 2)
y1 = -Math.Floor(shape[i]["y3"] / 2)
ox = shape[i]["x"] - x1
oy = shape[i]["y"] - y1
r = Math.SquareRoot(x1 * x1 + y1 * y1)
x = x1 * Math.Cos(alpha) - y1 * Math.Sin(alpha) + ox
y = x1 * Math.Sin(alpha) + y1 * Math.Cos(alpha) + oy
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
Else
Shapes.Move(shape[i]["obj"], shX + shape[i]["x"] * s, shY + shape[i]["y"] * s)
EndIf
If Text.IsSubText("rect|ell|tri|text", shape[i]["func"]) And shape[i]["angle"] <> 0 Then
Shapes.Rotate(shape[i]["obj"], shape[i]["angle"])
EndIf
shape[i]["rx"] = shape[i]["x"]
shape[i]["ry"] = shape[i]["y"]
EndFor
shAngle = 0
i = Stack.PopValue("local")
EndSub
Sub Shapes_CalcRotatePos
' Shapes | Calculate position for rotated shape
' param["x"], param["y"] - position of a shape
' param["width"], param["height"] - size of a shape
' param ["cx"], param["cy"] - center of rotation
' param ["angle"] - rotate angle
' return x, y - rotated position of a shape
_cx = param["x"] + param["width"] / 2
_cy = param["y"] + param["height"] / 2
x = _cx - param["cx"]
y = _cy - param["cy"]
Math_CartesianToPolar()
a = a + param["angle"]
x = r * Math.Cos(a * Math.Pi / 180)
y = r * Math.Sin(a * Math.Pi / 180)
_cx = x + param["cx"]
_cy = y + param["cy"]
x = _cx - param["width"] / 2
y = _cy - param["height"] / 2
EndSub
Sub Shapes_CalcWidthAndHeight
' Shapes | Calculate total width and height of shapes
' param iMin, iMax - shape indices to add
' return shWidth, shHeight - total size of shapes
For i = iMin To iMax
If shape[i]["func"] = "tri" Or shape[i]["func"] = "line" Then
xmin = shape[i]["x1"]
xmax = shape[i]["x1"]
ymin = shape[i]["y1"]
ymax = shape[i]["y1"]
If shape[i]["x2"] < xmin Then
xmin = shape[i]["x2"]
EndIf
If xmax < shape[i]["x2"] Then
xmax = shape[i]["x2"]
EndIf
If shape[i]["y2"] < ymin Then
ymin = shape[i]["y2"]
EndIf
If ymax < shape[i]["y2"] Then
ymax = shape[i]["y2"]
EndIf
If shape[i]["func"] = "tri" Then
If shape[i]["x3"] < xmin Then
xmin = shape[i]["x3"]
EndIf
If xmax < shape[i]["x3"] Then
xmax = shape[i]["x3"]
EndIf
If shape[i]["y3"] < ymin Then
ymin = shape[i]["y3"]
EndIf
If ymax < shape[i]["y3"] Then
ymax = shape[i]["y3"]
EndIf
EndIf
shape[i]["width"] = xmax - xmin
shape[i]["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shape[i]["x"] + shape[i]["width"]
shHeight = shape[i]["y"] + shape[i]["height"]
Else
If shWidth < shape[i]["x"] + shape[i]["width"] Then
shWidth = shape[i]["x"] + shape[i]["width"]
EndIf
If shHeight < shape[i]["y"] + shape[i]["height"] Then
shHeight = shape[i]["y"] + shape[i]["height"]
EndIf
EndIf
EndFor
EndSub
Sub Shapes_Move
' Shapes | Move shapes
' param iMin, iMax - shape indices to add
' param shape - array of shapes
' param scale - to zoom
' param x, y - position to move
' return shX, shY - new position of shapes
Stack.PushValue("local", i)
s = scale
shX = x
shY = y
For i = iMin To iMax
_x = shape[i]["rx"]
_y = shape[i]["ry"]
Shapes.Move(shape[i]["obj"], shX + _x * s, shY + _y * s)
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Remove
' Shapes | Remove shapes
' param iMin, iMax - shapes indices to remove
' param shape - array of shapes
Stack.PushValue("local", i)
For i = iMin To iMax
Shapes.Remove(shape[i]["obj"])
EndFor
i = Stack.PopValue("local")
EndSub
Sub Shapes_Rotate
' Shapes | Rotate shapes
' param iMin, iMax - shapes indices to rotate
' param shape - array of shapes
' param scale - to zoom
' param angle - to rotate
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
s = scale
param["angle"] = angle
param["cx"] = shWidth / 2
param["cy"] = shHeight / 2
For i = iMin To iMax
param["x"] = shape[i]["x"]
param["y"] = shape[i]["y"]
param["width"] = shape[i]["width"]
param["height"] = shape[i]["height"]
Shapes_CalcRotatePos()
If silverlight And Text.IsSubText("tri|line", shape[i]["func"]) Then
alpha = Math.GetRadians(angle + shape[i]["angle"])
SB_RotateWorkAround()
EndIf
shape[i]["rx"] = x
shape[i]["ry"] = y
Shapes.Move(shape[i]["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shape[i]["obj"], angle + shape[i]["angle"])
EndFor
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub