Author Topic: Problem to solve an easy condition  (Read 4741 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1431
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Problem to solve an easy condition
« on: September 03, 2011 »
I have a small problem to solve normaly a very simple codition but i dont really get it work which makes me silly. Lets try to explain. Just say we have a 2 dimensional array like [12,12]...
This array is filled with 0... On some randomized [x,y] coordinates there will be filled with value 1.

With following workring loop i check the [x,y] array to get the first top left position of a field with 1:

Code: [Select]
                               selectedObject = 1;

                                for (int y = 0; y < 12; y++)
                                {
                                    for (int x = 0; x < 12; x++)
                                    {
                                        if (arraydata[x, y] == selectedObject)
                                        {
                                            startPos.X = x;
                                            startPos.Y = y;
                                            x = 13;
                                            y = 13;
                                        }
                                    }
                                }

Now i have the startPos.X and startPos.Y of the first top/left object. Now i will check if there are neighboars filled with 1 and if so, how many. I will get the startPos and endPos of all neighboars (horizontal and vertical). This is what makes me crazy, even its so easy. In basic i used for this a Repeat/Until loop and tried it in C#, where it does not works. Maybe my fault. However here is what i have tried:

Code: [Select]
                                do
                                {
                                    endPos.X = startPos.X;
                                    endPos.Y = startPos.Y + bla;
                                    bla++;
                                } while (arraydata[endPos.X, endPos.Y] != selectedObject);

I have tried some other codes too..  like..

Code: [Select]
                                while (arraydata[endPos.X, startPos.Y] != selectedObject)
                                {
                                    endPos.X = startPos.X + tempX;
                                    tempX++;
                                }

But at least i dont get the correct results for the endPos, while startPos works fine!

Code: [Select]
000000000000
010010000000
010000000000
010000100000
010000000000
010000000000
000000000000
010001001000
000000000000

Should return for vertical scan:
startPos.X = 1
startPos.Y = 1
endPos.X = 1
endPos.Y = 5

- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Problem to solve an easy condition
« Reply #1 on: September 04, 2011 »
Is this C?
If you have arraydata[12*12], then arraydata[x,y] is incorrect C (it will compile and give you arraydata[y]).
You either need
Code: [Select]
arraydata[x+y*12]
or you need
Code: [Select]
arraydata[12][12]
and
arraydata[x][y]

Jim
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Problem to solve an easy condition
« Reply #2 on: September 04, 2011 »
Hi Jim, Would I be right in thinking a two dimension array not a single as as it would return [y] as it's the last one?

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Problem to solve an easy condition
« Reply #3 on: September 04, 2011 »
The comma operator in C evaluates the expressions left to right and has the value of the rightmost expression. Va!n's code can be made to work either way, with very similar performance.

Jim
Challenge Trophies Won:

Offline Pot Noodle

  • Sponsor
  • Amiga 1200
  • *******
  • Posts: 271
  • Karma: 15
  • Computers have lots of memory but no imagination
    • View Profile
Re: Problem to solve an easy condition
« Reply #4 on: September 04, 2011 »
Cheers Jim for the info.

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1292
  • Karma: 466
    • View Profile
    • my stuff
Re: Problem to solve an easy condition
« Reply #5 on: September 05, 2011 »
This algorithm doesn't appear very elegant to me. If you can explain what exactly you're trying to do, we can probably find a better solution to do it.
If you have many accesses to the matrix you might want to do some kind of summed area table first.

Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1431
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Problem to solve an easy condition
« Reply #6 on: September 05, 2011 »
Is this C?
If you have arraydata[12*12], then arraydata[x,y] is incorrect C (it will compile and give you arraydata[y]).
You either need
Code: [Select]
arraydata[x+y*12]
or you need
Code: [Select]
arraydata[12][12]
and
arraydata[x][y]

Jim

Hello Jim,
yes, the code snip is C#... Btw, i dont want a 1D array like array[12*12] for something like a map.... A 2D array like array[12,12] is more easy to handle and i dont need MULs to get/set x/y position.

In C# i have to write following for a 2D array, which works all fine:

Code: [Select]
int[,] arraydata = new int[12, 12];     // Works fine and easy to use for (level)maps i.e.

    ...fill arraydata with random values between between 0 and 1

// -------- Find first top/left position of wanted object/value

selectedObject = 1;

for (int y = 0; y < 12; y++)
{
    for (int x = 0; x < 12; x++)
    {
        if (arraydata[x, y] == selectedObject)
        {
            startPos.X = x;
            startPos.Y = y;
            x = 13;
            y = 13;
        }
    }
}

Now i have the startPos.X and startPos.Y of the first top/left object. Until here all works fine as i want/need... 

Next i will check if there are neighboars filled with 1 and if so, how many. I will get the startPos and endPos of all neighboars (horizontal and vertical). This is what makes me crazy, even its so easy in theory. In a basic language i used a Repeat/Until loop for this and tried the same in C#, where it does not works. Maybe my fault.

Take a look at the three images which should represent my arraydata[12,12]... All white fields may be value 0... the red and blue fields may be value 1... My first step is to get the first top/left block (with value 1) - Until here all works fine.

My second step is to check if there are direct horizontal or vertical neighboars and if so to check where the last position is. So i have tint the wanted fields blue.. So i should get following results:


Map1:
--------------
startPosX = 3
startPosY =1
endPosX = 6
endPosY = 3


Map2:
--------------
startPosX = 3
startPosY = 3
endPosX = 7
endPosY = 3

Map3:
--------------
startPosX = 3
startPosY = 3
endPosX = 4
endPosY = 7

Following code to check of endPos will not work.
Code: [Select]
do
{
    endPos.X = startPos.X + tempX ;
    tempX++;
} while (leveldata[endPos.X, startPos.Y] == selectedObject);

do
{
    endPos.Y = startPos.Y + tempY ;
    tempY++;
} while (leveldata[startPos.X, endPos.Y] == selectedObject);
« Last Edit: September 05, 2011 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Problem to solve an easy condition
« Reply #7 on: September 06, 2011 »
You haven't shown two things - one, whether you set tempX to 0 every time and two, how you deal with the case where there are blobs all the way to the edge (or you start at an edge).

This is how I'd write your loop:
Code: [Select]
endPos.X = startPos.X;
endPos.Y = startPos.Y;
while (endPos.X < 12 && arraydata[endPos.X, endPos.Y] == selectedObject)
  endPos.X++;
if (endPos.X == 12)
  endPos.X = startPos.X;

Jim
Challenge Trophies Won: