Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

Rounded Rectangles

edited July 2012 in Code Sharing Posts: 371

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! :D


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) rectMode(RADIUS) 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 elseif rectMode() == RADIUS then x = x - w y = y - h w = w * 2 h = h * 2 end ellipseMode(RADIUS) 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

Comments

Sign In or Register to comment.