drawing routines in mojo -- filled vice unfilled

Monkey Forums/Monkey Beginners/drawing routines in mojo -- filled vice unfilled

navyRod(Posted 2014) [#1]
Is there any options in the drawing functions to allow for filled vice unfilled shapes. ?

Thanks
Rod


Shinkiro1(Posted 2014) [#2]
This is from my framework and probably most of it copied from other users here.
The DrawRoundRect doesn't work perfectly yet.
Const MAX_VERTS:Int = 1024

Function DrawRectOutline:Void(x:Float, y:Float, width:Float, height:Float)
	DrawLine(x, y, x + width, y)
	DrawLine(x + width, y, x + width, y + height)
	DrawLine(x + width, y + height, x, y + height)
	DrawLine(x, y + height, x, y)
End

Function DrawCircleOutline:Void(x:Float, y:Float, radius:Float, detail:Int = -1)
	If detail < 0
		detail = radius
	ElseIf detail < 3
		detail = 3
	ElseIf detail > MAX_VERTS
		detail = MAX_VERTS
	End

	Local angleStep:Float = 360.0 / detail
	Local angle:Float
	Local offsetX:Float
	Local offsetY:float
	Local first:Bool = True
	Local firstX:Float
	Local firstY:float
	Local thisX:Float
	Local thisY:float
	Local lastX:Float
	Local lastY:Float

	For Local vertIndex:= 0 Until detail
		offsetX = Sin(angle) * radius
		offsetY = Cos(angle) * radius
		If first
			first = False
			firstX = x + offsetX
			firstY = y + offsetY
			lastX = firstX
			lastY = firstY
		Else
			thisX = x + offsetX
			thisY = y + offsetY
			DrawLine(lastX, lastY, thisX, thisY)
			lastX = thisX
			lastY = thisY
		EndIf
		angle += angleStep
	Next
	DrawLine(lastX, lastY, firstX, firstY)
End

Function DrawRoundRect:Void(x:Float, y:Float, width:Float, height:Float, radius:Float)
	radius = Min(radius, height)
	radius = Min(radius, width)
	DrawOval(x, y, radius, radius)
	DrawOval(x + (width - (radius)), y, radius, radius)
	DrawOval(x, y + (height - (radius)), radius, radius)
	DrawOval(x + (width - (radius)), y + (height - (radius)), radius, radius)
	DrawRect(x + (radius / 2), y, width - radius, height)
	DrawRect(x, y + (radius / 2), width, height - radius)
End

Function DrawRoundRectOutline:Void(x:Float, y:Float, w:Float, h:Float, radius:Float)
	Local radiusHalf:Float = radius * 0.5
	
	DrawRect(x + radiusHalf, y, w - radius, 1)
	DrawRect(x + radiusHalf, y + h, w - radius, 1)
	DrawRect(x, y + radiusHalf, 1, h - radius)
	DrawRect(x + w, y + radiusHalf, 1, h - radius)
	
	DrawArc(x + radiusHalf, y + radiusHalf, radius, 180, 90)
	DrawArc(x + w - radiusHalf, y + radiusHalf, radius, 180, -90)
	DrawArc(x + radiusHalf, y + h - radiusHalf, radius, 0, -90)
	DrawArc(x + w - radiusHalf, y + h - radiusHalf, radius, 0, 90)
End

Function DrawArc:Void(x:Float, y:Float, radius:Float, startAngle:Float, angle:Float, detail:Int = -1)
	Local radiusHalf:Float = radius * 0.5
	If detail = -1
		detail = Ceil(radiusHalf)
	ElseIf detail < 3
		detail = 3
	ElseIf detail > MAX_VERTS
		detail = MAX_VERTS
	End
	
	Local change:Float = 1.0 / detail
	Local time:Float

	Local lastX:Float = x + Sin(startAngle) * radiusHalf
	Local lastY:Float = y + Cos(startAngle) * radiusHalf
	
	For Local i:Int = 0 To detail
		Local offX:Float = Sin(startAngle + angle * time) * radiusHalf
		Local offY:Float = Cos(startAngle + angle * time) * radiusHalf
		Local thisX:Float = x + offX
		Local thisY:Float = y + offY
		DrawLine(lastX, lastY, thisX, thisY)
		lastX = thisX
		lastY = thisY
		time += change
	Next
End

Function DrawThickLine:Void(x1:Float, y1:Float, x2:Float, y2:Float, size:Float, filled:Bool = False, detail:Int = -1)
	Local radius:Float = size / 2.0

	If detail < 0
		detail = size / 5.0
		If detail < 12
			detail = 12
		ElseIf detail > MAX_VERTS
			detail = MAX_VERTS
		EndIf
	EndIf

	Local movementAngle:Float = ATan2ToDegrees(x1 - x2, y1 - y2)
	Local offsetX:Float = (Sin(movementAngle + 90) * radius)
	Local offsetY:Float = (Cos(movementAngle + 90) * radius)
	Local circleIndex:Int
	Local circleAngleStep:Float = 180.0 / (detail + 1)
	Local circleAngle:Float

	If filled = False
		'just draw lines
		Local firstX:Float
		Local firstY:Float
		Local lastX:Float
		Local lastY:Float
		Local thisX:Float
		Local thisY:float

		'edge
		firstX = x1 + offsetX
		firstY = y1 + offsetY
		lastX = x2 + offsetX
		lastY = y2 + offsetY
		DrawLine(firstX, firstY, lastX, lastY)

		'end circle
		If detail > 0
			circleAngle = movementAngle + 90 + circleAngleStep
			For circleIndex = 0 Until detail
				thisX = x2 + (Sin(circleAngle) * radius)
				thisY = y2 + (Cos(circleAngle) * radius)
				DrawLine(lastX, lastY, thisX, thisY)
				lastX = thisX
				lastY = thisY
				circleAngle += circleAngleStep
			Next
		EndIf

		'top/end circle last
		offsetX = -offsetX
		offsetY = -offsetY

		thisX = x2 + offsetX
		thisY = y2 + offsetY
		DrawLine(lastX, lastY, thisX, thisY)
		lastX = thisX
		lastY = thisY

		'edge
		thisX = x1 + offsetX
		thisY = y1 + offsetY
		DrawLine(lastX, lastY, thisX, thisY)
		lastX = thisX
		lastY = thisY

		'start circle
		If detail > 0
			circleAngle = movementAngle - 90 + circleAngleStep
			For circleIndex = 0 Until detail
				thisX = x1 + (Sin(circleAngle) * radius)
				thisY = y1 + (Cos(circleAngle) * radius)
				DrawLine(lastX, lastY, thisX, thisY)
				lastX = thisX
				lastY = thisY
				circleAngle += circleAngleStep
			Next
		EndIf

		'top/end circle last
		DrawLine(lastX, lastY, firstX, firstY)
		
	Else
		'setup verts array
		Local verts:Float[8 + (detail * 2 * 2)]
		Local index:Int

		'edge
		verts[0] = x1 + offsetX
		verts[1] = y1 + offsetY
		verts[2] = x2 + offsetX
		verts[3] = y2 + offsetY
		index = 4

		'end circle
		If detail > 0
			circleAngle = movementAngle + 90 + circleAngleStep
			For circleIndex = 0 Until detail
				verts[index] = x2 + (Sin(circleAngle) * radius)
				verts[index + 1] = y2 + (Cos(circleAngle) * radius)
				index += 2
				circleAngle += circleAngleStep
			Next
		EndIf

		'edge
		offsetX = -offsetX
		offsetY = -offsetY

		verts[index] = x2 + offsetX
		verts[index + 1] = y2 + offsetY
		verts[index + 2] = x1 + offsetX
		verts[index + 3] = y1 + offsetY
		index += 4

		'start circle
		If detail > 0
			circleAngle = movementAngle - 90 + circleAngleStep
			For circleIndex = 0 Until detail
				verts[index] = x1 + (Sin(circleAngle) * radius)
				verts[index + 1] = y1 + (Cos(circleAngle) * radius)
				index += 2
				circleAngle += circleAngleStep
			Next
		EndIf

	'draw it
	DrawPoly(verts)
	EndIf
End




Raph(Posted 2014) [#3]
There's a thread here with code for many shapes:

http://www.monkey-x.com/Community/posts.php?topic=2462


navyRod(Posted 2014) [#4]
appreciate info