User Tools

Site Tools


Shape drawing for Complex Lines and Polygons

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.



The commands assume that arrays are dimensioned from zero (not 1)

	Option Base 0


 Option Base 0


 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

 For p=0 To 320
 Colour 0
 Move p,0:DrawR 0,240

 For n=1 To 10
  Shape.Vertex(Rnd*Gxm,Rnd*Gym) ' some random scribble

 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

 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)


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
 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)
 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)
  If Opt Then Draw xOffset+Sh(0,0),yOffset+Sh(1,0)
 End Sub
mmbasic/shape_drawing_for_complex_lines_and_polygons.txt · Last modified: 2024/01/19 09:30 by