if statement condition

Hi There,

This is a loop I am using, which is part of a bigger code. The if statements in the loop are used to separate points on the screen into respective 16 screen rectangles. The prblem here is that all points are accumulated in the first rectangle, whereas they are supposed to be dispersed according to their x, y coordinates into the 16 rectangles. I am using code::blocks with Win XP. Here is the code:




for (int dd = 1; dd <= isoscelescounter ; dd = dd + 1)
{

if ( 0 < rightisosceles [dd][0][0] <= 11 && 0 < rightisosceles [dd][0][1] <= 18.5)
{isocounterbox1[isobox1] = dd; isobox1 = isobox1 + 1;}

else if ( 11 < rightisosceles [dd][0][0] <= 22 && 0 < rightisosceles [dd][0][1] <= 18.5)
{isocounterbox2[isobox2] = dd; isobox2 = isobox2 + 1;}

else if ( 22 < rightisosceles [dd][0][0] <= 33 && 0 < rightisosceles [dd][0][1] <= 18.5)
{isocounterbox3[isobox3] = dd; isobox3 = isobox3 + 1;}

else if ( 33 < rightisosceles [dd][0][0] <= 44 && 0 < rightisosceles [dd][0][1] <= 18.5)
{isocounterbox4[isobox4] = dd; isobox4 = isobox4 + 1;}

else if ( 0 < rightisosceles [dd][0][0] <= 11 && 18.5 < rightisosceles [dd][0][1] <= 37)
{isocounterbox5[isobox5] = dd; isobox5 = isobox5 + 1;}

else if ( 11 < rightisosceles [dd][0][0] <= 22 && 18.5 < rightisosceles [dd][0][1] <= 37)
{isocounterbox6[isobox6] = dd; isobox6 = isobox6 + 1;}

else if ( 22 < rightisosceles [dd][0][0] <= 33 && 18.5 < rightisosceles [dd][0][1] <= 37)
{isocounterbox7[isobox7] = dd; isobox7 = isobox7 + 1;}

else if ( 33 < rightisosceles [dd][0][0] <= 44 && 18.5 < rightisosceles [dd][0][1] <= 37)
{isocounterbox8[isobox8] = dd; isobox8 = isobox8 + 1;}

else if ( 0 < rightisosceles [dd][0][0] <= 11 && 37 < rightisosceles [dd][0][1] <= 55.5)
{isocounterbox9[isobox9] = dd; isobox9 = isobox9 + 1;}

else if ( 11 < rightisosceles [dd][0][0] <= 22 && 37 < rightisosceles [dd][0][1] <= 55.5)
{isocounterbox10[isobox10] = dd; isobox10 = isobox10 + 1;}

else if ( 22 < rightisosceles [dd][0][0] <= 33 && 37 < rightisosceles [dd][0][1] <= 55.5)
{isocounterbox11[isobox11] = dd; isobox11 = isobox11 + 1;}

else if ( 33 < rightisosceles [dd][0][0] <= 44 && 37 < rightisosceles [dd][0][1] <= 55.5)
{isocounterbox12[isobox12] = dd; isobox12 = isobox12 + 1;}

else if ( 0 < rightisosceles [dd][0][0] <= 11 && 55.5 < rightisosceles [dd][0][1] <= 74)
{isocounterbox13[isobox13] = dd; isobox13 = isobox13 + 1;}

else if ( 11 < rightisosceles [dd][0][0] <= 22 && 55.5 < rightisosceles [dd][0][1] <= 74)
{isocounterbox14[isobox14] = dd; isobox14 = isobox14 + 1;}

else if ( 22 < rightisosceles [dd][0][0] <= 33 && 55.5 < rightisosceles [dd][0][1] <= 74)
{isocounterbox15[isobox15] = dd; isobox15 = isobox15 + 1;}

else if ( 33 < rightisosceles [dd][0][0] <= 44 && 55.5 < rightisosceles [dd][0][1] <= 74)
{isocounterbox16[isobox16] = dd; isobox16 = isobox16 + 1;}

cout<< rightisosceles [dd][0][1] << endl;

}



Any help is greatly appreciated.

if(0 < rightisosceles [dd][0][0] <= 11)

This does not work like you think. It will always result in true because it does this:

if((0 < rightisosceles [dd][0][0]) <= 11)

The computer will evaluate 0 < x first, so you end up with one of these:

1
2
3
if(true <= 11)
//or
if(false <= 11)


since true and false are both less than 11, the final result is true.

What you probably want to do is this:

if((0 < rightisosceles [dd][0][0]) && (rightisosceles [dd][0][0] <= 11))

But good god, this many &&s and if statements makes me want to scream. There must be a better way to do this...

It doesn't help that you numerically named your variables instead of using an array (isobox1, isobox2, isobox3, etc instead of just making an array named isobox)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * I don't use code::blocks, but are you looking for something like this?
 * (You can set the values in Rect's static fields based on the real width and height.)
 */

struct Rect {
   int x, y;

   static const int
      WIDTH = 44,
      HEIGHT = 74,
      X_SEC = 4,
      Y_SEC = 4;
};

Rect getRect(int pixel_x, int pixel_y) {
   Rect where = {0, 0};
   for( int x_num = 1; num <= Rect::X_SEC; ++x_num )
      if( pixel_x <= x_num * Rect::WIDTH / Rect::X_SEC ) {
         where.x = x_num - 1;
         break;
      }
   for( int y_num = 1; num <= Rect::Y_SEC; ++y_num )
      if( pixel_y <= y_num * Rect::HEIGHT / Rect::Y_SEC ) {
         where.y = y_num - 1;
         break;
      }
   return where;
}


1
2
3
4
5
6
7
8
9
//Also, this is not right.
0 < rightisosceles [dd][0][0] <= 11
//remeber that (a < b) evaluates to a bool, so this is really:
0 < a //return some bool b
b <= 11 //this doesn't make sence to a compiler

//what you wanted was
0 < a && a <= 11
//Sorry, no shortcuts (compound inequalities...) 
Thank you very much Disch and Mathhead200..this worked...greatly appreciated..
Topic archived. No new replies allowed.