Draw hollow circle / doughnut

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
kexisse
Citizen
Posts: 56
Joined: Wed Jun 13, 2012 2:52 pm

Draw hollow circle / doughnut

Post by kexisse »

I thought this might be possible with setStencil but it doesn't seem to make sense for hollow stuff.

How can I draw a hollow circle / doughnut shape?
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Draw hollow circle / doughnut

Post by Ref »

kexisse wrote:I thought this might be possible with setStencil but it doesn't seem to make sense for hollow stuff.

How can I draw a hollow circle / doughnut shape?
You can do it using a series of segments & polygon.

Code: Select all

function arc_seg()
    local acx, acy    = mask.width/2, mask.height/2    -- arc center
    local rado, radi = 1.5 * mask.size, mask.size          -- outer & inner radius
    local arco, arci  = {}, {}                                         -- tables of arc points
    local arcs, arce  = mask.angle, mask.angle + mask.circle	-- start & ending angle
    local idx            = mask.size / 4                            -- steps in arc
    local val            = 1
    for i = arcs, arce,pi / idx do
         local xo       = rado * math.cos(i) + acx
         local yo       = rado * math.sin(i) + acy
         local xi        = radi * math.cos(i) + acx
         local yi        = radi * math.sin(i) + acy
         arco[val]      = { xo, yo }
         arci[val]       = { xi, yi }
         val               = val + 1
        end
     for i = 1, #arco - 1 do        -- polygon can't handle all the points at once
         gr.polygon('fill',
            arco[i][1],    arco[i][2],    arco[i+1][1],    arco[i+1][2],
            arci[i+1][1], arci[i+1][2], arci[i][1],	         arci[i][2])
            end
    end
This is the code I used for demo script 'mask_with_are.love
Press <v> to get complete hollow circle.
Edit: OK! Way too complex. Don't need canvases, pixel effect, or stencils. Just use draw!
Attachments
lifesaver.love
Simple hollow shapes
(205.22 KiB) Downloaded 166 times
mask_with_arc.love
Circular mask
(58.51 KiB) Downloaded 152 times
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Draw hollow circle / doughnut

Post by Ref »

Just trying things out.
Tried to make a doughnut shape have a pseudo 3D apperance.
Nothing optimized - used canvas & pixel effect.
Same effect can obviously be done with mapPixel and no caanvas.
<a><s> alters effect & <q><w> changes object size.
Attachments
peppermint_lifesavor.love
pseudo 3D shape
(205.44 KiB) Downloaded 168 times
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: Draw hollow circle / doughnut

Post by felix24 »

have you tried using a png image?
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Draw hollow circle / doughnut

Post by Ref »

felix24 wrote:have you tried using a png image?
Hi felix!
Not sure to whom you are replying.
The demo I supplied (peppermint_lifesaver) can be used with a png file in place of creating a canvas.
The pixel effect works just as well.
It punches a hole in the center of the image and applies shading effects to the remaining portion of the image (see attached demo).
Attachments
star.love
pixel effect to punch hole in a png image
(219.88 KiB) Downloaded 167 times
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Draw hollow circle / doughnut

Post by BlackBulletIV »

I'm not quite sure if I'm understanding the problem correctly, but it sounds like you could make a donut shape by using code like this (note that I haven't tested it):

Code: Select all

love.graphics.setLineWidth(width)
love.graphics.circle("line", x, y, radius, segments)
Replace x, y, and radius with their appropriate values, make sure to replace segments with a value that ensures the donut looks smooth enough, and replace width with how wide you want the "solid part" of the donut to be.

To save performance, you may also want to draw the shape to a canvas and draw that each frame.
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: Draw hollow circle / doughnut

Post by felix24 »

Ref wrote:
felix24 wrote:have you tried using a png image?
Hi felix!
Not sure to whom you are replying.
The demo I supplied (peppermint_lifesaver) can be used with a png file in place of creating a canvas.
The pixel effect works just as well.
It punches a hole in the center of the image and applies shading effects to the remaining portion of the image (see attached demo).
hey man, i was replying to
kexisse wrote:How can I draw a hollow circle / doughnut shape?
^^
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Draw hollow circle / doughnut

Post by Kadoba »

Can't you do this with an inverted stencil?
doughnut.love
(242 Bytes) Downloaded 164 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Draw hollow circle / doughnut

Post by kikito »

Modifying the line width and then drawing a "line" circle is considered cheating, right?

Code: Select all

-- draw a doughnut of internal radius 10, external radius 40, centered on 100, 100
love.graphics.setLineWidth(20)
love.graphics.circle("line", 100, 100, 30)
When I write def I mean function.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Draw hollow circle / doughnut

Post by Ref »

Boy!
We sure have beat this one to death!
Seems like the only one not too interested in it is Kexisse.
Felix's idea seems best for static textured holely objects while line width - circle for procedurally generated plain objects.
Couldn't resist another cobbed up demo.
Attachments
falling_lifesavers.love
Take not credit for original
(27.81 KiB) Downloaded 139 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 16 guests