|
generating a pie chart |
shadyvillian
Member #12,426
December 2010
|
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. Software Engineer by day, hacker by night. |
J-Gamer
Member #12,491
January 2011
|
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. " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
james_lohr
Member #1,947
February 2002
|
shadyvillian said: 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
Member #7,827
October 2006
|
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. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
shadyvillian
Member #12,426
December 2010
|
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. Software Engineer by day, hacker by night. |
SiegeLord
Member #7,827
October 2006
|
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. "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
shadyvillian
Member #12,426
December 2010
|
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. Software Engineer by day, hacker by night. |
|