Microsoft Small Basic

Program Listing: FGC959-0
' Windmill
' Version 0.2
' Copyright © 2015-2017 Nonki Takahashi. The MIT License.
' 2015-02-05 2:13:11 Shapes generated by Shapes 2.1b.
' Program ID FGC959-0
'
GraphicsWindow.Title = "Windmill 0.2"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
GraphicsWindow.BackgroundColor = "SkyBlue"
GraphicsWindow.BrushColor = "#CC9933"
GraphicsWindow.FillRectangle(0, 380, 598, 58)
' initialize shapes
Shapes_Init()
' add shapes
scale = 1
angle = 0
iMin = 1
iMax = 32
Shapes_Add()
iMin = 8
angle = 1
cx = 132
cy = 131
While "True"
Shapes_Rotate()
angle = angle + 1
If angle <= 360 Then
angle = angle - 360
EndIf
Program.Delay(50)
EndWhile

Sub Shapes_Init
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 169 ' x offset
shY = 100 ' y offset
shape = ""
shape[1] = "func=rect;x=70;y=152;width=121;height=129;bc=#FBFAF3;pc=#000000;pw=2;"
shape[2] = "func=tri;x=69;y=83;x1=62;y1=0;x2=0;y2=71;x3=124;y3=71;bc=#1E3E62;pc=#000000;pw=2;"
shape[3] = "func=rect;x=79;y=156;width=13;height=13;bc=#26230D;pc=#000000;pw=2;"
shape[4] = "func=rect;x=105;y=157;width=13;height=13;bc=#26230D;pc=#000000;pw=2;"
shape[5] = "func=rect;x=143;y=158;width=13;height=13;bc=#26230D;pc=#000000;pw=2;"
shape[6] = "func=rect;x=170;y=157;width=13;height=13;bc=#26230D;pc=#000000;pw=2;"
shape[7] = "func=rect;x=118;y=231;width=28;height=46;bc=#66421F;pc=#000000;pw=2;"
shape[8] = "func=ell;x=125;y=124;width=14;height=14;bc=#26230D;pc=#26230D;pw=2;"
shape[9] = "func=rect;x=111;y=0;width=41;height=101;bc=#99FBFAF3;pc=#26230D;pw=2;"
shape[10] = "func=line;x=131;y=1;x1=0;y1=0;x2=0;y2=128;pc=#26230D;pw=2;"
shape[11] = "func=line;x=111;y=20;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[12] = "func=line;x=111;y=40;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[13] = "func=line;x=111;y=60;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[14] = "func=line;x=111;y=80;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[15] = "func=rect;x=111;y=155;width=41;height=101;bc=#99FBFAF3;pc=#26230D;pw=2;"
shape[16] = "func=line;x=131;y=127;x1=0;y1=0;x2=0;y2=129;pc=#26230D;pw=2;"
shape[17] = "func=line;x=111;y=175;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[18] = "func=line;x=111;y=195;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[19] = "func=line;x=111;y=215;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[20] = "func=line;x=111;y=235;x1=0;y1=0;x2=41;y2=0;pc=#26230D;pw=2;"
shape[21] = "func=rect;x=0;y=111;width=101;height=41;bc=#99FBFAF3;pc=#26230D;pw=2;"
shape[22] = "func=line;x=0;y=131;x1=0;y1=0;x2=131;y2=0;pc=#26230D;pw=2;"
shape[23] = "func=line;x=20;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[24] = "func=line;x=40;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[25] = "func=line;x=60;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[26] = "func=line;x=80;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[27] = "func=rect;x=162;y=111;width=101;height=41;bc=#99FBFAF3;pc=#26230D;pw=2;"
shape[28] = "func=line;x=131;y=131;x1=0;y1=0;x2=131;y2=0;pc=#26230D;pw=2;"
shape[29] = "func=line;x=182;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[30] = "func=line;x=202;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[31] = "func=line;x=222;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
shape[32] = "func=line;x=242;y=111;x1=0;y1=0;x2=0;y2=41;pc=#26230D;pw=2;"
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 And y = 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_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)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Shapes_CalcWidthAndHeight()
s = scale
For i = iMin To iMax
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
shAngle = 0
y = Stack.PopValue("local")
x = Stack.PopValue("local")
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
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_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
shp = shape[i]
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"], 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
shp = shape[i]
Shapes.Remove(shp["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 cx, cy - rotation center
' 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
If cx <> "" Then
param["cx"] = cx
Else
param["cx"] = shWidth / 2
EndIf
If cx <> "" Then
param["cy"] = cy
Else
param["cy"] = shHeight / 2
EndIf
For i = iMin To iMax
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
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub