Here are some shape scripts that I put together that might be useful to someone. Some of the procedures already exist in the Canvas list, some don't, but these take away the need to worry about moving the pencil to x,y first and building paths with beginPath and Stroke'ing, (unless you need to build a complex path before drawing it). Also, the procedures that use angles accept degrees, not radians.
'************************************************************************************
'Converts angles to radians
'************************************************************************************
Function inRadians(angle)
Dim PI
PI = 3.14159265
inRadians = (angle * PI) / 180
End Function
'************************************************************************************
'Usage: showImage(context,locx,locy,path)
'************************************************************************************
Sub showImage(ctx,x,y,path)
Dim img
Set img = ctx.loadImage(path)
ctx.drawImage img,x,y
End Sub
'************************************************************************************
'Usage: drawArc(context,locx,locy,radius, startangle - in degrees, endangle - in degrees, direction)
' - direction = anticlockwise (true) clockwise (false)
'************************************************************************************
Sub drawArc(ctx,x,y,radius,startAngle,endAngle,anticlockwise)
Dim startA
Dim endA
startA = inRadians(startAngle)
endA = inRadians(endAngle)
ctx.beginPath
ctx.moveTo x,y
ctx.arc x,y,radius,startA,endA, anticlockwise
ctx.stroke
End Sub
'************************************************************************************
'Usage: drawPieSlice(context,locx,locy,radius, startangle - in degrees, endangle - in degrees, direction, filled)
' - direction = anticlockwise (true) clockwise (false)
'************************************************************************************
Sub drawPieSlice(ctx,x,y,radius,startAngle,endAngle,anticlockwise,filled)
Dim startA
Dim endA
startA = inRadians(startAngle)
endA = inRadians(endAngle)
ctx.beginPath
ctx.moveTo x,y
ctx.arc x,y,radius,startA,endA, anticlockwise
ctx.lineTo x,y
ctx.stroke
if filled = true then
ctx.fill
end if
End Sub
'******************************************************************************
'Usage: drawCircle context,centerx,centery, radius, number of segments, filled
'******************************************************************************
Sub drawCircle(ctx,cx,cy,radius,num_segments, filled)
Const PI = 3.14159265
Dim X
Dim Y
Dim theta
Dim dtheta
Dim seg
dtheta = 2 * PI/ num_segments
ctx.beginPath
ctx.moveto cx+radius, cy
theta = 0
For seg = 1 To num_segments
theta = theta + dtheta
ctx.lineto cx + (radius * Cos(theta)), cy + (radius * Sin(theta))
Next
if filled = true then
ctx.fill
end if
ctx.stroke
End Sub
'******************************************************************************
'Usage: drawAngleLine context,startx,starty, length of line, angle in degrees
'******************************************************************************
Sub drawAngleLine(ctx,startx,starty,linelength,angle)
Const PI = 3.14159265
Dim theta
theta = (2 * PI/ 360) * angle
ctx.beginPath
ctx.moveto startx,starty
ctx.lineto startx + (linelength * Cos(theta)), starty + (linelength * Sin(theta))
'drawLine ctx, startx + ((linelength-200) * Cos(theta)), starty + ((linelength-200) * Sin(theta)), startx + (linelength * Cos(theta)), starty + (linelength * Sin(theta))
ctx.stroke
End Sub
'************************************************************************************
'usage : returns distance from point x1,x1 to point x2,y2
'************************************************************************************
Function distance(x1,y1,x2,y2)
distance = sqr( ((x2-x1)*(x2-X1)) + ((y2-y1)*(y2-y1)))
End Function
'******************************************************************************
'Usage: drawRectangle context,startx,starty,endx,endy, filled
'******************************************************************************
Sub drawRectangle(ctx,x1,y1,x2,y2, filled)
ctx.beginPath
ctx.moveto x1, y1
ctx.rect x1,y1,abs(x2-x1),abs(y2-y1)
if filled = true then
ctx.fill
end if
ctx.stroke
End Sub
'******************************************************************************
'Usage: drawGrid context,startx,starty,endx,endy, grid size
' - Draws grid by specifying the grid size in pixels
'******************************************************************************
Sub drawGrid(ctx,x1,y1,x2,y2,gridsize)
Dim L
Dim Width
Dim Height
Width = ABS(x2-x1)
Height = ABS(y2-y1)
For L = 1 to (width+gridsize) step gridsize
drawLine ctx,x1+L,y1,x1+L,y2
drawLine ctx,x1,y1+L,x2,y1+L
Next
drawRectangle ctx,x1,y1,x2,y2,false
End Sub
'******************************************************************************
'Usage: drawGrid2 context,startx,starty,endx,endy, num of divisions along x, num of divisions along y
' - Draws grid by specifying the number of divisions per direction
'******************************************************************************
Sub drawGrid2(ctx,x1,y1,x2,y2,xdivisions, ydivisions)
Dim L
Dim Width
Dim Height
Dim xgridsize
Dim ygridsize
Width = ABS(x2-x1)
Height = ABS(y2-y1)
xgridsize = round(Width/xdivisions)
ygridsize = round(Height/ydivisions)
For L = 1 to width step xgridsize
drawLine ctx,x1+L,y1,x1+L,y2
Next
For L = 1 to height step ygridsize
drawLine ctx,x1,y1+L,x2,y1+L
Next
drawRectangle ctx,x1,y1,x2,y2,false
End Sub
'******************************************************************************
'Usage: draws a line on the x-axis with ticks on it, could be used for graph axis
'******************************************************************************
Sub tick_x(ctx,x1,y1,x2,xdivisions,ticklength)
Dim L
Dim Width
Dim xgridsize
Width = ABS(x2-x1)
xgridsize = round(Width/xdivisions)
For L = 0 to width step xgridsize
drawLine ctx,x1+L,y1,x1+L,y1-ticklength
Next
drawLine ctx,x1,y1,x2,y1
End Sub
'******************************************************************************
'Usage: draws a line on the y-axis with ticks on it, could be used for graph axis
'******************************************************************************
Sub tick_y(ctx,x1,y1,y2,ydivisions,ticklength)
Dim L
Dim Height
Dim ygridsize
Height = ABS(y2-y1)
ygridsize = round(Height/ydivisions)
For L = 0 to height step ygridsize
drawLine ctx,x1,y1+L,x1+ticklength,y1+L
Next
drawLine ctx,x1,y1,x1,y2
End Sub
'******************************************************************************
'Usage: drawEllipse context,centerx,centery, radiuswide, raiustall, number of segments, filled
'******************************************************************************
Sub drawEllipse(ctx,cx,cy,radius1,radius2,num_segments, filled)
Const PI = 3.14159265
Dim startX
Dim startY
Dim theta
Dim dtheta
Dim seg
dtheta = 2 * PI/ num_segments
ctx.beginPath
ctx.moveto cx+radius1, cy
theta = 0
For seg = 1 To num_segments
theta = theta + dtheta
ctx.lineto cx + ((radius1) * Cos(theta)), cy + (radius2 * Sin(theta))
if seg =1 then
startX=cx + (radius1 * Cos(theta))
startY=cy + (radius2 * Sin(theta))
end if
Next
ctx.lineto startX, StartY
if filled = true then
ctx.fill
end if
ctx.stroke
End Sub
'************************************************************************************
'Usage: drawRoundRectangle context,Leftx,Lefty, width, height, corner radius, filled
'************************************************************************************
Sub drawRoundRectangle(ctx, x, y, width, height, radius, filled)
ctx.beginPath
ctx.moveTo x, y + radius
ctx.lineTo x, y + height - radius
ctx.quadraticCurveTo x , y + height, x + radius, y + height
ctx.lineTo x + width - radius, y + height
ctx.quadraticCurveTo x + width, y + height, x + width, y + height - radius
ctx.lineTo x + width, y + radius
ctx.quadraticCurveTo x + width, y, x + width - radius, y
ctx.lineTo x + radius, y
ctx.quadraticCurveTo x, y, x, y + radius
if filled = true then
ctx.fill
end if
ctx.stroke
End Sub