# Rounded Rectangles

Here is my code for a rounded rectangle, I focused on it not affecting any other code, and it having a similar syntax to the rect() function.
P.S. It works with the different rectModes!

``````
function draw()
background(0, 0, 0, 255)
fill(255, 0, 0, 255)
stroke(255, 255, 255, 255)
strokeWidth(10)
rectMode(CORNER)
roundRect(WIDTH / 2 - 250, HEIGHT / 2 + 50, 200, 100, 25)
rectMode(CORNERS)
roundRect(WIDTH / 2 + 50, HEIGHT / 2 + 50, WIDTH / 2 + 250, HEIGHT / 2 + 150, 25)
rectMode(CENTER)
roundRect(WIDTH / 2 - 150, HEIGHT / 2 - 150, 200, 100, 25)
roundRect(WIDTH / 2 + 150, HEIGHT / 2 - 150, 100, 50, 25)
end

function roundRect(x,y,w,h,r)
local eM = ellipseMode() -- The previous mode, this is so not to disrupt other ellipses
local rM = rectMode()
if rectMode() == CORNERS then
w = (w - x)
h = (h - y)
elseif rectMode() == CENTER then
x = x - w / 2
y = y - h / 2
x = x - w
y = y - h
w = w * 2
h = h * 2
end
rectMode(CORNER)
clip(x, y + r, w, h - r * 2); rect(x, y, w, h) -- Left and Right
clip(x + r, y, w - r * 2, h); rect(x, y, w, h) -- Top and Bottom
strokeWidth(strokeWidth() - 1) -- Necessary because without it the ellipse looks too big
clip(x, y, r, r); ellipse(x + r - 0.5, y + r - 0.5, r) -- Bottom Left
clip(x, y + h - r, r, r); ellipse(x + r - 0.5, y + h - r + 0.5, r) -- Top Left
clip(x + w - r, y, r, r); ellipse(x + w - r + 0.5, y + r - 0.5, r) -- Bottom Right
clip(x + w - r, y + h - r, r, r); ellipse(x + w - r + 0.5, y + h - r + 0.5, r) -- Top Right
strokeWidth(strokeWidth() + 1) -- Make everything normal again
clip()
ellipseMode(eM)
rectMode(rM)
end
``````

Hi Jordan

Great routine. I used this method earlier and added text and text styling to it on the following link.

Iām halfway through incorporating button functionality to it, and considering to use it for text boxes.

http://twolivesleft.com/Codea/Talk/discussion/comment/7818#Comment_7818

Keep up the good work.

Bri_G