Last active
September 25, 2020 15:31
-
-
Save mes51/b165b72edb9eaf73827989e90ac8ad6f to your computer and use it in GitHub Desktop.
Stormworksでアンチエイリアス付きの多角形を描画する関数
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
-- 交差判定は以下のコードを利用 | |
-- https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule | |
-- 通常ver (x2 SS) | |
function fillPoly(ps, w, h, r, g, b, a) | |
a = a and a or 255 | |
local left, top, right, bottom = w, h, 0, 0 | |
for i = 1, #ps do | |
local v = ps[i] | |
left = math.min(math.max(math.floor(v.x), 0), left) | |
top = math.min(math.max(math.floor(v.y), 0), top) | |
right = math.max(math.min(math.ceil(v.x), w), right) | |
bottom = math.max(math.min(math.ceil(v.y), h), bottom) | |
end | |
for rx = left, right do | |
for ry = top, bottom do | |
local c = 0 | |
for x = rx, rx + 0.5, 0.5 do | |
for y = ry, ry + 0.5, 0.5 do | |
local hit = false | |
local j = ps[#ps] | |
for ii = 1, #ps do | |
local i = ps[ii] | |
if ((i.y > y) ~= (j.y > y)) and (x < i.x + (j.x - i.x) * (y - i.y) / (j.y - i.y)) then | |
hit = not hit | |
end | |
j = i | |
end | |
c = c + (hit and 1 or 0) | |
end | |
end | |
screen.setColor(r, g, b, a * c * 0.25) | |
screen.drawRect(rx, ry, 1, 1) | |
end | |
end | |
end | |
-- 綺麗だが少し遅いver (RGSS) | |
function fillPoly(ps, w, h, r, g, b, a) | |
a = a and a or 255 | |
local left, top, right, bottom = w, h, 0, 0 | |
for i = 1, #ps do | |
local v = ps[i] | |
left = math.min(math.max(math.floor(v.x), 0), left) | |
top = math.min(math.max(math.floor(v.y), 0), top) | |
right = math.max(math.min(math.ceil(v.x), w), right) | |
bottom = math.max(math.min(math.ceil(v.y), h), bottom) | |
end | |
local rad = math.atan(0.5) | |
local sin, cos = math.sin(rad), math.cos(rad) | |
local pv = math.sqrt(5) * 0.5 * 0.25 | |
local rg = { | |
{ -pv * cos + pv * sin + 0.5, -pv * sin - pv * cos + 0.5 }, | |
{ pv * cos + pv * sin + 0.5, pv * sin - pv * cos + 0.5 }, | |
{ pv * cos - pv * sin + 0.5, pv * sin + pv * cos + 0.5 }, | |
{ -pv * cos - pv * sin + 0.5, -pv * sin + pv * cos + 0.5 } | |
} | |
for rx = left, right do | |
for ry = top, bottom do | |
local c = 0 | |
for pi = 1, #rg do | |
local x, y = rx + rg[pi][1], ry + rg[pi][2] | |
local hit = false | |
local j = ps[#ps] | |
for ii = 1, #ps do | |
local i = ps[ii] | |
if ((i.y > y) ~= (j.y > y)) and (x < i.x + (j.x - i.x) * (y - i.y) / (j.y - i.y)) then | |
hit = not hit | |
end | |
j = i | |
end | |
c = c + (hit and 1 or 0) | |
end | |
screen.setColor(r, g, b, a * c * 0.25) | |
screen.drawRect(rx, ry, 1, 1) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment