Microsoft Small Basic

Program Listing:
Embed this in your website
' Generated by GetShapes.VBA Version 0.2
' Copyright © 2015 Nonki Takahashi. The MIT License.
' Last update 2015-10-31
'
bg = "Black"
GraphicsWindow.BackgroundColor = bg
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
' initialize shapes
Shapes_Init()
' add shapes
scale = 1
angle = 0
iMin = 1
iMax = 15
Shapes_Add()
Sub Shapes_Init
  ' 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] = "func=rect;x=283;y=88;width=22;height=41;pw=0;bc=#993300;name=Rectangle 3;"
  shape[2] = "func=ell;x=125;y=130;width=81;height=244;pw=0;bc=#FF6600;name=Oval 16;"
  shape[3] = "func=ell;x=387;y=130;width=81;height=244;pw=0;bc=#FF6600;name=Oval 41;"
  shape[4] = "func=ell;x=153;y=114;width=106;height=278;pw=0;bc=#FF6600;name=Oval 42;"
  shape[5] = "func=ell;x=328;y=114;width=106;height=278;pw=0;bc=#FF6600;name=Oval 43;"
  shape[6] = "func=ell;x=193;y=99;width=142;height=306;pw=0;bc=#FF6600;name=Oval 44;"
  shape[7] = "func=ell;x=253;y=100;width=142;height=306;pw=0;bc=#FF6600;name=Oval 19;"
  shape[8] = "func=tri;x=199;y=204;x1=38;y1=0;x2=0;y2=49;x3=77;y3=49;angle=180;pw=0;bc=#000000;name=Isosceles Triangle 27;"
  shape[9] = "func=tri;x=313;y=201;x1=39;y1=0;x2=0;y2=49;x3=78;y3=49;angle=180;pw=0;bc=#000000;name=Isosceles Triangle 46;"
  shape[10] = "func=tri;x=200;y=229;x1=93;y1=0;x2=0;y2=116;x3=186;y3=116;pw=0;bc=#000000;name=Isosceles Triangle 1;"
  shape[11] = "func=rect;x=233;y=262;width=22;height=88;pw=0;bc=#FF6600;name=Rectangle 45;"
  shape[12] = "func=rect;x=331;y=262;width=22;height=88;pw=0;bc=#FF6600;name=Rectangle 50;"
  shape[13] = "func=rect;x=283;y=223;width=22;height=124;pw=0;bc=#FF6600;name=Rectangle 51;"
  shape[14] = "func=text;text=Happy Halloween;fn=Trebuchet MS;fs=60;fb=True;fi=False;x=60;y=14;width=506;height=79;pw=0;bc=#FFFFFF;name=TextBox 49;"
  shape[15] = "func=text;text=2015;fn=Trebuchet MS;fs=88;fb=False;fi=False;x=340;y=72;width=200;height=113;pw=0;bc=#80FFFFFF;name=TextBox 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 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
Copyright (c) Microsoft Corporation. All rights reserved.