#Possible optimization of 8 if-else branches?

6 messages · Page 1 of 1 (latest)

prisma atlas
#

Is there a better way to write and possibly optimize this?

// a and b are objects with a shape attribute which is an enum value
// the shape enum value is either a circle or polygon

if (a->shape == CIRCLE && b->shape == CIRCLE)
    collide_circle_x_circle(a, b);

else if (a->shape == CIRCLE && b->shape == POLYGON)
    collide_polygon_x_circle(b, a);

else if (a->shape == POLYGON && b->shape == CIRCLE)
    collide_polygon_x_circle(a, b);

else if (a->shape == POLYGON && b->shape == POLYGON)
    collide_polygon_x_polygon(a, b);

// these last 4 if-else branches are controlled if collision happened
if (collision) {
    if (a->shape == CIRCLE && b->shape == CIRCLE)
        contact_circle_x_circle(a, b);

    else if (a->shape == CIRCLE && b->shape == POLYGON)
        contact_polygon_x_circle(b, a);

    else if (a->shape == POLYGON && b->shape == CIRCLE)
        contact_polygon_x_circle(a, b);

    else if (a->shape == POLYGON && b->shape == POLYGON)
        contact_polygon_x_polygon(a, b);
wooden vaporBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

polar bronze
#

You can use nested if

#
if(a->shape==CIRCLE)
{
    if(b->shape==CIRCLE)
        collide_circle_x_circle(a,b);
    else if(b->shape==POLYGON)
        collide_polygon_x_circle(b,a);
}
fringe veldt
#

switch

wooden vaporBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.