generating a pie chart
shadyvillian

I'm trying to generate a pie chart using only allegro 5 primitives. Each "slice" of the pie is a different color. I was going to make triangles for each slice then put a circle behind it to make it look rounded. But I'm having trouble calculating the x and y positions of the triangles.

J-Gamer

I assume your slices store the percentage they have. So you can iterate over all the slices, multiply the percentage by 2*PI and you have the angle range. Then, just iterate over the slices and keep adding each range while using sin/cos*radius to get the y/x coordinates.

james_lohr

I was going to make triangles for each slice then put a circle behind it to make it look rounded

I don't see how this could work, unless you're planning to have a very bumpy pie.

SiegeLord

Untested:

float percentages[] = {10, 20, 40, 30};
size_t num_pieces = 4;
float cur_theta = 0;
float cx = 200, cy = 200, r = 100;
ALLEGRO_COLOR colors[] = {...}; 

for(size_t ii = 0; ii < num_pieces; ii++)
{
    float delta_theta = percentages[ii] / 100.0 * 2.0 * ALLEGRO_PI;
    al_draw_arc(cx, cy, r / 2, cur_theta, delta_theta, color[ii], r / 2);
    cur_theta += delta_theta;
}

You can also swipe the al_draw_filled_pieslice from the 5.1 branch, which you would use instead of the al_draw_arc. One bonus of using al_draw_arc here, though, is that you can make those ring charts instead of simple pie charts.

shadyvillian
SiegeLord said:

You can also swipe the al_draw_filled_pieslice from the 5.1 branch

Wheres the 5.1 branch?

EDIT: I think I found it but I'm guessing I have to build it myself.

SiegeLord

Well, the question to ask is why my solution isn't acceptable for you. It's only issue is that it draws 2x more polygons than it should. Either way, instead of building the entire 5.1 I'd just go here and copy-paste al_draw_filled_pieslice for your own use.

shadyvillian

Oh, I was just curious. I'm testing it right now, but theres a bug my data analyzer so i need to get that working right to see what my results are.

EDIT: ok I got it working everything looks good.
EDIT2: would anti-aliasing help the edge of the circle look smoother?

Thread #608432. Printed from Allegro.cc