# Microsoft Small Basic

Program Listing: GTV460-0
' This program (GTV460-1) is a part of "Shapes 1.1" (TLW744).
buf = buf + CRLF
buf = buf + "Sub Shapes_CalcWidthAndHeight" + CRLF
buf = buf + " ' Shapes | Calculate total width and height of shapes for rotation" + CRLF
buf = buf + " ' param nShapes - number of shapes" + CRLF
buf = buf + " ' return shWidth, shHeight - total size of shapes" + CRLF
buf = buf + " For i = 1 To nShapes" + CRLF
buf = buf + " If shape[i][" + WQ + "func" + WQ + "] = " + WQ + "tri" + WQ + " Or shape[i][" + WQ + "func" + WQ + "] = " + WQ + "line" + WQ + " Then" + CRLF
buf = buf + " xmin = shape[i][" + WQ + "x1" + WQ + "]" + CRLF
buf = buf + " xmax = shape[i][" + WQ + "x1" + WQ + "]" + CRLF
buf = buf + " ymin = shape[i][" + WQ + "y1" + WQ + "]" + CRLF
buf = buf + " ymax = shape[i][" + WQ + "y1" + WQ + "]" + CRLF
buf = buf + " If shape[i][" + WQ + "x2" + WQ + "] < xmin Then" + CRLF
buf = buf + " xmin = shape[i][" + WQ + "x2" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If xmax < shape[i][" + WQ + "x2" + WQ + "] Then" + CRLF
buf = buf + " xmax = shape[i][" + WQ + "x2" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If shape[i][" + WQ + "y2" + WQ + "] < ymin Then" + CRLF
buf = buf + " ymin = shape[i][" + WQ + "y2" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If ymax < shape[i][" + WQ + "y2" + WQ + "] Then" + CRLF
buf = buf + " ymax = shape[i][" + WQ + "y2" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If shape[i][" + WQ + "func" + WQ + "] = " + WQ + "tri" + WQ + " Then" + CRLF
buf = buf + " If shape[i][" + WQ + "x3" + WQ + "] < xmin Then" + CRLF
buf = buf + " xmin = shape[i][" + WQ + "x3" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If xmax < shape[i][" + WQ + "x3" + WQ + "] Then" + CRLF
buf = buf + " xmax = shape[i][" + WQ + "x3" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If shape[i][" + WQ + "y3" + WQ + "] < ymin Then" + CRLF
buf = buf + " ymin = shape[i][" + WQ + "y3" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If ymax < shape[i][" + WQ + "y3" + WQ + "] Then" + CRLF
buf = buf + " ymax = shape[i][" + WQ + "y3" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " shape[i][" + WQ + "width" + WQ + "] = xmax - xmin" + CRLF
buf = buf + " shape[i][" + WQ + "height" + WQ + "] = ymax - ymin" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If i = 1 Then" + CRLF
buf = buf + " shWidth = shape[i][" + WQ + "x" + WQ + "] + shape[i][" + WQ + "width" + WQ + "]" + CRLF
buf = buf + " shHeight = shape[i][" + WQ + "y" + WQ + "] + shape[i][" + WQ + "height" + WQ + "]" + CRLF
buf = buf + " Else" + CRLF
buf = buf + " If shWidth < shape[i][" + WQ + "x" + WQ + "] + shape[i][" + WQ + "width" + WQ + "] Then" + CRLF
buf = buf + " shWidth = shape[i][" + WQ + "x" + WQ + "] + shape[i][" + WQ + "width" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If shHeight < shape[i][" + WQ + "y" + WQ + "] + shape[i][" + WQ + "height" + WQ + "] Then" + CRLF
buf = buf + " shHeight = shape[i][" + WQ + "y" + WQ + "] + shape[i][" + WQ + "height" + WQ + "]" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " EndFor" + CRLF
buf = buf + "EndSub" + CRLF
buf = buf + CRLF
buf = buf + "Sub Shapes_CalcRotatePos" + CRLF
buf = buf + " ' Shapes | Calculate position for rotated shape" + CRLF
buf = buf + " ' param[" + WQ + "x" + WQ + "], param[" + WQ + "y" + WQ + "] - position of a shape" + CRLF
buf = buf + " ' param[" + WQ + "width" + WQ + "], param[" + WQ + "height" + WQ + "] - size of a shape" + CRLF
buf = buf + " ' param[" + WQ + "cx" + WQ + "], param[" + WQ + "cy" + WQ + "] - center of shapes" + CRLF
buf = buf + " ' param[" + WQ + "angle" + WQ + "] - rotate angle" + CRLF
buf = buf + " ' return x, y - rotated position of a shape" + CRLF
buf = buf + " _cx = param[" + WQ + "x" + WQ + "] + param[" + WQ + "width" + WQ + "] / 2" + CRLF
buf = buf + " _cy = param[" + WQ + "y" + WQ + "] + param[" + WQ + "height" + WQ + "] / 2" + CRLF
buf = buf + " x = _cx - param[" + WQ + "cx" + WQ + "]" + CRLF
buf = buf + " y = _cy - param[" + WQ + "cy" + WQ + "]" + CRLF
buf = buf + " Math_CartesianToPolar()" + CRLF
buf = buf + " a = a + param[" + WQ + "angle" + WQ + "]" + CRLF
buf = buf + " x = r * Math.Cos(a * Math.Pi / 180)" + CRLF
buf = buf + " y = r * Math.Sin(a * Math.Pi / 180)" + CRLF
buf = buf + " _cx = x + param[" + WQ + "cx" + WQ + "]" + CRLF
buf = buf + " _cy = y + param[" + WQ + "cy" + WQ + "]" + CRLF
buf = buf + " x = _cx - param[" + WQ + "width" + WQ + "] / 2" + CRLF
buf = buf + " y = _cy - param[" + WQ + "height" + WQ + "] / 2" + CRLF
buf = buf + "EndSub" + CRLF
buf = buf + CRLF
buf = buf + "Sub Shapes_Move" + CRLF
buf = buf + " ' Shapes | Move shapes" + CRLF
buf = buf + " ' param shape - array of shapes" + CRLF
buf = buf + " ' param scale - to zoom" + CRLF
buf = buf + " ' param x, y - position to move" + CRLF
buf = buf + " ' return shX, shY - new position of shapes" + CRLF
buf = buf + " Stack.PushValue(" + WQ + "local" + WQ + ", i)" + CRLF
buf = buf + " dx = x - shX" + CRLF
buf = buf + " dy = y - shY" + CRLF
buf = buf + " shX = x" + CRLF
buf = buf + " shY = y" + CRLF
buf = buf + " For i = 1 To nShapes" + CRLF
buf = buf + " _x = shape[i][" + WQ + "rx" + WQ + "] + dx" + CRLF
buf = buf + " _y = shape[i][" + WQ + "ry" + WQ + "] + dy" + CRLF
buf = buf + " Shapes.Move(shape[i][" + WQ + "obj" + WQ + "], shX + _x * s, shY + _y * s)" + CRLF
buf = buf + " EndFor" + CRLF
buf = buf + " i = Stack.PopValue(" + WQ + "local" + WQ + ")" + CRLF
buf = buf + "EndSub" + CRLF
buf = buf + CRLF
buf = buf + "Sub Shapes_Remove" + CRLF
buf = buf + " ' Shapes | Remove shapes" + CRLF
buf = buf + " ' param shape - array of shapes" + CRLF
buf = buf + " Stack.PushValue(" + WQ + "local" + WQ + ", i)" + CRLF
buf = buf + " For i = 1 To nShapes" + CRLF
buf = buf + " Shapes.Remove(shape[i][" + WQ + "obj" + WQ + "])" + CRLF
buf = buf + " EndFor" + CRLF
buf = buf + " i = Stack.PopValue(" + WQ + "local" + WQ + ")" + CRLF
buf = buf + "EndSub" + CRLF
buf = buf + CRLF
buf = buf + "Sub Shapes_Rotate" + CRLF
buf = buf + " ' Shapes | Rotate shapes" + CRLF
buf = buf + " ' param shape - array of shapes" + CRLF
buf = buf + " ' param scale - to zoom" + CRLF
buf = buf + " ' param angle - to rotate" + CRLF
buf = buf + " Stack.PushValue(" + WQ + "local" + WQ + ", i)" + CRLF
buf = buf + " Stack.PushValue(" + WQ + "local" + WQ + ", x)" + CRLF
buf = buf + " Stack.PushValue(" + WQ + "local" + WQ + ", y)" + CRLF
buf = buf + " s = scale" + CRLF
buf = buf + " param[" + WQ + "angle" + WQ + "] = angle" + CRLF
buf = buf + " param[" + WQ + "cx" + WQ + "] = shWidth / 2" + CRLF
buf = buf + " param[" + WQ + "cy" + WQ + "] = shHeight / 2" + CRLF
buf = buf + " For i = 1 To nShapes" + CRLF
buf = buf + " param[" + WQ + "x" + WQ + "] = shape[i][" + WQ + "x" + WQ + "]" + CRLF
buf = buf + " param[" + WQ + "y" + WQ + "] = shape[i][" + WQ + "y" + WQ + "]" + CRLF
buf = buf + " param[" + WQ + "width" + WQ + "] = shape[i][" + WQ + "width" + WQ + "]" + CRLF
buf = buf + " param[" + WQ + "height" + WQ + "] = shape[i][" + WQ + "height" + WQ + "]" + CRLF
buf = buf + " Shapes_CalcRotatePos()" + CRLF
buf = buf + " shape[i][" + WQ + "rx" + WQ + "] = x" + CRLF
buf = buf + " shape[i][" + WQ + "ry" + WQ + "] = y" + CRLF
buf = buf + " Shapes.Move(shape[i][" + WQ + "obj" + WQ + "], shX + x * s, shY + y * s)" + CRLF
buf = buf + " Shapes.Rotate(shape[i][" + WQ + "obj" + WQ + "], angle + shape[i][" + WQ + "angle" + WQ + "])" + CRLF
buf = buf + " EndFor" + CRLF
buf = buf + " y = Stack.PopValue(" + WQ + "local" + WQ + ")" + CRLF
buf = buf + " x = Stack.PopValue(" + WQ + "local" + WQ + ")" + CRLF
buf = buf + " i = Stack.PopValue(" + WQ + "local" + WQ + ")" + CRLF
buf = buf + "EndSub" + CRLF
buf = buf + CRLF
buf = buf + "Sub Math_CartesianToPolar" + CRLF
buf = buf + " ' Math | convert cartesian coodinate to polar coordinate" + CRLF
buf = buf + " ' param x, y - cartesian coordinate" + CRLF
buf = buf + " ' return r, a - polar coordinate" + CRLF
buf = buf + " r = Math.SquareRoot(x * x + y * y)" + CRLF
buf = buf + " If x = 0 And y > 0 Then" + CRLF
buf = buf + " a = 90 ' [degree]" + CRLF
buf = buf + " ElseIf x = 0 And y < 0 Then" + CRLF
buf = buf + " a = -90" + CRLF
buf = buf + " Else" + CRLF
buf = buf + " a = Math.ArcTan(y / x) * 180 / Math.Pi" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + " If x < 0 Then" + CRLF
buf = buf + " a = a + 180" + CRLF
buf = buf + " ElseIf x > 0 And y < 0 Then" + CRLF
buf = buf + " a = a + 360" + CRLF
buf = buf + " EndIf" + CRLF
buf = buf + "EndSub" + CRLF