Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SUB that draws boxes with rounded corners.
#1
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

Find my programs here in Dav's QB64 Corner
Reply
#2
Hi, thanks for sharing. You saved me work again Smile that will come in handy. I would proceed the same way in the program.


Reply
#3
https://qb64phoenix.com/forum/showthread.php?tid=2091
Reply
#4
https://qb64phoenix.com/forum/showthread...18#pid1218
b = b + ...
Reply
#5
(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

Find my programs here in Dav's QB64 Corner
Reply
#6
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
Reply
#7
Looks like I have an error somewhere, my box positions aren't lining up.  Will fix it...

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#8
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..
Reply
#9
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...8#pid28378
Write name of program in 1st line to copy & paste & save filename.bas
Insert program pictures: press print-screen-shot button
Open paint & Paste & Save as PNG
Add picture file to program topic

Russia looks world from future. Big data is peace data.
I never recommend anything & always write only about myself
Reply
#10
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

Find my programs here in Dav's QB64 Corner
Reply




Users browsing this thread: 2 Guest(s)