-
-
Save Resike/5861478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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