Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Filled circular sector

This thread is locked; no one can reply to it. rss feed Print
Filled circular sector
Polybios
Member #12,293
October 2010

Hi guys,

how do I draw a filled circular sector aka pieslice with Allegro 4.9/5.0?

I suppose, I need some combination of al_calculate_arc and the low level primitives routines... But how exactly should I do this? ???

Any help is highly appreciated! :)

Arthur Kalliokoski
Second in Command
February 2005
avatar

Draw a line from center of circle to each end of the arc and floodfill?

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Polybios
Member #12,293
October 2010

That was my first thought, too, but there is no floodfill in Allegro 4.9...!

All the pixel-reading, which a floodfill would have to do, would be really slow on textures, I guess.

I should add, I need it to be quite ... fast! ::)

SiegeLord
Member #7,827
October 2006
avatar

Polybios said:

I suppose, I need some combination of al_calculate_arc and the low level primitives routines... But how exactly should I do this? ???

That is the proper way... it'd be something like this (created by mushing al_draw_filled_ellipse and al_draw_arc)(untested):

#SelectExpand
1void al_draw_filled_pieslice(float cx, float cy, float r, float start_theta, 2 float delta_theta, ALLEGRO_COLOR color) 3{ 4 ALLEGRO_VERTEX vertex_cache[ALLEGRO_VERTEX_CACHE_SIZE]; 5 int num_segments, ii; 6 7 num_segments = fabs(delta_theta / (2 * ALLEGRO_PI) * ALLEGRO_PRIM_QUALITY * sqrtf(r)); 8 9 if (num_segments < 2) 10 return; 11 12 if (num_segments >= ALLEGRO_VERTEX_CACHE_SIZE) { 13 num_segments = ALLEGRO_VERTEX_CACHE_SIZE - 1; 14 } 15 16 al_calculate_arc(&(vertex_cache[1].x), sizeof(ALLEGRO_VERTEX), cx, cy, r, r, start_theta, delta_theta, 0, num_segments); 17 vertex_cache[0].x = cx; vertex_cache[0].y = cy; 18 19 for (ii = 0; ii < num_segments + 1; ii++) { 20 vertex_cache[ii].color = color; 21 vertex_cache[ii].z = 0; 22 } 23 24 al_draw_prim(vertex_cache, 0, 0, 0, num_segments + 1, ALLEGRO_PRIM_TRIANGLE_FAN); 25}

You can also hack around it by using a thick outlined arc.

EDIT: Fixed the code a bit... still untested though.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Polybios
Member #12,293
October 2010

Oh thank you so much! :)

It seems that there is always a vertex drawn which is at 0,0; that means the pieslice is crudely extended to the left-upper corner of the screen.

Maybe you meant to pass "&(vertex_cache[1].x)" to al_calculate_arc?

SiegeLord
Member #7,827
October 2006
avatar

Yes. Fixed now.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Kibiz0r
Member #6,203
September 2005
avatar

I recall there was a request for this forever.5 ago, people started talking about what to call it, then the thread died.

jmasterx
Member #11,410
October 2009

al_draw_sector
al_draw_pie_sector
al_draw_circle_sector

are these good names?

Arthur Kalliokoski
Second in Command
February 2005
avatar

I'd call it al_pie_slice().

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

SiegeLord
Member #7,827
October 2006
avatar

Kibiz0r said:

I recall there was a request for this forever.5 ago, people started talking about what to call it, then the thread died.

The implementation for the outlined pieslice/sector/w/e is very non-trivial, so I haven't had the time to implement it.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Steve Terry
Member #1,989
March 2002
avatar

mmmmmmmmmmmmmmmmmm pie

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Polybios
Member #12,293
October 2010

Thanks again, SiegeLord! :)

One question though: Could you (or anyone else) explain why you take the square root of the radius there in order to calculate the number of segments?

SiegeLord
Member #7,827
October 2006
avatar

It's... complicated. I don't remember exactly how I came up with it. The last thing I remember is that it is an approximation to this formula:
<math>N = \frac{2 \pi}{\arccos (1 - 0.25 / r)}</math>
Which calculates the number of segments by limiting the length of the KB segment in the diagram on this page.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Kibiz0r
Member #6,203
September 2005
avatar

I'd call it al_pacman().

Go to: