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


Messages In This Thread
SUB that draws boxes with rounded corners. - by Dav - 09-14-2024, 04:19 PM



Users browsing this thread: 25 Guest(s)