Skip to content

Instantly share code, notes, and snippets.

@tylerneylon
Created March 25, 2013 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tylerneylon/5235809 to your computer and use it in GitHub Desktop.
Save tylerneylon/5235809 to your computer and use it in GitHub Desktop.
This Lua function draws a rounded-corner rectangle when used in the Love game engine. Its parameters are consistent with the love.graphics conventions.
-- This is similar to love.graphics.rectangle, except that the rectangle has
-- rounded corners. r = radius of the corners, n ~ #points used in the polygon.
function rounded_rectangle(mode, x, y, w, h, r, n)
n = n or 20 -- Number of points in the polygon.
if n % 4 > 0 then n = n + 4 - (n % 4) end -- Include multiples of 90 degrees.
local pts, c, d, i = {}, {x + w / 2, y + h / 2}, {w / 2 - r, r - h / 2}, 0
while i < n do
local a = i * 2 * math.pi / n
local p = {r * math.cos(a), r * math.sin(a)}
for j = 1, 2 do
table.insert(pts, c[j] + d[j] + p[j])
if p[j] * d[j] <= 0 and (p[1] * d[2] < p[2] * d[1]) then
d[j] = d[j] * -1
i = i - 1
end
end
i = i + 1
end
love.graphics.polygon(mode, pts)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment