Skip to content

Instantly share code, notes, and snippets.

@Resike
Forked from tylerneylon/rounded_rectangle.lua
Created June 25, 2013 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Resike/5861478 to your computer and use it in GitHub Desktop.
Save Resike/5861478 to your computer and use it in GitHub Desktop.
-- 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