The MMX and ST variants of MMBasic implement a Polygon command that takes a set of points in two integer arrays and draws an outline of the area with the option to fill the interior.
The following is inspired by this command but includes an option to draw the data as either a line or a polygon. There is no option to fill the enclosed area.
As written below, the routines use the Cartesian drawing commands (see dependencies) to provide co-ordinate 0,0 in the bottom left of the display. It is an easy enough job to replace the relevant drawing commands with the native ones of MMBasic.
An offset can be specified for the Draw command thus allowing the Shape data to be placed using relative coordinates. Scaling could easily be added but I have avoided it for now because it would slow things down with a (probably largely) un-necessary calculation.
This is a pure MMBasic set of routines and thus will work on any MMBasic platform.
DependenciesSensible Cartesian Co-ordinate graphics packPreambleThe commands assume that arrays are dimensioned from zero (not 1)
Option Base 0
Example'preamble
Option Base 0
CLS
Const Gxm=MM.HRes-1,Gym=MM.VRes-1 'screen dimensions follow LCDPANEL settings
Dim Integer tx,ty,otx,oty,Gx,Gy,n,p 'Gx and Gy are the current native co-ordinates
Do
For p=0 To 320
Colour 0
Move p,0:DrawR 0,240
Shape.New(10)
For n=1 To 10
Shape.Vertex(Rnd*Gxm,Rnd*Gym) ' some random scribble
Next
Colour RGB(White)
Shape.Draw(n,n,0) ' 0= the last point isn't connected to the first - a complex line
Shape.New(4) ' a 50 pixel square
Shape.Vertex(0,0)
Shape.Vertex(0,50)
Shape.Vertex(50,50)
Shape.Vertex(50,0)
Colour RGB(Red)
For n=1 To 20 ' 20 random squares
Shape.Draw(Rnd*(Gxm-50),Rnd*(Gym-50),1) ' 1= the last point is connected to the first, enclosing the area (polygon)
Next
Next
Loop
The Code
'Clear the shape stack and pointers in readiness for a new shape.
Sub Shape.New(Points As Integer)
On Error Skip 2
Erase Sh
Dim Integer ShPtr
Dim Integer Sh(1,Points-1) '0,n is X, 1,n is Y
ShPtr=0
End Sub
'Add a new vertex (corner) to the shape stack
'Care must be taken not to add more vertices than were specified in Shape.New
Sub Shape.Vertex(x As Integer,y As Integer)
Sh(0,ShPtr)=x:Sh(1,ShPtr)=y
ShPtr=ShPtr+1
End Sub
'Draw the shape. If opt is non-zero, the shape is closed by drawing between the first and last vertices to produce a polygon
'The shape stack is preserved and so can be re-used.
'X/Y offset is added to the shape vertices allowing the shape to be drawn in multiple locations with the same data.
Sub Shape.Draw(xOffset As Integer,yOffset As Integer,Opt As Integer)
Local n As Integer
Move xOffset+Sh(0,0),yOffset+Sh(1,0)
For n=1 To ShPtr-1
Draw xOffset+Sh(0,n),yOffset+Sh(1,n)
Next
If Opt Then Draw xOffset+Sh(0,0),yOffset+Sh(1,0)
End Sub