473,320 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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++;
}

}
Apr 9 '08 #1
1 1309
pereges <Br*****@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.
Apr 9 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
3
by: coolsti | last post by:
Can someone help me enhance this code snippet which works only for IE so that it will also work for Firefox? I have been trying all sorts of things and have gotten to where I can capture the...
10
by: Joseph S. | last post by:
Hi, How do I pass a string from one call to a php block to another call in the same page but from another form? Here's my full php code: I'm using two forms in the same page. A hidden field...
4
by: Terencetrent | last post by:
I having been using Access '97/2002 for about 4 years now and have never really had the need or the time to learn visual basic. Well, I think the time has finally come. I need help with Visual...
11
by: my-wings | last post by:
I think I've painted myself into a corner, and I'm hoping someone can help me out. I have a table of books (tblBooks), which includes a field (strPubName) for Publisher Name and another field...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
31
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.