user2865835
user2865835

Reputation: 59

how to save coordinates in C++

I have a matrix with cubes that have these colors: red, green, yellow.

Some of the cubes which are next to each other contact with others which are same color. For example:

  1 2 3 4 5
1 r r g y r
2 g y g g g
3 g y r g r
4 g g r g g

I want to count how many cubes with the same color are near to each other (in this example there are 7 g (green) cubes which are near to each other. This part I have already done.

bool Move(Cubes & Test, Cubes & Kub, int i, int j, int p, string color, int & count){
    int tt = 0;
    bool yra = false;
    if(Test.Get(i, j).GetCube(p) == Color){
        Test.SetSP(i, j, p, "-");
        count++;
        while(!yra && tt < 4 ){
            i = i + Test.GetEil(tt);
            j = j + Test.GetStu(tt++);
            if(Test.Get(i, j).GetCube(p) == color){
                Move(Test, Kub, i, j, p, color, count);
            }
        }
    }
    return yra;
}

void Max(int & count, int & max, string & s, string Spal[], int sp, Maximum Maxi[], int & ind, int i, int j){
    if(count!=0){
        Maxi[ind].Set(count, Spal[sp]);
        ind++;
    }
    if(max < count){
        max = count;
        count = 0;
        s = Spal[sp];
    }
    else
        count = 0;
}

And now somehow I need to save my green color coordinates which are near to each other.

What should I do to achieve that?

Upvotes: 1

Views: 1209

Answers (1)

jrd1
jrd1

Reputation: 10726

From your question, I think you're trying to determine the nearest square, and not cube - as you're using two variables to keep track of i and j. Additionally, I can't comment accurately on your code as some details are missing.

So, since you've asked how to determine the nearest "cube"(sic) - i.e. square - this is something called a Nearest Neighbor Search - which is also known as the Post-Office Problem - the difficulty of which increases with dimensionality.

Nonetheless, examining your grid and making the assumption that we are limited to 2D:

random grid of colored red, green and yellow squares

We immediately observe that we can use regular Euclidean techniques to find the closest square relative to one:

Euclidean nearest distance formula

So if we know the coordinates of the each square, we can then calculate the distance to each from the current square in question.

EXAMPLE

Nearest neighbors of the yellow square on (row 1, col 4) (assuming a size of two for each side):

This sample grid has two other yellow squares square1(2, 2) and square2(3, 2)

If we assume that this grid is cartesian, with the bottom-left corner of green square (row 4, col 1) being centered at coordinate (1.0, 1.0) (remember we said that the sides were of length 2). Then, the center coordinates for:

yellow: (7.0, 7.0)
square1: (3.0, 5.0)
square2: (3.0, 3.0)

At this point you have to determine how you want to calculate the nearest square: edgewise, or from center to center?

Center to center is the easiest so, using yellow square at (row 1, col 4), the distances of the other squares relative to it (using the formula given) are:

yellow -> square1: sqrt(4^2 + 2^2) = sqrt(20) = 4.4721...
yellow -> square2: sqrt(4^2 + 4^2) = 4*sqrt(2) = 5.6569...

From these, we can clearly see that square1 is closer to the yellow square (row 1, col 4).

  1. So, one way to calculate those coordinates are to poll the grid for all squares of a different color, determine their respective coordinates.

  2. Then, iterate over these coordinates (edgewise or center-wise) and determine which square has the smallest distance to a particular square in question.

  3. Store the coordinates of the originating square and the closest square.

  4. Repeat for all squares.

For more reading on this, you can consult:

http://web.engr.oregonstate.edu/~tgd/classes/534/slides/part3.pdf

Upvotes: 1

Related Questions