Microsoft Small Basic

Program Listing: PCS454
GraphicsWindow.Title = "Regular Polygon"
gw = GraphicsWindow.Width
gh = GraphicsWindow.Height
gx0 = gw / 2
gy0 = gh / 2
scaleX = 130
scaleY = -130
x = 0
y = 0
r = 1
DrawGrid()
GraphicsWindow.BrushColor = "#99999999"
n = 3
DrawPolygon()
GraphicsWindow.BrushColor = "Black"
Controls.AddButton("Draw", 120, 20)
sides = Controls.AddTextBox(20, 20)
Controls.SetSize(sides, 90, 30)
Controls.SetTextBoxText(sides, n)
CalcArea()
area = Shapes.AddText("Area = " + a)
Shapes.Move(area, 20, 60)
Controls.ButtonClicked = OnButtonClicked
Sub CalcArea
' param n - sides of regular polygon
' return a - area of regular polygon
angle = 360 / n
_angle = Math.GetRadians(angle)
a = Math.Sin(_angle) / 2 * n
EndSub
Sub OnButtonClicked
_n = Controls.GetTextBoxText(sides)
If _n + 0 = _n And 3 <= _n Then
n = _n
CalcArea()
Shapes.SetText(area, "Area = " + a)
GraphicsWindow.BrushColor = "White"
GraphicsWindow.FillRectangle(0, 0, gw, gh)
DrawGrid()
GraphicsWindow.BrushColor = "#99999999"
DrawPolygon()
EndIf
EndSub
Sub DrawPolygon
' Draw regular polygon
' param n - number of side
param["x1"] = x
param["y1"] = y
param["x2"] = x + r
param["y2"] = y
For i = 1 To n
a = 360 / n * i
_a = Math.GetRadians(a)
param["x3"] = x + r * Math.Cos(_a)
param["y3"] = y + r * Math.Sin(_a)
FillTriangle()
param["x2"] = param["x3"]
param["y2"] = param["y3"]
EndFor
EndSub
Sub FillTriangle
' param x1, y1, x2, y2, x3, y3
gx1 = gx0 + param["x1"] * scaleX
gy1 = gy0 + param["y1"] * scaleY
gx2 = gx0 + param["x2"] * scaleX
gy2 = gy0 + param["y2"] * scaleY
gx3 = gx0 + param["x3"] * scaleX
gy3 = gy0 + param["y3"] * scaleY
GraphicsWindow.FillTriangle(gx1, gy1, gx2, gy2, gx3, gy3)
EndSub
Sub DrawLine
' param x1, y1, x2, y2
gx1 = gx0 + param["x1"] * scaleX
gy1 = gy0 + param["y1"] * scaleY
gx2 = gx0 + param["x2"] * scaleX
gy2 = gy0 + param["y2"] * scaleY
GraphicsWindow.DrawLine(gx1, gy1, gx2, gy2)
EndSub
Sub DrawText
' param x, y, text
gx = gx0 + param["x"] * scaleX
gy = gy0 + param["y"] * scaleY
GraphicsWindow.DrawText(gx, gy, param["text"])
EndSub
Sub DrawGrid
GraphicsWindow.PenColor = "MediumSeaGreen"
GraphicsWindow.BrushColor = "MediumSeaGreen"
For _x = -2.5 To 2.0 Step 0.5
param["x1"] = _x
param["y1"] = -3
param["x2"] = _x
param["y2"] = 3
DrawLine()
If 2.0 - 0.5 < _x Then
param["x"] = _x + 0.05
param["y"] = -0.05
param["text"] = "x"
DrawText()
Else
param["x"] = _x + 0.05
param["y"] = -0.05
param["text"] = _x
DrawText()
EndIf
EndFor
For _y = -1.5 To 1.5 Step 0.5
param["x1"] = -3
param["y1"] = _y
param["x2"] = 3
param["y2"] = _y
DrawLine()
If 1.5 - 0.5 < _y Then
param["x"] = 0.05
param["y"] = _y - 0.05
param["text"] = "y"
DrawText()
Else
param["x"] = 0.05
param["y"] = _y - 0.05
param["text"] = _y
DrawText()
EndIf
EndFor
EndSub