Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SUB that draws boxes with rounded corners.
#24
Updated mine to get ride of the edge overlap.  May add thick line edges next time,  I like those thicker edges.  Planning to use this for menus.

- Dav

Code: (Select All)

'========
'RBOX.BAS v1.02
'========
'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, _RGBA(Rnd * 255, 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, y2/y2 = placement of box
    ' r = radius of rounded corner
    ' clr~& = color of box
    ' fill =  1 for filled, 0 for just an edge

    ReDim filled(_Width + x2, _Height + y2) As Integer

    If fill = 1 Then
        Line (x1 + r + 1, y1)-(x2 - r - 1, y1 + r), clr~&, BF 'top
        Line (x1 + r + 1, y2 - r)-(x2 - r - 1, y2), clr~&, BF 'bottom
        Line (x1, y1 + r + 1)-(x1 + r, y2 - r - 1), clr~&, BF 'left
        Line (x2 - r, y1 + r + 1)-(x2, y2 - r - 1), clr~&, BF 'right
        Line (x1 + r + 1, y1 + r + 1)-(x2 - r - 1, y2 - r - 1), clr~&, BF 'middle
    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
    For angle = 180 To 270
        If fill = 1 Then
            For radius = 0 To r
                x3 = (x1 + r) + radius * Cos(_D2R(angle))
                y3 = (y1 + r) + radius * Sin(_D2R(angle))
                If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
            Next
        Else
            x3 = (x1 + r) + r * Cos(_D2R(angle))
            y3 = (y1 + r) + r * Sin(_D2R(angle))
            If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
        End If
    Next

    'top right corner
    For angle = 270 To 360
        If fill = 1 Then
            For radius = 0 To r
                x3 = (x2 - r) + radius * Cos(_D2R(angle))
                y3 = (y1 + r) + radius * Sin(_D2R(angle))
                If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
            Next
        Else
            x3 = (x2 - r) + r * Cos(_D2R(angle))
            y3 = (y1 + r) + r * Sin(_D2R(angle))
            If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
        End If
    Next

    'bottom left corner
    For angle = 90 To 180
        If fill = 1 Then
            For radius = 0 To r
                x3 = (x1 + r) + radius * Cos(_D2R(angle))
                y3 = (y2 - r) + radius * Sin(_D2R(angle))
                If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
            Next
        Else
            x3 = (x1 + r) + r * Cos(_D2R(angle))
            y3 = (y2 - r) + r * Sin(_D2R(angle))
            If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
        End If
    Next

    'bottom right corner
    For angle = 0 To 90
        If fill = 1 Then
            For radius = 0 To r
                x3 = (x2 - r) + radius * Cos(_D2R(angle))
                y3 = (y2 - r) + radius * Sin(_D2R(angle))
                If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
            Next
        Else
            x3 = (x2 - r) + r * Cos(_D2R(angle))
            y3 = (y2 - r) + r * Sin(_D2R(angle))
            If filled(x3, y3) = 0 Then PSet (x3, y3), clr~&: filled(x3, y3) = 1
        End If
    Next

End Sub

Find my programs here in Dav's QB64 Corner
Reply


Messages In This Thread
RE: SUB that draws boxes with rounded corners. - by Dav - 09-15-2024, 01:27 PM



Users browsing this thread: 6 Guest(s)