Pie charts! But donuts are pretty tasty too.

So i like mathy things, but watching numbers gets boring really fast…
So why not look at some nice pie charts! Or donuts?

http://i.imgur.com/paR0ebe.jpg (I can’t get images working at all, this forum has some weird markdown I’ve never seen…)

Anyways, I thought I’d share this because i can’t be the only one seeing some use in this.
It’s using meshes so its pretty efficient in drawing too.

https://github.com/Luxizzle/Donut

Look at “Example.lua” to see how it works, but its fairly simple. And i made it that way on purpose, nobody likes using complicated things!

nice job, thanks for sharing!

@Luxizzle - you might find curves easier (and smoother) to draw with shaders

The demo below just draws one slice. You can set up your pie slices as you do at the moment, then just include the shader settings (see setup below)

function setup()
    inner,outer=200,300 --radii for donut
    local w,h=WIDTH/2,HEIGHT/2
    --a dummy pie slice
    m=mesh()
    m.vertices={vec2(w,h),vec2(w+outer,h+outer),vec2(w,h+outer)}
    m:setColors(color(255,255,0))
    m.shader=shader(DonutShader.v,DonutShader.f)
    m.shader.innerRadius=inner
    m.shader.outerRadius=outer
    m.shader.centrePos=vec2(w,h)
    --m.shader=nil
end

function draw()
    background(120)
    m:draw()
end

DonutShader = {
v = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
varying lowp vec4 vColor;
varying highp vec2 vPosition;
void main()
{
    vColor=color;
    vPosition=position.xy;
    gl_Position = modelViewProjection * position;
}]],
f = [[
precision highp float;
uniform vec2 centrePos;
uniform float innerRadius;
uniform float outerRadius;
varying lowp vec4 vColor;
varying highp vec2 vPosition;
void main()
{
    float d=distance(vPosition,centrePos);
    if ((d>outerRadius) || (d<innerRadius)) discard;
    else gl_FragColor = vColor;
}

]]}

@Ignatz I honestly haven’t even come close to touching shaders yet, it all looks like crazy wizardry to me.

@Luxizzle - I was terrified of them too, until I found they are pretty easy to write, most of the time.

I wrote an ebook that might help, it’s here (ignore the login prompt)

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

@Luxizzle - +1 for @Ignatz’s eBook. It’s very simple, it’s very short, and it’ll snap a few key concepts into place right quick. He’s done a great job of understanding the frightened newcomer mindset and writing directly to it–I say as a former frigthened newcomer myself.

Lol, I was definitely a member of the frightened newcomer club not that long ago, with all this graphics stuff. And I still feel like an idiot, most days of the week.

The built-in arc shader would be good for this too, because you supply it with start and stop angle, which might be easier than calculating vectors for each segment.

@Ignatz Thanks! I’ll definitely take a look at it when i have the time!