Microsoft Small Basic

Program Listing: DHW172-5
' Traffic Game
' Version 0.7b
' Copyright © 2017 Nonki Takahashi. The MIT License.
' Last update 2017-05-24
' Program ID DHW172-5

' 2017-05-15 14:09:50 Shapes generated by Shapes 2.21b.

title = "Traffic Game 0.7b"
GraphicsWindow.Title = title
Init()
InitLane()
InitCars()
' initialize blow
Shapes_Init_Burst()
name = "Burst"
Group_Add()
Program.Delay(500)
i = nGroup
Group_Hide()
InitCaption()
' turn right
angle = 5
i = nGroup - 1
Group_Rotate()
' move right
x = 100
y = 220
For x = 110 To xLane[2] Step 10
Group_Move()
Scroll()
Program.Delay(100)
EndFor
' turn left
angle = 0
Group_Rotate()
' game start
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.KeyUp = OnKeyUp
speed["Car 1"] = 60
time = 60 ' [sec]
While 0 < time
If keyDown["Right"] Then
' turn right
angle = 5
Group_Rotate()
Group_GetPos()
x = x + 10
Group_Move()
right = "True"
ElseIf right Then
' turn left
angle = 0
Group_Rotate()
right = "False"
EndIf
If keyDown["Left"] Then
' turn left
angle = -5
Group_Rotate()
Group_GetPos()
x = x - 10
Group_Move()
left = "True"
ElseIf left Then
' turn right
angle = 0
Group_Rotate()
left = "False"
EndIf
If keyDown["Up"] Then
speed["Car 1"] = speed["Car 1"] + 10
EndIf
If keyDown["Down"] Then
speed["Car 1"] = speed["Car 1"] - 10
EndIf
Scroll()
Stack.PushValue("local", i)
Collision()
Program.Delay(100)
If 0 < burst Then
burst = burst - 100
If burst <= 0 Then
name = "Burst"
Group_GetIndexOf()
Group_Hide()
EndIf
EndIf
If 0 < ready Then
ready = ready - 100
If ready <= 3000 Then
Shapes.SetText(caption, "READY?")
EndIf
If ready <= 0 Then
Shapes.SetText(caption, "")
Shapes.HideShape(mask)
ShowInfo()
Timer.Interval = 1000
Timer.Tick = OnTick
EndIf
Else
score = score + Math.Floor(speed["Car 1"] / 5)
Shapes.SetText(display, "SCORE " + score)
EndIf
i = Stack.PopValue("local")
EndWhile
Timer.Pause()
Shapes.ShowShape(mask)
Shapes.SetText(caption, "GAME OVER")

Sub ChangeLane
' Change lane for car n
' param n - car number
' param group[i] - of car n
' return x - position x for the changed lane
candidate = "1=1;2=2;3=3;"
m = 5 - n
candidate[lane[m]] = ""
index = Array.GetAllIndices(candidate)
iLane = Math.GetRandomNumber(2)
l = candidate[index[iLane]]
lane[n] = l
x = xLane[l]
EndSub

Sub Collision
' Collision detect
name = "Car 1"
Group_GetIndexOf()
Group_GetPos()
xCar1 = x
yCar1 = y
Group_GetSize()
wCar1 = width
hCar1 = height
cxCar1 = x + width / 2
cyCar1 = y + height / 2
For car = 2 To 3
name = "Car " + car
Group_GetIndexOf()
Group_GetPos()
Group_GetSize()
cx = x + width / 2
cy = y + height / 2
dx = Math.Abs(x - xCar1)
dy = Math.Abs(y - yCar1)
lx = (width + wCar1) / 2
ly = (height + hCar1) / 2
If (dx < lx) And (dy < ly) Then
' collision
xCollision = (cx + cxCar1) / 2
yCollision = (cy + cyCar1) / 2
name = "Burst"
Group_GetIndexOf()
Group_GetSize()
x = xCollision - width / 2
y = yCollision - height / 2
Group_Move()
Group_Show()
burst = 500
score = score - 10 * Math.Abs(speed["Car 1"])
EndIf
EndFor
EndSub

Sub Init
' Initialization
Not = "False=True;True=False;"
right = "False"
left = "False"
xLane = "1=50;2=240;3=430;"
lane = "2=3;3=1;"
CRLF = Text.GetCharacter(13) + Text.GetCharacter(10)
GraphicsWindow.BackgroundColor = "Gray"
SB_Workaround()
gw = 598
gh = 428
GraphicsWindow.Width = gw
GraphicsWindow.Height = gh
EndSub

