Log In  

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


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
4

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)

Mind if I use this in a project I'm working on?

P#116624 2022-08-30 22:37

Hi @ValerADHD.

It should be no difficult feat to make an inverted circle that does not require vectors or rectangles if CPU usage is not a consideration.

It doesn't go above 30.22% in any case.

Cart #sapatofujo-0 | 2022-08-31 | Code ▽ | Embed ▽ | No License

Use LEFT and RIGHT arrow keys to vanish or appear the picture behind it.

. . .

Cart #someyubafo-0 | 2022-08-31 | Code ▽ | Embed ▽ | No License

Here is a faster version using rectangles ^^
The CPU does not go above 19%.

. . .

Cart #fupujefaso-0 | 2022-08-31 | Code ▽ | Embed ▽ | No License

Here it is ^^ not manually redrawing the rectangles every time, decreasing the CPU usage to 13%.

. . .

There should be a way to get this even faster.

Cart #gejemodafi-0 | 2022-09-01 | Code ▽ | Embed ▽ | No License

Ah, even using memory transfer, it is spr() that is slowing me down and not much else. This method ^^ uses 18% CPU.
And chomps up memory 0x8000-0xBFFF besides.

. . .

I think what we really need is invcircfill(x1,y1,x2,y2,x3,y3,r,c)
Where x1,y1 are top-left of the rectangle.
Where x2,y2 are bottom-right of the rectangle.
Where x3,y3 is the transparent circle location.
Where r,c is radius and color.

P#116658 2022-08-31 14:59 ( Edited 2022-09-01 01:04)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 13:35:54 | 0.028s | Q:28