Question

screenshot of debug: http://img1.uploadscreenshot.com/images/orig/12/36121481470-orig.jpg

note how x, y have values (i have no idea why x and y stopped on 69 in the for loop - x should've went up to 86 and y to 183) yet r has no value at all. (the variable doesn't exist? what?) how should I fix this?

code if you want to read:

        public float[] cht(int[,] matrix)
    {
        float[] xyrd = new float[4];
        int xthreshold, ythreshold;
        float slope;
        double dir;
        float zone;
        int[] limitsStorage = new int[3] { matrix.GetLength(0), matrix.GetLength(1), matrix.GetLength(0) / 2 - 10 };
        short[,,] accumulator = new short[limitsStorage[0]+1, limitsStorage[1]+1,limitsStorage[2]+1];
        for (int x = 0; x < limitsStorage[0]; x++)
        { //first dimension loop of matrix 100
            for (int y = 0; y < limitsStorage[1]; y++)
            { //second dimension loop of matrix 120
                if (matrix[x, y] == 225)
                {//the data at the for loop location is a 1 and not 0 hit.
                    xthreshold = x - limitsStorage[0] / 2;
                    ythreshold = y - limitsStorage[1] / 2;
                    //forget angle, search slope: float angle = xthreshold > 0 ? ((float)Math.Atan2(xthreshold, ythreshold)) : ((float)Math.Atan2(xthreshold, ythreshold) + 180);
                    slope = xthreshold / ythreshold;
                    //initiate if loops.
                    dir = 180 + Math.Round(Math.Atan2(ythreshold, xthreshold) * 57.2957 / 45, 0) * 45 + 45 * (Math.Round(((Math.Atan2(ythreshold, xthreshold) * 57.2957) % 45) / 45));
                    if (slope > .404 || slope < -.404)
                    {
                        if (slope < 2.3558 || slope > -2.3558)
                        {
                            if (xthreshold > 0)
                            {
                                if (ythreshold > 0)
                                {
                                    //+x+y zone
                                    zone = 45 + 180;
                                }
                                else
                                {
                                    //+x-y zone
                                    zone = 180 - 45;
                                }
                            }
                            else
                            {
                                if (ythreshold > 0)
                                {
                                    //-x+y zone
                                    zone = 360 - 45;
                                }
                                else
                                {
                                    //-x-y zone
                                    zone = 45;
                                }
                            }
                        }
                        else if (ythreshold > 0)
                        {
                            //+y zone
                            zone = 360 - 90;
                        }
                        else
                        {
                            //-y zone
                            zone = 90;
                        }
                    }
                    else if (xthreshold > 0)
                    {
                        //+x zone
                        zone = 180;
                    }
                    else
                    {
                        //-x zone
                        zone = 0;
                    }
                    for (int R = 6; R < limitsStorage[2]; R++)
                    { //Radius loop for scan 44
                        float delta = (float)((1 / R) * 57.2957);
                        for (float Theta = zone - 25; Theta < zone + 25; Theta += delta)
                        {
                            accumulator[(int)(((R * Math.Cos(Theta / 57.2957)) < 0 || (R * Math.Cos(Theta / 57.2957)) > limitsStorage[0]) ? 0 : R * Math.Cos(Theta / 57.2957)), (int)(((R * Math.Sin(Theta / 57.2957)) < 0 || (R * Math.Sin(Theta / 57.2957)) > limitsStorage[1]) ? 0 : R * Math.Sin(Theta / 57.2957)),R]++;
                            //btw, 0,0,R is always the non-center area.
                        }
                    }

                }
            }
        }

        for (int x = 1; x < limitsStorage[0]; x++)
        {
            for (int y = 1; y < limitsStorage[1]; y++)
            {
                for (int r = 6; r < limitsStorage[2]; r++)
                {
                    if (xyrd[3] > accumulator[x, y, r])
                    {
                        xyrd[0] = x;
                        xyrd[1] = y;
                        xyrd[2] = r;
                        xyrd[3] = accumulator[x, y, r];
                    }
                }
            }
        }

        if (accPrint)
        {
            //do something for debugging?
            accPrint = false;
        }
        return xyrd;
    }
Was it helpful?

Solution

I just noticed that the x and y have the little lock symbol under them indicating that you have private variables named x and y in the class in which this method is executing. Those are the x and y that you are seeing in the debugger.

r is appropriately out of scope as you've exited the loop in which it is declared.

By the way, x and y are ridiculously bad member variable names, and are ridiculously bad names for for loop variables of type int, especially if they are contained in a class with member variables named x and y.

OTHER TIPS

The only place you declare r is in the for statement, right? That means r goes out of scope as soon as the loop ends. So naturally if you inspect variables at he end of the function, r won't be there.

Confessing I don't know why x and y are in scope based on the comments. They could be class variables, but the asker says not. That's the only explanation I can think of, though.

The behaviour is not weird -- you actually get exactly what you expect.

Please note that the watch window can only accurately show you values that are in scope at the breakpoint.

At the highlighted breakpoint, only accumulator[x, y, r] is in scope, and you see exactly the values you expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top