Welcome Guest, you are in: Login

Fruit Of The Shed

Navigation (MMBasic)






Search the wiki

»


Page History: Shape drawing for Complex Lines and Polygons

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: 2019/05/21 16:00


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.

Dependencies
Sensible Cartesian Co-ordinate graphics pack

Preamble
The commands assume that arrays are dimensioned from zero (not 1)
	Option Base 0


Example
Shape.New(4) ' draw a 50 pixel square
Shape.Vertex(0,0)
Shape.Vertex(0,50)
Shape.Vertex(50,50)
Shape.Vertex(50,0)
Shape.Draw(0,0,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 1
		Erase ShX
		Dim Integer ShX(Points-1)

		On Error Skip 1
		Erase ShY
		Dim Integer ShY(Points-1)

		On Error Skip 1
		Dim Integer ShPtr
		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)
		ShX(ShPtr)=x
		ShY(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+ShX(0),yOffset+ShY(0)
		For n=1 to ShPtr-1
			Draw xOffset+ShX(n),yOffset+ShY(n)
		Next
		If Opt Then Draw xOffset+ShX(0),yOffset+ShY(0)
	End Sub