Golf linefill

This commit is contained in:
Pyrex 2022-12-22 18:30:05 -08:00
parent d782f6eb14
commit f3a0b04cc4

View File

@ -53,21 +53,19 @@ function linefill(ax,ay,bx,by,r,c)
local dx,dy=bx-ax,by-ay local dx,dy=bx-ax,by-ay
-- avoid overflow -- avoid overflow
-- credits: https://www.lexaloffle.com/bbs/?tid=28999 -- credits: https://www.lexaloffle.com/bbs/?tid=28999
local d=max(abs(dx),abs(dy)) local n,d=_mnmx(abs(dx),abs(dy))
local n=min(abs(dx),abs(dy))/d n/=d
d*=sqrt(n*n+1) d*=sqrt(n*n+1)
if(d<0.001) return if(d<0.001) return
local ca,sa=dx/d,-dy/d local ca,sa=dx/d,-dy/d
-- polygon points -- polygon points
local s={ local s={{0,-r},{d,-r},{d,r},{0,r}}
{0,-r},{d,-r},{d,r},{0,r} local spans={}
} local function calcxy(u,v) return ax+u*ca+v*sa,ay-u*sa+v*ca end
local u,v,spans=s[4][1],s[4][2],{} local x0,y0=calcxy(0,r)
local x0,y0=ax+u*ca+v*sa,ay-u*sa+v*ca
for i=1,4 do for i=1,4 do
local u,v=s[i][1],s[i][2] local x1,y1=calcxy(unpack(s[i]))
local x1,y1=ax+u*ca+v*sa,ay-u*sa+v*ca
local _x1,_y1=x1,y1 local _x1,_y1=x1,y1
if(y0>y1) x0,y0,x1,y1=x1,y1,x0,y0 if(y0>y1) x0,y0,x1,y1=x1,y1,x0,y0
local dx=(x1-x0)/(y1-y0) local dx=(x1-x0)/(y1-y0)
@ -78,11 +76,8 @@ function linefill(ax,ay,bx,by,r,c)
for y=y0\1+1,min(y1\1,127) do for y=y0\1+1,min(y1\1,127) do
-- open span? -- open span?
local span=spans[y] local span=spans[y]
if span then if (span) rectfill(x0,y,span,y)
rectfill(x0,y,span,y) spans[y]=x0
else
spans[y]=x0
end
x0+=dx x0+=dx
end end
x0,y0=_x1,_y1 x0,y0=_x1,_y1