Sub InitCaption
' Initialize caption

' show caption
GraphicsWindow.PenWidth = 0
GraphicsWindow.BrushColor = "#99000000"
mask = Shapes.AddRectangle(gw, gh)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FontSize = 80
GraphicsWindow.FontName = "Trebuchet MS"
caption = Shapes.AddText("TRAFFIC" + CRLF + "GAME")
Shapes.Move(caption, 40, 30)
GraphicsWindow.FontSize = 20
display = Shapes.AddText("")
Shapes.Move(display, 40, 10)
ready = 5000
EndSub

Sub InitCars
' Initialize cars

' initialize truck shapes
Shapes_Init_Truck()
' add to a group
name = "Car 2"
Group_Add()
speed[name] = 40

' initialize crimson car shapes
Shapes_Init_CrimsonCar()
' add to a group
name = "Car 3"
Group_Add()
speed[name] = 40

' initialize blue car shapes
Shapes_Init_BlueCar()
' add to a group
name = "Car 1"
Group_Add()
speed[name] = 40
EndSub

Sub InitLane
' Initialize lane

' initialize lane shapes
Shapes_Init_Lane()
' add to a group
scale = 1
name = "Lane 1"
Group_Add()
' add to a group and move it
name = "Lane 2"
Group_Add()
i = nGroup
x = 0
y = 214
Group_Move()
' add to a group and move it
name = "Lane 3"
Group_Add()
i = nGroup
x = 0
y = 428
Group_Move()
EndSub

Sub OnKeyDown
' Key down event handler
key = GraphicsWindow.LastKey
keyDown[key] = "True"
EndSub

Sub OnKeyUp
' Key up event handler
key = GraphicsWindow.LastKey
keyDown[key] = ""
EndSub

Sub OnTick
' Timer event handler
time = time - 1
ShowInfo()
EndSub

Sub Scroll
' Scroll view
Stack.PushValue("local", n)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
Stack.PushValue("local", name)
Stack.PushValue("local", grp)
Stack.PushValue("local", i)
For n = 1 To 3
name = "Lane " + n
Group_GetIndexOf()
grp = group[i]
x = grp["x"]
y = grp["y"]
y = y + speed["Car 1"] / 4
If gh < y Then
y = y - 1.5 * gh
ElseIf y < -0.5 * gh Then
y = y + 1.5 * gh
EndIf
Group_Move()
EndFor
For n = 2 To 3
name = "Car " + n
Group_GetIndexOf()
grp = group[i]
x = grp["x"]
y = grp["y"]
y = y - (speed[name] - speed["Car 1"]) / 4
If gh < y Then
y = y - 1.5 * gh
ChangeLane()
ElseIf y < -0.5 * gh Then
y = y + 1.5 * gh
ChangeLane()
EndIf
Group_Move()
EndFor
i = Stack.PopValue("local")
grp = Stack.PopValue("local")
name = Stack.PopValue("local")
y = Stack.PopValue("local")
x = Stack.PopValue("local")
n = Stack.PopValue("local")
EndSub

Sub ShowInfo
GraphicsWindow.Title = title + " | " + speed["Car 1"] + " km/h | " + time + " sec"
EndSub

Sub Group_Add
' Group | add shapes to a group
' param name - group name
' param shX, shY, origin of shape array
' param shape[] - shape array
' param nGroup - number of group
' return nGroup - updated number of group
' return group - group array
Stack.PushValue("local", i)
Stack.PushValue("local", x)
Stack.PushValue("local", y)
nGroup = nGroup + 1
grp = ""
grp["name"] = name
grp["x"] = shX
grp["y"] = shY
grp["angle"] = 0
grp["dir"] = 1
Shapes_CalcWidthAndHeight()
grp["width"] = shWidth
grp["cx"] = shWidth / 2
grp["height"] = shHeight
s = 1
grp["scale"] = s
For i = 1 To Array.GetItemCount(shape)
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
grp["shape"] = shape
group[nGroup] = grp
y = Stack.PopValue("local")
x = Stack.PopValue("local")
i = Stack.PopValue("local")
EndSub

Sub Group_GetIndexOf
' Group | Get index of a group
' param name - a group name
' return i - index of the group
Stack.PushValue("local", _i)
i = 0 ' not found
For _i = 1 To nGroup
grp = group[_i]
If grp["name"] = name Then
i = _i
_i = nGroup ' exit For
EndIf
EndFor
_i = Stack.PopValue("local")
EndSub

Sub Group_GetPos
' Group | Get position of a group
' param group[i] - a group
' return x, y - position of the group
grp = group[i]
x = grp["x"]
y = grp["y"]
EndSub

