QB64 Phoenix Edition
SUB that draws boxes with rounded corners. - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: SUB that draws boxes with rounded corners. (/showthread.php?tid=3032)

Pages: 1 2 3


SUB that draws boxes with rounded corners. - Dav - 09-14-2024

Needed a SUB to draw boxes with rounded corners, filled or unfilled.  Here's what came out of it.  You can control the amount of corner rounded-ness by giving a radius value.   I made a smaller one using the filled circle routine (circle for each corner), but it only could do filled boxes, so used arc routines to draw them.  Probably someone has a better method to do this, just thought I'd throw mine into the mix. 

- Dav

EDIT: Code fixed!

Code: (Select All)
'========
'RBOX.BAS
'========
'Draws a box with rounded corners, filled or unfilled.
'Coded by Dav, SEP/2024

Randomize Timer

Screen _NewImage(1000, 700, 32)

'this demo draws random boxes with round corners...

Do
    x1 = Int(Rnd * _Width): x2 = x1 + 120 + Int(Rnd * 100)
    y1 = Int(Rnd * _Height): y2 = y1 + 120 + Int(Rnd * 100)
    radius = 20 + Int(Rnd * 30)
    Rbox x1, y1, x2, y2, radius, _RGB(Rnd * 255, Rnd * 255, Rnd * 255), Int(Rnd * 2)
    _Limit 30
Loop Until InKey$ <> ""

End


Sub Rbox (x1, y1, x2, y2, r, clr&, fill)
    'x1/y1, y1/y2 = placement of box
    'r = radius of rounded corner
    'clr& = color of box
    'fill =  1 for filled, 0 for just an edge
    If fill = 1 Then
        Line (x1, y1 + r)-(x2, y2 - r), clr&, BF 'middle
        Line (x1 + r, y1)-(x2 - r, y2), clr&, BF '(ditto)
    Else
        Line (x1 + r, y1)-(x2 - r, y1), clr& 'top
        Line (x1 + r, y2)-(x2 - r, y2), clr& 'bottom
        Line (x1, y1 + r)-(x1, y2 - r), clr& 'left
        Line (x2, y1 + r)-(x2, y2 - r), clr& 'right
    End If
    'top left corner arc
    For angle = 180 To 270
        x3 = (x1 + r) + r * Cos(_D2R(angle))
        y3 = (y1 + r) + r * Sin(_D2R(angle))
        If fill = 1 Then
            Line (x3 + r, y3 + r)-(x3, y3), clr&, BF
        Else
            PSet (x3, y3), clr&
        End If
    Next
    'top right corner arc
    For angle = 270 To 360
        x3 = (x2 - r) + r * Cos(_D2R(angle))
        y3 = (y1 + r) + r * Sin(_D2R(angle))
        If fill = 1 Then
            Line (x2 - r, y1 + r)-(x3, y3), clr&, BF
        Else
            PSet (x3, y3), clr&
        End If
    Next
    'bottom left corner arc
    For angle = 90 To 180
        x3 = (x1 + r) + r * Cos(_D2R(angle))
        y3 = (y2 - r) + r * Sin(_D2R(angle))
        If fill = 1 Then
            Line (x1 + r, y2 - r)-(x3, y3), clr&, BF
        Else
            PSet (x3, y3), clr&
        End If
    Next
    'bottom right corner
    For angle = 0 To 90
        x3 = (x2 - r) + r * Cos(_D2R(angle))
        y3 = (y2 - r) + r * Sin(_D2R(angle))
        If fill = 1 Then
            Line (x2 - r, y2 - r)-(x3, y3), clr&, BF
        Else
            PSet (x3, y3), clr&
        End If
    Next
End Sub



RE: SUB that draws boxes with rounded corners. - Petr - 09-14-2024

Hi, thanks for sharing. You saved me work again Smile that will come in handy. I would proceed the same way in the program.


RE: SUB that draws boxes with rounded corners. - SMcNeill - 09-14-2024

https://qb64phoenix.com/forum/showthread.php?tid=2091


RE: SUB that draws boxes with rounded corners. - bplus - 09-14-2024

https://qb64phoenix.com/forum/showthread.php?tid=272&pid=1218#pid1218


RE: SUB that draws boxes with rounded corners. - Dav - 09-14-2024

(09-14-2024, 04:33 PM)SMcNeill Wrote: https://qb64phoenix.com/forum/showthread.php?tid=2091

Neat routines!  I need to make it habit of skimming through your area first before trying to come up with something.

EDIT: Yours too, @bplus.  Nice.

- Dav


RE: SUB that draws boxes with rounded corners. - SMcNeill - 09-14-2024

There's a lot of stuff out there that's already been created and used for years now -- we're not a young and undeveloped project anymore.  The problem is always finding it when you need it.  There's just soooo much quality stuff by everyone, that it's hard to remember it all, so many of us just end up reinventing the wheel over and over.  Wink


RE: SUB that draws boxes with rounded corners. - Dav - 09-14-2024

Looks like I have an error somewhere, my box positions aren't lining up.  Will fix it...

- Dav


RE: SUB that draws boxes with rounded corners. - RhoSigma - 09-14-2024

Basically you could also take my Polygon drawing library.

Just define the shape of the box as series of x/y points and then call FillPolygon.

I use this e.g. in my GuiTools to draw the tooltip speaking bubbles as you see in the pictures here, but basically every shape would be possible like sine wave borders or ellipse boxes etc..


RE: SUB that draws boxes with rounded corners. - DANILIN - 09-14-2024

Line 18

If Rnd > .33 Then Print ' Danilin

or simple

PRINT

and picture fly up ... since 1990-th

plus: automatic MazeBall

https://qb64phoenix.com/forum/showthread.php?tid=2761&pid=28378#pid28378


RE: SUB that draws boxes with rounded corners. - Dav - 09-14-2024

I fixed the code.  It is updated, and works correctly now. I sure had the x2/y2 all messed up.  So, @Petr, if you do use this please get the updated code, old one is no good.

@RhoSigma: Thank you.  Your libraries are outstanding and very professional!

- Dav