rounded rect

BlitzMax Forums/BlitzMax Programming/rounded rect

ziggy(Posted 2005) [#1]
Is there anyway to draw a rounded rectangle?


semar(Posted 2005) [#2]
With the current Bmax syntax, as far as I know, the answer is no - that is, there's not a command like DrawRoundRect.

You could try these approaches:
- draw and resize a previously made rounded-rect image
- make a class (Type) which will draw a rounded rectangle.

That should not too much complicated. You need to draw 4 arcs which will substitute the rect corners.

Each arc is a fraction of a circle, which center depends on the 4 edges of the rect; width and height of the rect gives also the range of circle.

Hope this helps,
Sergio.


Diablo(Posted 2005) [#3]
try this because i can't - at school



semar(Posted 2005) [#4]
You may adjust to your needs - still some problem with small rects, and no optimized code (too much variables) , but works:

Strict

Graphics 640,480,0
SeedRnd MilliSecs()

Local x = 100
Local y = 100
Local w = 200
Local h = 100

'SetLineWidth 3


While Not KeyDown(KEY_ESCAPE)

x = Rand(0,640)
y = Rand(0,480)
w = Rand(0,640)
h = Rand(0,480)
SetColor Rand(255),Rand(255),Rand(255)

roundrect(x,y,w,h)


FlushMem
Flip

Wend

'================================================
Function RoundRect(x,y,w,h)
'================================================

Local DR = 4
Local R#

If h < w Then
	r = Floor(h/dr)
Else
	r = Floor(w/dr)
EndIf

Local x1 = x + r
Local y1 = y

Local x2 = x + w - r
Local y2 = y

Local x3 = x + w - 1
Local y3 = y + r

Local x4 = x + w - 1
Local y4 = y + h - r

Local x5 = x2
Local y5 = y + h - 1

Local x6 = x1
Local y6 = y5 

Local x7 = x
Local y7 = y4

Local x8 = x
Local y8 = y3

Local xc1 = x1
Local yc1 = y8

Local xc2 = x2
Local yc2 = y3

Local xc3 = x2
Local yc3 = y4

Local xc4 = x1
Local yc4 = y4

'draw lines
'SetColor 0,0,255
DrawLine x1,y1,x2,y2,True
DrawLine x3,y3,x4,y4,True
DrawLine x5,y5,x6,y6,True
DrawLine x7,y7,x8,y8,True

'SetColor 255,255,255
'draw arcs
arc(xc1,yc1,r,180,270)
arc(xc2,yc2,r,270,360)
arc(xc3,yc3,r,0,90)
arc(xc4,yc4,r,90,180)


End Function

'================================================
Function arc(a,b,r,sd,ed)
'================================================

'Plot a,b

If sd < 0 Then
	sd = 360 + sd
EndIf

If ed < 0 Then
	ed = 360 + ed
EndIf

Local angle,x,y

For angle = sd To ed
	x = a + R*Cos(angle)
	y = b + R*Sin(angle)
	Plot x,y
Next



End Function


Sergio.


ImaginaryHuman(Posted 2005) [#5]
Better off for speed to just draw 4 ovals and 2 rects.


Diablo(Posted 2005) [#6]
ok got to fix it



ziggy(Posted 2005) [#7]
Thanks Diablo, it works fine :)