Aleksandr Albert
Aleksandr Albert

Reputation: 1877

Nested for loop not iterating properly

Guys i have a really weird problem. I am trying to implement a nested for loop which splits up a rectangle into smaller blocks and then checks to see which of these smaller blocks have points from an array i have defined in them(big rectangle is 320*240 btw):

        int[,] edgetrunc = new int[edgeIndex, 2]; //edgeIndex is the number of points in the external array 
        Array.Copy(edgePoint, edgetrunc, edgeIndex*2);//truncates the nulls from the tail of my array
        int delx = 16;//width of block
        int dely = 12;//height of block

        int blockIndex = new int();
        bool[] block = new bool[(depthFrame.Width/delx)*(depthFrame.Height/dely)];

        for (int ymin = 0;ymin < depthFrame.Height;ymin += dely)
        {
            for (int xmin = 0; xmin < depthFrame.Width; xmin += delx)
            {
                blockIndex = (xmin / delx) + (ymin / dely);
                for (int i = 0; i < edgeIndex; i++)
                {
                    if (edgetrunc[i, 0] >= xmin && edgetrunc[i, 0] < xmin++ && edgetrunc[i, 1] >= ymin && edgetrunc[i, 1] < ymin++)
                    {
                        block[blockIndex] = true;
                        break;
                    }
                }
            }
        }

heres the problem though, i put a breakpoint on the second for loop(the xmin loop) and started to iterate it, and the value of xmin jumped from 0 to 320 on the first iteration, and after that stayed there and ymin alone changed on each iteration. Am i going cray? what did i do wrong?

PS i even tested this and i got the same problem:

    for (int ymin = 0;ymin < depthFrame.Height;ymin += dely)
    {
        for (int xmin = 0; xmin < depthFrame.Width; xmin += delx)
        {

        }
    }

EDIT:

figured it out, it was strange, apparantly it had to do with the way i was trying to find block index. To fix it, i initialized blockIndex to 0 outside the for loops and put blockIndex++ after the 3rd for loop, thanks for the help anyways =)

        int blockIndex = 0;
        bool[] block = new bool[(depthFrame.Width/delx)*(depthFrame.Height/dely)];

        for (int ymin = 0;ymin < depthFrame.Height;ymin += dely)
        {
            for (int xmin = 0; xmin < depthFrame.Width; xmin += delx)
            {
                for (int i = 0; i < edgeIndex; i++)
                {
                    if ((edgetrunc[i, 0] >= xmin) && (edgetrunc[i, 0] < (xmin + delx)) && (edgetrunc[i, 1] >= ymin) && (edgetrunc[i, 1] < (ymin + dely)))
                    {
                        block[blockIndex] = true;
                        break;
                    }
                }
                blockIndex++;
            }
        }

Upvotes: 0

Views: 834

Answers (3)

paxdiablo
paxdiablo

Reputation: 881423

The reason it's jumping up like that is because you have xmin++ in the if statement inside your inner loop.

This could quite easily drive the value of xmin up quickly, depending on the value of edgeIndex.

x++ increments x then uses its old value. I think what you need is simply x+1 which uses the value plus one, without changing the value.

Ditto for the ymin++ in there as well.

In other words, it probably should be:

if (edgetrunc[i, 0] >= xmin && edgetrunc[i, 0] < xmin + 1 &&
    edgetrunc[i, 1] >= ymin && edgetrunc[i, 1] < ymin + 1)

Upvotes: 0

Oleksi
Oleksi

Reputation: 13097

On your first inner loop iteration, the following gets run:

xmin++

as part of the if statement in the innermost loop. This gets executed 320 times, hence your value. One the second iteration,

edgetrunc[i, 0] >= xmin

will be false, and so the xmin++ line will not be executed. In general, you should avoid using mutation statements like xmin++ inside if statements. Refactor that out, and see if it fixes your problem

Upvotes: 0

Ian Mercer
Ian Mercer

Reputation: 39277

Instead of using xmin++ and ymin++ I think you probably meant to use xmin+1 and ymin+1. ++ will change the value in the variable.

Upvotes: 2

Related Questions