Microsoft Small Basic

Program Listing: PJX549-0
' Carousel
' Version 0.1
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Program ID PJX549-0
' 2017-10-04 11:34:15 Shapes generated by Shapes 2.3b.

GraphicsWindow.Title = "Carousel 0.1"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
' initialize shapes
Shapes_Init()
' add shapes
name = "Horse"
Group_Add()
x = shX
y = shY
i = nGroup
r = 200
For angle = 6 To 360 Step 6
Program.Delay(500)
_angle = Math.GetRadians(angle)
x = shX - r * Math.Sin(_angle)
Group_Move()
Group_Flip()
EndFor

Sub Group_Add
' Group | add shapes to a group
' param name - group name
' param shX, shY, origin of shape array
' param shape[] - shape array
' param nGroup - number of group
' return nGroup - updated number of group
' return group - group array
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
nGroup = nGroup + 1
grp = ""
grp["name"] = name
grp["x"] = shX
grp["y"] = shY
grp["angle"] = 0
grp["dir"] = 1
Shapes_CalcWidthAndHeight()
grp["width"] = shWidth
grp["cx"] = shWidth / 2
grp["height"] = shHeight
s = 1
grp["scale"] = s
For i = 1 To Array.GetItemCount(shape)
shp = shape[i]
GraphicsWindow.PenWidth = shp["pw"] * s
If shp["pw"] > 0 Then
GraphicsWindow.PenColor = shp["pc"]
EndIf
If Text.IsSubText("rect|ell|tri|text", shp["func"]) Then
GraphicsWindow.BrushColor = shp["bc"]
EndIf
If shp["func"] = "rect" Then
shp["obj"] = Shapes.AddRectangle(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "ell" Then
shp["obj"] = Shapes.AddEllipse(shp["width"] * s, shp["height"] * s)
ElseIf shp["func"] = "tri" Then
shp["obj"] = Shapes.AddTriangle(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s, shp["x3"] * s, shp["y3"] * s)
ElseIf shp["func"] = "line" Then
shp["obj"] = Shapes.AddLine(shp["x1"] * s, shp["y1"] * s, shp["x2"] * s, shp["y2"] * s)
ElseIf shp["func"] = "text" Then
If silverlight Then
fs = Math.Floor(shp["fs"] * 0.9)
Else
fs = shp["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shp["fn"]
shp["obj"] = Shapes.AddText(shp["text"])
EndIf
x = shp["x"]
y = shp["y"]
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(shp["angle"])
SB_RotateWorkaround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
If Text.IsSubText("rect|ell|tri|text", shp["func"]) And (shp["angle"] <> 0) And (shp["angle"] <> "") Then
Shapes.Rotate(shp["obj"], shp["angle"])
EndIf
shape[i] = shp
EndFor
grp["shape"] = shape
group[nGroup] = grp
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub Group_Flip
' Group | Flip a group
' param group[i] - group to flip
' param angle - to flip
' return group[i] - flipped group
Stack.PushValue("local", angle)
grp = group[i]
gx = grp["x"]
gy = grp["y"]
shape = grp["shape"]
n = Array.GetItemCount(shape)
angle = Math.Remainder(angle, 360)
If angle < 0 Then
angle = angle + 360
EndIf
If (angle <= 90) Or (270 < angle) Then
sign = 1
Else
sign = -1
EndIf
If (grp["flip"] <= 90) Or (270 < grp["flip"]) Then
lastSign = 1
Else
lastSign = -1
EndIf
_a = Math.GetRadians(angle)
scaleX = Math.Cos(_a)
For j = 1 To n
shp = shape[j]
Shapes.Zoom(shp["obj"], Math.Abs(scaleX), 1)
cx = shp["x"] + shp["width"] / 2
fx = (cx - grp["cx"]) * scaleX + grp["cx"]
Shapes.Move(shp["obj"], fx - shp["width"] / 2 + gx, shp["y"] + gy)
If sign <> lastSign Then
If shp["angle"] <> 0 Then
shp["angle"] = -shp["angle"]
Shapes.Rotate(shp["obj"], shp["angle"])
EndIf
_x = Math.Floor((cx - shp["width"] / 2) * 100) / 100
shp["rx"] = _x
shp["x"] = _x
shape[j] = shp
EndIf
EndFor
grp["flip"] = angle
grp["shape"] = shape
group[i] = grp
angle = Stack.PopValue("local")
EndSub

Sub Group_Move
' Group | Move a group
' param group[i] - group to move
' param x, y - position to move
' return group[i] - updated group
grp = group[i]
s = grp["scale"]
grp["x"] = x
grp["y"] = y
shape = grp["shape"]
n = Array.GetItemCount(shape)
For j = 1 To n
shp = shape[j]
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
_x = shp["wx"]
_y = shp["wy"]
Else
_x = shp["rx"]
_y = shp["ry"]
EndIf
Shapes.Move(shp["obj"], grp["x"] + _x * s, grp["y"] + _y * s)
EndFor
grp["shape"] = shape
group[i] = grp
EndSub

Sub Group_Remove
' Group | Remove a group
' param group[i] - group to move
grp = group[i]
shape = grp["shape"]
n = Array.GetItemCount(shape)
Stack.PushValue("local", i)
For i = 1 To n
shp = shape[i]
Shapes.Remove(shp["obj"])
EndFor
i = Stack.PopValue("local")
group[i] = ""
EndSub

Sub Group_Rotate
' Group | Rotate a group
' param group[i] - group to move
' param cx, cy - rotation center (if given)
' param angle - to rotate
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Stack.PushValue("local", s)
Stack.PushValue("local", n)
param["angle"] = angle
If cx <> "" Then
param["cx"] = cx
Else
cx = "" ' to avoid syntax error
param["cx"] = shWidth / 2
EndIf
If cy <> "" Then
param["cy"] = cy
Else
cy = "" ' to avoid syntax error
param["cy"] = shHeight / 2
EndIf
grp = group[i]
s = grp["scale"]
shape = grp["shape"]
shX = grp["x"]
shY = grp["y"]
n = Array.GetItemCount(shape)
Stack.PushValue("local", i)
For i = 1 To n
shp = shape[i]
param["x"] = shp["x"]
param["y"] = shp["y"]
param["width"] = shp["width"]
param["height"] = shp["height"]
Shapes_CalcRotatePos()
shp["rx"] = x
shp["ry"] = y
If silverlight And Text.IsSubText("tri|line", shp["func"]) Then
alpha = Math.GetRadians(angle + shp["angle"])
SB_RotateWorkAround()
shp["wx"] = x
shp["wy"] = y
EndIf
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
Shapes.Rotate(shp["obj"], angle + shp["angle"])
shape[i] = shp
EndFor
i = Stack.PopValue("local")
grp["shape"] = shape
group[i] = grp
n = Stack.PopValue("local")
s = Stack.PopValue("local")
y = Stack.PopValue("local")
x = Stack.PopValue("local")
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
ElseIf x = 0 Then
a = 0
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 shp - current shape
' param x, y - original coordinate
' param alpha - angle [radian]
' returns x, y - workaround coordinate
If shp["func"] = "tri" Then
x1 = -Math.Floor(shp["x3"] / 2)
y1 = -Math.Floor(shp["y3"] / 2)
ElseIf shp["func"] = "line" Then
x1 = -Math.Floor(Math.Abs(shp["x1"] - shp["x2"]) / 2)
y1 = -Math.Floor(Math.Abs(shp["y1"] - shp["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_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 shape[] - shape array
' return shWidth, shHeight - total size of shapes
For i = i To Array.GetItemCount(shape)
shp = shape[i]
If shp["func"] = "tri" Or shp["func"] = "line" Then
xmin = shp["x1"]
xmax = shp["x1"]
ymin = shp["y1"]
ymax = shp["y1"]
If shp["x2"] < xmin Then
xmin = shp["x2"]
EndIf
If xmax < shp["x2"] Then
xmax = shp["x2"]
EndIf
If shp["y2"] < ymin Then
ymin = shp["y2"]
EndIf
If ymax < shp["y2"] Then
ymax = shp["y2"]
EndIf
If shp["func"] = "tri" Then
If shp["x3"] < xmin Then
xmin = shp["x3"]
EndIf
If xmax < shp["x3"] Then
xmax = shp["x3"]
EndIf
If shp["y3"] < ymin Then
ymin = shp["y3"]
EndIf
If ymax < shp["y3"] Then
ymax = shp["y3"]
EndIf
EndIf
shp["width"] = xmax - xmin
shp["height"] = ymax - ymin
EndIf
If i = 1 Then
shWidth = shp["x"] + shp["width"]
shHeight = shp["y"] + shp["height"]
Else
If shWidth < shp["x"] + shp["width"] Then
shWidth = shp["x"] + shp["width"]
EndIf
If shHeight < shp["y"] + shp["height"] Then
shHeight = shp["y"] + shp["height"]
EndIf
EndIf
shape[i] = shp
EndFor
EndSub

Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 217 ' x offset
shY = 155 ' y offset
shape = ""
shape[1] = "func=rect;x=36;y=78;width=126;height=47;bc=#000000;pw=0;"
shape[2] = "func=rect;x=37;y=124;width=9;height=74;bc=#000000;pw=0;"
shape[3] = "func=rect;x=50;y=124;width=9;height=74;bc=#000000;pw=0;"
shape[4] = "func=rect;x=139;y=124;width=9;height=74;bc=#000000;pw=0;"
shape[5] = "func=rect;x=152;y=124;width=9;height=74;bc=#000000;pw=0;"
shape[6] = "func=rect;x=31;y=21;width=31;height=61;angle=348;bc=#000000;pw=0;"
shape[7] = "func=rect;x=0;y=16;width=55;height=27;bc=#000000;pw=0;"
shape[8] = "func=tri;x=59;y=12;x1=7;y1=0;x2=0;y2=24;x3=15;y3=24;angle=86;bc=#000000;pw=0;"
shape[9] = "func=tri;x=61;y=24;x1=7;y1=0;x2=0;y2=24;x3=15;y3=24;angle=86;bc=#000000;pw=0;"
shape[10] = "func=tri;x=64;y=35;x1=7;y1=0;x2=0;y2=24;x3=15;y3=24;angle=86;bc=#000000;pw=0;"
shape[11] = "func=tri;x=67;y=47;x1=7;y1=0;x2=0;y2=24;x3=15;y3=24;angle=86;bc=#000000;pw=0;"
shape[12] = "func=tri;x=69;y=59;x1=7;y1=0;x2=0;y2=24;x3=15;y3=24;angle=86;bc=#000000;pw=0;"
shape[13] = "func=ell;x=39;y=0;width=9;height=22;bc=#000000;pw=0;"
shape[14] = "func=ell;x=50;y=0;width=9;height=22;bc=#000000;pw=0;"
shape[15] = "func=ell;x=168;y=75;width=9;height=39;angle=321;bc=#000000;pw=0;"
shape[16] = "func=ell;x=36;y=22;width=9;height=9;bc=#FFFFFF;pw=0;"
EndSub