help needed with if- else cases

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pereges

    #1

    help needed with if- else cases

    in my program i have a piece of code like this -

    int left_count, right_count;
    left_count = right_count = 0;
    for all triangles
    {

    a = condition that x coordinates of all vertices are <= split.x
    b = condition that x coordinates of all vertices that are >= split.x

    if(a is true)
    left_count++;
    else
    if(b is true)
    right_count++;
    else
    if(!a && !b)
    {
    left_count++;
    right_count++;
    }

    }

    Now with the above code, I get left_count as 1200 and right_count 619

    When I did not have the else if(!a && !b), I was getting 581 and 581
    which is 1162 i.e. triangles which are only in left box and only in
    right box . Not the ones that belong to both left and right boxes. So
    that means triangles which are on both sides totals to about 38. I
    believe 619 for right_count is correct but theres something fishy
    about lefT_count which should also be 619 or so in my opinion. Prior
    to changing the code in the above manner i had if cases like below -

    if(a is true)
    left_count++;

    if(b is true)
    right_count++;

    else
    {
    left_count++;
    right_count++;
    }

    And I was getting left_count and right_count as 1200 and 1200 which
    was quite surprising to see.


    **actual code **

    for(i=0; i<(*kd)->maxtriangles ; i++)
    {

    a = (*kd)->v[(*kd)->t[i].v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
    >t[i].v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t[i].v2].x <= (*kd)-
    >split.x;
    b = (*kd)->v[(*kd)->t[i].v0].x <=(*kd)->split.x && (*kd)->v[(*kd)-
    >t[i].v1].x <= (*kd)->split.x &&(*kd)->v[(*kd)->t[i].v2].x <= (*kd)-
    >split.x;
    if(a)
    left_count++;
    if(b)
    right_count++;
    else
    if(!a && !b)
    {
    left_count++;
    right_count++;
    }

    }
  • Ben Bacarisse

    #2
    Re: help needed with if- else cases

    pereges <Broli00@gmail. comwrites:
    <snip>
    a = condition that x coordinates of all vertices are <= split.x
    b = condition that x coordinates of all vertices that are >= split.x
    >
    if(a is true)
    left_count++;
    else
    if(b is true)
    right_count++;
    else
    if(!a && !b)
    {
    left_count++;
    right_count++;
    }
    <snip>
    if(a is true)
    left_count++;
    >
    if(b is true)
    right_count++;
    >
    else
    {
    left_count++;
    right_count++;
    }
    >
    And I was getting left_count and right_count as 1200 and 1200 which
    was quite surprising to see.
    The two patterns are quite different. The "else" applies only to
    nearest "if" so in your second case, when a is false right_count is
    always incremented since the if ... else increments it in both arms
    regardless of the value of b.

    You don't need the !a && !b test in your first version because it is
    guaranteed to be true at that point in the code:

    if (a)
    left_count++;
    else if (b)
    right_count++;
    else {
    left_count++;
    right_count++;
    }

    is the same.

    --
    Ben.

    Comment

    Working...