Nazar
Nazar

Reputation: 880

condition statement executes when conditions are not met

I can not figure out where the bug is in the condition statement:

xo=130, y0=160, x=180, y=210
for(row = 0; row < 480; row++){
    for(col = 0; col < 640; col++){    
         if((col>xo) && (col<(xo+x)) && (row>=yo) && (row<(yo+y))){//do something;}
    }
}

In my particular situation, I want the if statement to execute only when I am withing the window defined by top left [xo+1,yo] and bottom right [x,y]. However, the statement executes even after col is greater then xo+x. I also tried single & operator but it did not help. Is something wrong with my condition statement?

Upvotes: 0

Views: 41

Answers (1)

user6902061
user6902061

Reputation:

If I understand correctly, you want to be within a window defined by the two points [xo,yo] and [x,y].

Let [xo,yo] = [130,160] and [x,y] = [180,210].

You want to go 50 steps on the x-coordinates, and 50 steps on the y-coordinates. What your code effectively does is go from [xo,yo] to [xo+x,yo+y], which would translate to [130+180,160+210] = [320,370]. This goes beyond the point defined by [x,y].

Upvotes: 2

Related Questions