Log In  

Cart #invertedcircle-1 | 2020-11-05 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

I noticed that people relatively often ask for a way to draw an inverted circle and don't have an easy way to implement it, since it requires a bit of math that can be difficult to derive without experience. I decided to make a quick cart and standalone function to do that! This isn't amazingly efficient, and it only does circles (not ellipses), but it is a drop in function:
Update: new function utilizing trig, faster and now subpixel!

x=x or 64
    y=y or 64
    r=r or 32
    left=left or 0
    right=right or 128
    top=top or 0
    bottom=bottom or 128
    local p=r*6.28
    if(top<y-r)rectfill(left,top,right,y-r,c)
    if(bottom>y+r)rectfill(left,y+r,right,bottom,c)
    for d=-.001,.25,1/p do
        local ex,ey=r*cos(d),r*sin(d)
        if(x-ex>left) then
            if(y-ey>top) then
                rect(left,y-ey,x-ex,y-ey,c)
            end
            if(y+ey<bottom) then
                rect(left,y+ey,x-ex,y+ey,c)
            end
        end
        if(x+ex<right) then
            if(y-ey>top) then
                rect(x+ex,y-ey,right,y-ey,c)
            end
            if(y+ey<bottom) then
                rect(x+ex,y+ey,right,y+ey,c)
            end
        end
    end

Old versions:


Cart #invertedcircle-0 | 2020-11-05 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
1

function invertedcircle(x,y,r,left,right,top,bottom,c)
    x=x or 64
    y=y or 64
    r=r or 32
    left=left or 0
    right=right or 128
    top=top or 0
    bottom=bottom or 128

    local rsqr=r*r
    for i=flr(top),bottom do
        local yr=i-y
        local w=max(0,(rsqr-yr*yr)^.5)
        if(x-w>left)rect(left,i,x-w,i,c)
        if(x+w<right)rect(right,i,x+w,i,c)
    end
end

P#83816 2020-11-05 20:16 ( Edited 2020-11-05 21:17)


[Please log in to post a comment]

Follow Lexaloffle:        
Generated 2022-05-04 14:27:41 | 0.006s | Q:15