Sub Group_GetSize
' Group | Get size of a group
' param group[i] - a group
' return width, height - size of the group
grp = group[i]
width = grp["width"]
height = grp["height"]
EndSub

Sub Group_Hide
' Group | Hide a group
' param group[i] - group to move
grp = group[i]
shape = grp["shape"]
n = Array.GetItemCount(shape)
For j = 1 To n
shp = shape[j]
Shapes.HideShape(shp["obj"])
EndFor
EndSub

Sub Group_Move
' Group | Move a group
' param group[i] - group to move
' param x, y - position to move
' return group[i] - updated group
Stack.PushValue("local", grp)
Stack.PushValue("local", s)
Stack.PushValue("local", shape)
Stack.PushValue("local", n)
Stack.PushValue("local", j)
Stack.PushValue("local", shp)
Stack.PushValue("local", _x)
Stack.PushValue("local", _y)
grp = group[i]
s = grp["scale"]
grp["x"] = x
grp["y"] = y
shape = grp["shape"]
n = Array.GetItemCount(shape)
For j = 1 To n
shp = shape[j]
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"], grp["x"] + _x * s, grp["y"] + _y * s)
EndFor
group[i] = grp
_y = Stack.PopValue("local")
_x = Stack.PopValue("local")
shp = Stack.PopValue("local")
j = Stack.PopValue("local")
n = Stack.PopValue("local")
shape = Stack.PopValue("local")
s = Stack.PopValue("local")
grp = Stack.PopValue("local")
EndSub

Sub Group_Remove
' Group | Remove a group
' param group[i] - group to move
group[i] = ""
EndSub

Sub Group_Rotate
' Group | Rotate a group
' param group[i] - group to move
' param cx, cy - rotation center (if given)
' param angle - to rotate
Stack.PushValue("local", x)
Stack.PushValue("local", y)
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
grp = group[i]
s = grp["scale"]
shape = grp["shape"]
shX = grp["x"]
shY = grp["y"]
n = Array.GetItemCount(shape)
Stack.PushValue("local", i)
For i = 1 To n
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
i = Stack.PopValue("local")
grp["shape"] = shape
group[i] = grp
y = Stack.PopValue("local")
x = Stack.PopValue("local")
EndSub

Sub Group_Show
' Group | Show a group
' param group[i] - group to move
grp = group[i]
shape = grp["shape"]
n = Array.GetItemCount(shape)
For j = 1 To n
shp = shape[j]
Shapes.ShowShape(shp["obj"])
EndFor
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_Init_BlueCar
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 100 ' x offset
shY = 220 ' y offset
shape = ""
shape[1] = "func=rect;x=0;y=29;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[2] = "func=rect;x=83;y=28;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[3] = "func=rect;x=1;y=141;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[4] = "func=rect;x=83;y=142;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[5] = "func=rect;x=15;y=0;width=91;height=199;bc=#5A95CD;pw=0;"
shape[6] = "func=rect;x=29;y=64;width=65;height=98;bc=#3B3B3B;pw=0;"
shape[7] = "func=rect;x=35;y=85;width=53;height=68;bc=#5A95CD;pw=0;"
shape[8] = "func=line;x=28;y=64;x1=0;y1=0;x2=8;y2=21;pc=#5A95CD;pw=2;"
shape[9] = "func=line;x=87;y=63;x1=6;y1=0;x2=0;y2=22;pc=#5A95CD;pw=2;"
shape[10] = "func=line;x=86;y=152;x1=0;y1=0;x2=7;y2=10;pc=#5A95CD;pw=2;"
shape[11] = "func=line;x=28;y=151;x1=8;y1=0;x2=0;y2=11;pc=#5A95CD;pw=2;"
shape[12] = "func=rect;x=20;y=192;width=22;height=6;bc=#800000;pw=0;"
shape[13] = "func=rect;x=78;y=192;width=22;height=6;bc=#800000;pw=0;"
shape[14] = "func=ell;x=24;y=7;width=19;height=25;bc=#66FFFFFF;pw=0;"
shape[15] = "func=ell;x=78;y=7;width=19;height=25;bc=#66FFFFFF;pw=0;"
EndSub

