Small Basic: Polygon
This article explains about polygon and Microsoft Small Basic programming language.
What is a Polygon
A polygon is a shape which have more than three edges.
How to Draw Polygon
Small Basic doesn't have method (operation) to draw / fill polygons. But at least there are following three ways to do that.
Turtle
One easy way for drawing a regular polygon is to use Turtle. Following code draws a regular pentagon.
n = 5
e = 100
a = 360 / n
For i = 1 To n
Turtle.Move(e)
Turtle.Turn(a)
EndFor
Lines and Triangles
Another way for drawing a polygon is to connect lines to draw edges and fill with triangles. Following code draws and fills a hexagon.
points[1] = "x=100;y=100;"
points[2] = "x=200;y=80;"
points[3] = "x=300;y=110;"
points[4] = "x=350;y=200;"
points[5] = "x=250;y=250;"
points[6] = "x=120;y=220;"
n = Array.GetItemCount(points)
p1 = points[1]
' fill polygon
For i = 3 To n
p2 = points[i - 1]
p3 = points[i]
GraphicsWindow.FillTriangle(p1["x"], p1["y"], p2["x"], p2["y"], p3["x"], p3["y"])
EndFor
' draw polygon
For i = 2 To n
p1 = points[i - 1]
p2 = points[i]
GraphicsWindow.DrawLine(p1["x"], p1["y"], p2["x"], p2["y"])
EndFor
p1 = points[n]
p2 = points[1]
GraphicsWindow.DrawLine(p1["x"], p1["y"], p2["x"], p2["y"])
LitDev Extension
The other way is to use LDShapes.AddPolygon() in LitDev Extension. Following codes adds a heptagon as a shape.
points[1] = "1=120;2=100;"
points[2] = "1=180;2=80;"
points[3] = "1=280;2=110;"
points[4] = "1=320;2=200;"
points[5] = "1=240;2=250;"
points[6] = "1=150;2=220;"
points[7] = "1=100;2=160;"
LDShapes.AddPolygon(points)
Tools
There are two tools to edit polygons.
Shapes Editor using Polygon
NaochanON's Shapes Editor can edit polygons with mouse. This tool can generate Small Basic code including LDShapes.AddPolygon() operation.
Small Basic Prime
Small Basic Prime (extended IDE) has a Shapes Editor tool which can also allow to edit polygons with mouse and generates Small Basic code.