Rounded Rectangles
Posted: Wed Mar 06, 2019 7:29 pm
These are some simple functions for drawing rectangles with rounded corners. They are designed to match the built-in DRAW RECT and FILL RECT functions but have an additional parameter for the rectangle's corner radius. These functions work as you would expect with DRAW COLOR, DRAW ALPHA, DRAW SIZE, and FILL COLOR.
Note: If you want to fill a rounded rectangle with transparency (FILL ALPHA < 1) or using a graphics mode other than NORMAL, you will need to do the following:
For examples, see the screenshots on my Gradient Editor and MSGBOX library forum posts.
Note: If you want to fill a rounded rectangle with transparency (FILL ALPHA < 1) or using a graphics mode other than NORMAL, you will need to do the following:
- Create a new sprite and begin editing it / drawing to it with SPRITE BEGIN
- Make sure FILL APHA is set to 1 and GRAPHICS MODE is set to NORMAL
- Call FILL_ROUNDRECT to draw the rounded rectangle at full opacity
- Finish drawing to the sprite with SPRITE END
- Move the sprite to the desired location and set the desired graphics settings (using SPRITE ALPHA and GRAPHICS MODE)
- Stamp the sprite to the main graphics layer (or to another sprite)
For examples, see the screenshots on my Gradient Editor and MSGBOX library forum posts.
Code: Select all
'==============================================================
'g'
/*
DRAW_ROUNDRECT (x1, y1, x2, y2, r)
FILL_ROUNDRECT (x1, y1, x2, y2, r)
Functions for drawing rectangles with rounded corners.
Both functions take an input of the rectangle's top-left x,y coordinates and bottom-right x,y coordinates, and the corner radius.
Inputs
------
x1, y1 x,y-coordinates of the top-left corner
x2, y2 x,y-coordinates of the bottom-right corner
r corner radius
DRAW_ROUNDRECT draws a rectangle with rounded corners. This function uses only DRAW LINE and DRAW ARC commands.
FILL_ROUNDRECT fills a rectangle with rounded corners. This function uses only FILL CIRCLE and FILL RECT commands. Note: This function uses multiple fill commands that overlap in some areas. For alpha values that are less than one and non-normal graphics modes, run this command with FILL ALPHA 1 and GRAPHICS MODE NORMAL in a sprite and then stamp the sprite to the final destination with the desired graphics settings.
'==============================================================
*/
''
DEF DRAW_ROUNDRECT (x1, y1, x2, y2, r)
pi = 2*ACOS(0) ' depends on OPTION ANGLE
DRAW LINE x1+r, y1 TO x2-r, y1 ' top
DRAW LINE x2-r, y2 TO x1+r, y2 ' bottom
DRAW LINE x2 , y1+r TO x2 , y2-r ' right
DRAW LINE x1 , y2-r TO x1 , y1+r ' left
DRAW ARC x1+r, y1+r, r, pi , pi*3/2 ' top-left
DRAW ARC x2-r, y1+r, r, pi*3/2, 0 ' top-right
DRAW ARC x2-r, y2-r, r, 0 , pi/2 ' bottom-right
DRAW ARC x1+r, y2-r, r, pi/2 , pi ' bottom-left
END DEF
'==============================================================
DEF FILL_ROUNDRECT (x1, y1, x2, y2, r)
FILL CIRCLE x1+r, y1+r SIZE r ' top-left
FILL CIRCLE x2-r, y1+r SIZE r ' top-right
FILL CIRCLE x2-r, y2-r SIZE r ' bottom-right
FILL CIRCLE x1+r, y2-r SIZE r ' bottom-left
FILL RECT x1+r, y1 TO x2-r, y2 ' vertical
FILL RECT x1 , y1+r TO x2 , y2-r ' horizontal
END DEF
'==============================================================