Sub Shapes_Init_Burst
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 100 ' x offset
shY = 160 ' y offset
shape = ""
shape[1] = "pw=0;bc=Red;func=ell;width=40;height=40;x=30;y=30;angle=0;"
shape[2] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=43.16;y=0.01;"
shape[3] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=65.26;y=8.06;angle=40;"
shape[4] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=77.02;y=28.43;angle=80;"
shape[5] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=72.94;y=51.6;angle=120;"
shape[6] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=54.92;y=66.72;angle=160;"
shape[7] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=31.39;y=66.72;angle=200;"
shape[8] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=13.37;y=51.6;angle=240;"
shape[9] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=9.29;y=28.43;angle=280;"
shape[10] = "pw=0;bc=Red;func=tri;x1=6.84;y1=0;x2=0;y2=31.2;x3=13.68;y3=31.2;x=21.05;y=8.06;angle=320;"
EndSub

Sub Shapes_Init_CrimsonCar
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 50 ' x offset
shY = 10 ' y offset
shape = ""
shape[1] = "func=rect;x=0;y=29;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[2] = "func=rect;x=83;y=28;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[3] = "func=rect;x=1;y=141;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[4] = "func=rect;x=83;y=142;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[5] = "func=rect;x=15;y=0;width=91;height=199;bc=#DC143C;pw=0;"
shape[6] = "func=rect;x=29;y=64;width=65;height=98;bc=#3B3B3B;pw=0;"
shape[7] = "func=rect;x=35;y=85;width=53;height=68;bc=#DC143C;pw=0;"
shape[8] = "func=line;x=28;y=64;x1=0;y1=0;x2=8;y2=21;pc=#DC143C;pw=2;"
shape[9] = "func=line;x=87;y=63;x1=6;y1=0;x2=0;y2=22;pc=#DC143C;pw=2;"
shape[10] = "func=line;x=86;y=152;x1=0;y1=0;x2=7;y2=10;pc=#DC143C;pw=2;"
shape[11] = "func=line;x=28;y=151;x1=8;y1=0;x2=0;y2=11;pc=#DC143C;pw=2;"
shape[12] = "func=rect;x=20;y=192;width=22;height=6;bc=#800000;pw=0;"
shape[13] = "func=rect;x=78;y=192;width=22;height=6;bc=#800000;pw=0;"
shape[14] = "func=ell;x=24;y=7;width=19;height=25;bc=#66FFFFFF;pw=0;"
shape[15] = "func=ell;x=79;y=7;width=19;height=25;bc=#66FFFFFF;pw=0;"
EndSub

Sub Shapes_Init_Lane
' 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=line;x1=20;y1=0;x2=20;y2=107;pc=#FFFFFF;pw=8;"
shape[2] = "func=line;x1=206;y1=0;x2=206;y2=107;pc=#FFFFFF;pw=8;"
shape[3] = "func=line;x1=392;y1=0;x2=392;y2=107;pc=#FFFFFF;pw=8;"
shape[4] = "func=line;x1=578;y1=0;x2=578;y2=107;pc=#FFFFFF;pw=8;"
EndSub

Sub Shapes_Init_Truck
' Shapes | Initialize shapes data
' return shX, shY - current position of shapes
' return shape - array of shapes
shX = 430 ' x offset
shY = 150 ' y offset
shape = ""
shape[1] = "func=rect;x=0;y=20;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[2] = "func=rect;x=77;y=23;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[3] = "func=rect;x=0;y=154;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[4] = "func=rect;x=77;y=156;width=36;height=44;bc=#3B3B3B;pw=0;"
shape[5] = "func=rect;x=11;y=0;width=93;height=230;bc=#C2C2C2;pw=0;"
shape[6] = "func=rect;x=17;y=25;width=81;height=55;bc=#3B3B3B;pw=0;"
shape[7] = "func=rect;x=27;y=35;width=62;height=48;bc=#C2C2C2;pw=0;"
shape[8] = "func=line;x=17;y=24;x1=11;y1=14;x2=0;y2=0;pc=#C2C2C2;pw=2;"
shape[9] = "func=line;x=87;y=23;x1=0;y1=16;x2=12;y2=0;pc=#C2C2C2;pw=2;"
shape[10] = "func=rect;x=17;y=222;width=22;height=6;bc=#800000;pw=0;"
shape[11] = "func=rect;x=75;y=222;width=22;height=6;bc=#800000;pw=0;"
shape[12] = "func=ell;x=77;y=4;width=21;height=15;bc=#66FFFFFF;pw=0;"
shape[13] = "func=ell;x=17;y=4;width=21;height=15;bc=#66FFFFFF;pw=0;"
shape[14] = "func=rect;x=17;y=85;width=81;height=134;bc=#807547;pw=0;"
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 shape[] - shape array
' return shWidth, shHeight - total size of shapes
For i = 1 To Array.GetItemCount(shape)
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