0

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

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

that didnt work unfortunately, please see the update in my question.
0

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

1 Comment

i tried that but the problem is still there =( I'm so totally lost. i even commented out the third loop as shown in my edited question and it still screws up.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.