Microsoft Small Basic

Program Listing: BXZ994
' Registers
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-08-30

GraphicsWindow.Title = "Registers"
SB_Workaround()
Init()
n = Array.GetItemCount(register)
index = Array.GetAllIndices(register)
shY = 90
shX = 80
angle = 0
iMin = 1
i = 1
While i <= n
Stack.PushValue("local", i)
If Array.GetItemCount(register[index[i]]) = 4 Then
InitRegister()
Else
InitMetalFilmRegister()
EndIf
iMax = Array.GetItemCount(shape)
Shapes_Add()
i = Stack.PopValue("local")
shX = shX + 100
If gw - 100 < shX Then
shY = shY + 120
shX = 80
EndIf
i = i + 1
EndWhile

Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
line = "Silver"
base = "Tan"
metal = "MediumTurquoise"
digit[0] = "Black"
digit[1] = "SaddleBrown"
digit[2] = "Crimson"
digit[3] = "OrangeRed"
digit[4] = "Yellow"
digit[5] = "Green"
digit[6] = "Blue"
digit[7] = "DarkViolet"
digit[8] = "Gray"
digit[9] = "White"
For i = 0 To 9
power[i] = digit[i]
EndFor
power[-1] = "DarkGoldenrod"
power[-2] = "Silver"
error["±1"] = digit[1]
error["±2"] = digit[2]
error["±5"] = power[-1]
error["±10"] = power[-2]
error["±20"] = "Transparent"
register["1MΩ"] = "1=1;2=0;3=5;4=±5;"
register["80kΩ"] = "1=8;2=0;3=0;4=2;5=±1;"
register["10kΩ"] = "1=1;2=0;3=3;4=±5;"
register["4.7kΩ"] = "1=4;2=7;3=2;4=±5;"
register["1kΩ"] = "1=1;2=0;3=2;4=±5;"
register["220Ω"] = "1=2;2=2;3=1;4=±5;"
register["200Ω"] = "1=2;2=0;3=1;4=±5;"
EndSub

Sub InitRegister
' Initialize carbon film register
scale = 4
reg = register[index[i]]
c[1] = digit[reg[1]]
c[2] = digit[reg[2]]
c[3] = power[reg[3]]
c[4] = error[reg[4]]
shape = ""
shape[1] = "func=line;x=0;y=0;x1=1.5;y1=3;x2=1.4;y2=13.5;pc="+line+";pw=0.5;"
shape[2] = "func=line;x=0;y=0;x1=9.5;y1=3;x2=9.6;y2=13.5;pc="+line+";pw=0.5;"
shape[3] = "func=line;x=0;y=0;x1=1.5;y1=3;x2=2;y2=2.5;pc="+line+";pw=0.5;"
shape[4] = "func=line;x=0;y=0;x1=9.5;y1=3;x2=9;y2=2.5;pc="+line+";pw=0.5;"
shape[5] = "func=ell;x=2;y=1;width=2;height=3;bc="+base+";"
shape[6] = "func=rect;x=2;y=1.5;width=7;height=2;bc="+base+";"
shape[7] = "func=ell;x=7;y=1;width=2;height=3;bc="+base+";"
shape[8] = "func=rect;x=3;y=1.5;width=0.7;height=2;bc="+c[1]+";"
shape[9] = "func=rect;x=4.4;y=1.5;width=0.7;height=2;bc="+c[2]+";"
shape[10] = "func=rect;x=5.8;y=1.5;width=0.7;height=2;bc="+c[3]+";"
shape[11] = "func=rect;x=7.5;y=1.5;width=0.7;height=2;bc="+c[4]+";"
shape[12] = "func=text;x=1.5;y=15;text="+index[i]+";fn=Arial;fs=3;bc=Black;"
EndSub

Sub InitMetalFilmRegister
' Initialize metal film register
scale = 4
reg = register[index[i]]
c[1] = digit[reg[1]]
c[2] = digit[reg[2]]
c[3] = digit[reg[3]]
c[4] = power[reg[4]]
c[5] = error[reg[5]]
shape = ""
shape[1] = "func=line;x=0;y=0;x1=1.5;y1=3;x2=1.4;y2=13.5;pc="+line+";pw=0.5;"
shape[2] = "func=line;x=0;y=0;x1=9.5;y1=3;x2=9.6;y2=13.5;pc="+line+";pw=0.5;"
shape[3] = "func=line;x=0;y=0;x1=1.5;y1=3;x2=2;y2=2.5;pc="+line+";pw=0.5;"
shape[4] = "func=line;x=0;y=0;x1=9.5;y1=3;x2=9;y2=2.5;pc="+line+";pw=0.5;"
shape[5] = "func=ell;x=2;y=1;width=2;height=3;bc="+metal+";"
shape[6] = "func=rect;x=2;y=1.5;width=7;height=2;bc="+metal+";"
shape[7] = "func=ell;x=7;y=1;width=2;height=3;bc="+metal+";"
shape[8] = "func=rect;x=3;y=1.5;width=0.5;height=2;bc="+c[1]+";"
shape[9] = "func=rect;x=4;y=1.5;width=0.5;height=2;bc="+c[2]+";"
shape[10] = "func=rect;x=5;y=1.5;width=0.5;height=2;bc="+c[3]+";"
shape[11] = "func=rect;x=6;y=1.5;width=0.5;height=2;bc="+c[4]+";"
shape[12] = "func=rect;x=7.5;y=1.5;width=0.5;height=2;bc="+c[5]+";"
shape[13] = "func=text;x=1.5;y=15;text="+index[i]+";fn=Arial;fs=3;bc=Black;"
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_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
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
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