Microsoft Small Basic

Program Listing: HRX565-1
' Abstract and Concrete
' Version 0.3
' Copyright © 2016 Nonki Takahashi. The MIT License.
' Program ID HRX565-1

GraphicsWindow.Title = "Abstract and Concrete 0.3"

Init()

While "True"

' average value
GraphicsWindow.FontName = "Trebuchet MS"
GraphicsWindow.BrushColor = "Gray"
For x = 0 To 400 Step 100
shp[x] = Shapes.AddText(x)
Shapes.Move(shp[x], x + gap, 8 * gap)
EndFor
a = 300
b = 400
Average()
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 50
shp["title"] = Shapes.AddText("Average (Value)")
Shapes.Move(shp["title"], gap, 0)
GraphicsWindow.FontSize = 12
shp["a"] = Shapes.AddText("a = " + a)
Shapes.Move(shp["a"], gap, 2 * gap - 14)
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "Cyan"
shp["rectA"] = Shapes.AddRectangle(a, gap)
Shapes.Move(shp["rectA"], gap, 2 * gap)
Shapes.SetOpacity(shp["rectA"], 50)
GraphicsWindow.BrushColor = "Black"
shp["b"] = Shapes.AddText("b = " + b)
Shapes.Move(shp["b"], gap, 4 * gap - 14)
GraphicsWindow.BrushColor = "Cyan"
shp["rectB"] = Shapes.AddRectangle(b, gap)
Shapes.Move(shp["rectB"], gap, 4 * gap)
Shapes.SetOpacity(shp["rectB"], 50)
GraphicsWindow.BrushColor = "Black"
shp["c"] = Shapes.AddText("c = Average(a, b) = (a + b) / 2 = " + c)
Shapes.Move(shp["c"], gap, 6 * gap - 14)
GraphicsWindow.BrushColor = "Cyan"
shp["rectC"] = Shapes.AddRectangle(c, gap)
Shapes.Move(shp["rectC"], gap, 6 * gap)
Shapes.SetOpacity(shp["rectC"], 50)
Program.Delay(3000)

' intermediate value
Shapes.SetText(shp["title"], "Intermediate (Value)")
GraphicsWindow.BrushColor = "Black"
shp["txtK"] = Shapes.AddText("k = ")
Shapes.Move(shp["txtK"], 10 * gap, 6 * gap)
GraphicsWindow.BrushColor = "Cyan"
For k = 0 To 1 Step 0.25
Shapes.SetText(shp["txtK"], "k = " + k)
Intermediate()
Shapes.SetText(shp["c"], "c = Intermediate(a, b, k) = a * (1 - k) + b * k = " + c)
Shapes.Remove(shp["rectC"])
shp["rectC"] = Shapes.AddRectangle(c, gap)
Shapes.Move(shp["rectC"], gap, 6 * gap)
Shapes.SetOpacity(shp["rectC"], 50)
Program.Delay(2000)
EndFor
Program.Delay(2000)
RemoveShapes()

' average point
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 50
shp["title"] = Shapes.AddText("Average (Point)")
Shapes.Move(shp["title"], gap, 0)
GraphicsWindow.FontSize = 12
point = "x=100;y=100;"
name = "A"
color = "Black"
DrawPoint()
pointA = point
point = "x=300;y=200;"
name = "B"
DrawPoint()
pointB = point
AveragePoint()
point = pointC
name = "C"
color = "Red"
DrawPoint()
Program.Delay(3000)

' intermediate point
Shapes.SetText(shp["title"], "Intermediate (Point)")
GraphicsWindow.BrushColor = "Black"
shp["txtK"] = Shapes.AddText("k = ")
Shapes.Move(shp["txtK"], 2 * gap, 5 * gap)
For k = 0 To 1 Step 0.25
Shapes.SetText(shp["txtK"], "k = " + k)
IntermediatePoint()
Shapes.Remove(shp["C1"])
Shapes.Remove(shp["C2"])
Shapes.Remove(shp["C3"])
point = pointC
DrawPoint()
Program.Delay(2000)
EndFor
Program.Delay(2000)
RemoveShapes()

' average color
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.FontSize = 50
shp["title"] = Shapes.AddText("Average (Color)")
Shapes.Move(shp["title"], gap, 0)
GraphicsWindow.FontSize = 12
colorA = "#006699"
colorB = "#336633"
param = "color=" + colorA + ";x=50;y=100;width=100;height=100;caption=colorA;"
DrawColor()
param = "color=" + colorB + ";x=200;y=100;width=100;height=100;caption=colorB;"
DrawColor()
AverageColor()
param = "color=" + colorC + ";x=350;y=100;width=100;height=100;caption=colorC;"
DrawColor()
Program.Delay(3000)

