Microsoft Small Basic

Program Listing: TWD491
' Flag of Korea
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-10-20

GraphicsWindow.Title = "Flag of Korea"
SB_Workaround()
Init()
' initialize shapes (1)
Shapes_Init_1()
name = "Korea1"
scale = gw / 360
Group_Add()
' initialize shapes (2)
Shapes_Init_2()
name = "Korea2"
Group_Add()
' rotate shapes
x = 72
y = 48
Math_CartesianToPolar()
angle = a - 90
i = nGroup
cx = 180
cy = 120
Group_Rotate()
' initialize shapes (3)
Shapes_Init_3()
name = "Korea3"
Group_Add()
' rotate shapes
x = 72
y = 48
Math_CartesianToPolar()
angle = 90 - a
i = nGroup
cx = 180
cy = 120
Group_Rotate()

Sub Init
gw = 598
gh = gw / 1.5
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
angle = 0
EndSub

Sub Group_Add
' Group | add shapes to a group
' param name - group name
' param shX, shY, origin of shape array
' param scale - scale of group
' 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 = scale
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|btn", shp["func"]) Then
GraphicsWindow.BrushColor = shp["bc"]
EndIf
If Text.IsSubText("text|btn", shp["func"]) Then
If silverlight Then
fs = Math.Floor(shp["fs"] * 0.9)
Else
fs = shp["fs"]
EndIf
GraphicsWindow.FontSize = fs * s
GraphicsWindow.FontName = shp["fn"]
If shp["fb"] = "False" Then
GraphicsWindow.FontBold = "False"
Else
GraphicsWindow.FontBold = "True"
EndIf
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
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
If shp["func"] = "btn" Then
shp["obj"] = Controls.AddButton(shp["caption"], shX + x * s, shY + y * s)
Else
Shapes.Move(shp["obj"], shX + x * s, shY + y * s)
EndIf
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_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_1
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 0 ' x offset
shY = 0 ' y offset
shape = ""
shape[1] = "pw=0;bc=#003478;func=rect;width=180;height=240;x=0;y=0;"
shape[2] = "pw=0;bc=#C60C30;func=rect;width=180;height=240;x=180;y=0;"
shape[3] = "pw=0;bc=#003478;func=tri;x1=180;y1=0;x2=0;y2=120;x3=360;y3=120;x=0;y=120;width=360;height=120;"
shape[4] = "pw=0;bc=#C60C30;func=tri;x1=180;y1=0;x2=0;y2=120;x3=360;y3=120;x=0;y=0;angle=180;width=360;height=120;"
shape[5] = "pw=0;bc=#FFF;func=rect;width=360;height=60;x=0;y=0;angle=180;"
shape[6] = "pw=0;bc=#FFF;func=rect;width=360;height=60;x=0;y=180;angle=180;"
shape[7] = "pw=0;bc=#FFF;func=rect;width=120;height=240;x=0;y=0;angle=180;"
shape[8] = "pw=0;bc=#FFF;func=rect;width=120;height=240;x=240;y=0;angle=180;"
shape[9] = "pw=30;pc=#FFF;bc=Transparent;func=ell;width=180;height=180;x=90;y=30;angle=180;"
EndSub

Sub Shapes_Init_2
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 0 ' x offset
shY = 0 ' y offset
shape = ""
shape[1] = "pw=0;bc=#C60C30;func=ell;width=60;height=60;x=150;y=60;angle=180;"
shape[2] = "pw=0;bc=#003478;func=ell;width=60;height=60;x=150;y=120;angle=180;"
shape[3] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=-10;angle=180;"
shape[4] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=5;angle=180;"
shape[5] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=20;angle=180;"
shape[6] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=210;angle=180;"
shape[7] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=225;angle=180;"
shape[8] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=240;angle=180;"
shape[9] = "pw=0;bc=#FFF;func=rect;width=5;height=45;x=177.5;y=207.5;angle=180;"
EndSub

Sub Shapes_Init_3
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 0 ' x offset
shY = 0 ' y offset
shape = ""
shape[1] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=-10;angle=180;"
shape[2] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=5;angle=180;"
shape[3] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=20;angle=180;"
shape[4] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=210;angle=180;"
shape[5] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=225;angle=180;"
shape[6] = "pw=0;bc=#000;func=rect;width=60;height=10;x=150;y=240;angle=180;"
shape[7] = "pw=0;bc=#FFF;func=rect;width=5;height=15;x=177.5;y=-12.5;angle=180;"
shape[8] = "pw=0;bc=#FFF;func=rect;width=5;height=15;x=177.5;y=17.5;angle=180;"
shape[9] = "pw=0;bc=#FFF;func=rect;width=5;height=15;x=177.5;y=222.5;angle=180;"
EndSub