Filled circular sector
Polybios

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

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

Polybios

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
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.

Polybios

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

Yes. Fixed now.

Kibiz0r

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

jmasterx

al_draw_sector
al_draw_pie_sector
al_draw_circle_sector

are these good names?

Arthur Kalliokoski

I'd call it al_pie_slice().

SiegeLord
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.

Steve Terry

mmmmmmmmmmmmmmmmmm pie

Polybios

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

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.

Kibiz0r

I'd call it al_pacman().

Thread #605684. Printed from Allegro.cc