' intermediate color
Shapes.SetText(shp["title"], "Intermediate (Color)")
GraphicsWindow.BrushColor = "Black"
shp["txtK"] = Shapes.AddText("k = ")
Shapes.Move(shp["txtK"], 7 * gap, 5 * gap)
For k = 0 To 1 Step 0.25
Shapes.SetText(shp["txtK"], "k = " + k)
IntermediateColor()
Shapes.Remove(shp["rect_colorC"])
Shapes.Remove(shp["txt_colorC"])
param = "color=" + colorC + ";x=350;y=100;width=100;height=100;caption=colorC;"
DrawColor()
Program.Delay(2000)
EndFor
Program.Delay(2000)
RemoveShapes()

EndWhile

Sub Init
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
gap = 50
DrawGrid()
EndSub

Sub DrawGrid
GraphicsWindow.PenColor = "LightGray"
For y = 0 To gh - 1 Step gap
GraphicsWindow.DrawLine(0, y, gw - 1, y)
EndFor
For x = 0 To gw - 1 Step gap
GraphicsWindow.DrawLine(x, 0, x, gh - 1)
EndFor
EndSub

Sub DrawPoint
' param point - array for x and y coordinate of the point
' param name - point name
' param color - color for the point
x = Math.Floor(point["x"])
y = Math.Floor(point["y"])
GraphicsWindow.PenColor = color
GraphicsWindow.PenWidth = 2
shp[name + "1"] = Shapes.AddLine(-5, -5, 5, 5)
Shapes.Move(shp[name + "1"], x, y)
shp[name + "2"] = Shapes.AddLine(-5, 5, 5, -5)
Shapes.Move(shp[name + "2"], x, y)
GraphicsWindow.BrushColor = color
shp[name + "3"] = Shapes.AddText(name + "(" + x + "," + y + ")")
Shapes.Move(shp[name + "3"], x, y + 10)
EndSub

Sub DrawColor
' param["color"] - color of the rectangle
' param["x"], param["y"] - left top corner of the rectangle
' param["width"], param["height"] - size of the rectangle
' param["caption"] - caption of the color
GraphicsWindow.BrushColor = param["color"]
GraphicsWindow.PenWidth = 0
shp["rect_" + param["caption"]] = Shapes.AddRectangle(param["width"], param["height"])
Shapes.Move(shp["rect_" + param["caption"]], param["x"], param["y"])
GraphicsWindow.BrushColor = "Black"
shp["txt_" + param["caption"]] = Shapes.AddText(param["caption"] + "=" + param["color"])
Shapes.Move(shp["txt_" + param["caption"]], param["x"], param["y"] + param["height"])
EndSub

Sub RemoveShapes
n = Array.GetItemCount(shp)
index = Array.GetAllIndices(shp)
For i = 1 To n
Shapes.Remove(shp[index[i]])
shp[index[i]] = ""
EndFor
EndSub

Sub Average
' param a - the first value
' param b - the second value
' return c - the average of the given values
c = (a + b) / 2
EndSub

Sub AveragePoint
' param pointA - the first point
' param pointB - the second point
' return pointC - the average point of the given points
n = Array.GetItemCount(pointA)
index = Array.GetAllIndices(pointA)
For j = 1 To n
a = pointA[index[j]]
b = pointB[index[j]]
Average()
pointC[index[j]] = c
EndFor
EndSub

Sub AverageColor
' param colorA - the first color
' param colorB - the second color
' return colorC - the average of the given colors
For j = 1 To 3
hex = Text.GetSubText(colorA, j * 2, 2)
Hex2Dec()
a = dec
hex = Text.GetSubText(colorB, j * 2, 2)
Hex2Dec()
b = dec
Average()
rgb[j] = c
EndFor
colorC = GraphicsWindow.GetColorFromRGB(rgb[1], rgb[2], rgb[3])
EndSub

Sub Intermediate
' param a - the first value
' param b - the second value
' param k - ratio between a and b (0 ≦ k ≦ 1)
' return c - the intermediate value between a and b
c = a * (1 - k) + b * k
EndSub

Sub IntermediatePoint
' param pointA - the first point
' param pointB - the second point
' param k - ratio between a and b (0 ≦ k ≦ 1)
' return pointC - the intermidiate point between pointA and pointB
n = Array.GetItemCount(pointA)
index = Array.GetAllIndices(pointA)
For j = 1 To n
a = pointA[index[j]]
b = pointB[index[j]]
Intermediate()
pointC[index[j]] = c
EndFor
EndSub

Sub IntermediateColor
' param a - the first color
' param b - the second color
' param k - ratio between a and b (0 ≦ k ≦ 1)
' return c - the intermediate color between colorA and colorB
For j = 1 To 3
hex = Text.GetSubText(colorA, j * 2, 2)
Hex2Dec()
a = dec
hex = Text.GetSubText(colorB, j * 2, 2)
Hex2Dec()
b = dec
Intermediate()
rgb[j] = c
EndFor
colorC = GraphicsWindow.GetColorFromRGB(rgb[1], rgb[2], rgb[3])
EndSub

Sub Hex2Dec
' param hex
' return dec
len = Text.GetLength(hex)
dec = 0
For i = 1 To len
dec = dec * 16 + Text.GetIndexOf("123456789ABCDEF", Text.GetSubText(Text.ConvertToUpperCase(hex), i, 1))
EndFor
EndSub