•      Sign In
• Create Account

## Checking dynamic 2d array diagonally

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1shinypixel  Members

226
Like
0Likes
Like

Posted 26 May 2014 - 12:09 PM

Edit: Please check my reply below for the current code.

I'm not having too much luck with this. Think of Tic Tac Toe, and trying to find if the angle contains all of the same value (such as 1). I'm operating on a dynamic, 2D array.


bool isAngleWithValue(const int xPosFrom, const int yPosFrom,
const int xPosTo, const int yPosTo, const T &value)
{
bool blnGreatSuccess = true;
int width, height;

// ... removed validation code for demostration

// get width and height
width = (xPosTo - xPosFrom);
height = (yPosTo - yPosFrom);

for (int y = yPosFrom; y < height; y++)
{
for (int x = xPosFrom; x < width; x++)
{
if (x == y)
{
if (m_array[y * m_width + x] != value)
blnGreatSuccess = false;
}
}
}

return blnGreatSuccess;
}


For example, this works:

    Array2D<int> a(3, 3);
a.fill(0);

a.get(0, 0) = 1;
a.get(1, 1) = 1;
a.get(2, 2) = 1;

if (a.isAngleWithValue(0, 0, 2, 2, 1))
cout << "Yes!" << endl;
else
cout << "No!" << endl;


But when I change a.get(2,2) to a.get(1, 2), it's still outputting "Yes!", which is not functioning correctly.

I tried researching various sites, but I can't seem to relate it to this problem. A general algorithm can be helpful too for checking diagonally.

Edited by shinypixel, 26 May 2014 - 03:22 PM.

### #2Samith  Members

2450
Like
0Likes
Like

Posted 26 May 2014 - 01:16 PM

What your code is doing is basically just checking that any elements on the diagonal of the 2D array that are within the rect (xPosFrom, yPosFrom) to (xPosFrom + width, yPosFrom + height) have the value 'value'. Try printing out what x,y positions actually ever get checked, and you'll see what I mean.

What you're looking for is probably a DDA type of algorithm. DDA stands for "digital differential analyzer". It's the kind of algorithm that's used to raster triangles/lines into 2D framebuffers, for example. What you're doing is essentially trying to draw a line across a 2D grid and check all the squares it intersects with, which is very similar to the rasterization problem.

### #3shinypixel  Members

226
Like
0Likes
Like

Posted 26 May 2014 - 03:13 PM

Thanks for the thoughts. I'm just trying to think of this really simple, like checking for a win in a Tic Tac Toe game (but keeping this as a general array member function).

The idea is:

If a line starting from (x,y) is N long, no matter what direction, return true. Else, return false.

I modified the function a bit.

    ///////////////////////////////////////////////////////////////////////////
/// <summary>
/// Checks if an angle contains all the same value.
/// </summary>
/// <param name = "xPosFrom"> the starting x-position to check for value. </param>
/// <param name = "yPosFrom"> the starting y-position to check for value. </param>
/// <param name = "intPlaces"> How many cells to check for value. </param>
/// <param name = "value"> The value to check for in each cell. </param>
/// <returns> Returns true on success, else false. </returns>
///////////////////////////////////////////////////////////////////////////
bool isAngleWithValue(const int xPosFrom, const int yPosFrom,
const int intPlaces, const T &value)
{
bool blnGreatSuccess = false;
int width, height;
int xPos, yPos;
int times = 0;

// validate array
if (!isValid())
return false;

// check for first occurance of finding value
if ((m_array[yPosFrom * m_width + xPosFrom]) == value)
{
times++;
}
else
{
// value doesn't exist in first position. Bail out.
return false;
}

// TODO: How to check around the first position?

if (times >= intPlaces)
blnGreatSuccess = true;

return blnGreatSuccess;
}


Edited by shinypixel, 26 May 2014 - 03:20 PM.

### #4shinypixel  Members

226
Like
0Likes
Like

Posted 26 May 2014 - 04:41 PM

I think I got it.. it just takes a lot of testing on each side of the cell N times.

### #5Pink Horror  Members

2459
Like
0Likes
Like

Posted 26 May 2014 - 05:41 PM

But when I change a.get(2,2) to a.get(1, 2), it's still outputting "Yes!", which is not functioning correctly.

I tried researching various sites, but I can't seem to relate it to this problem. A general algorithm can be helpful too for checking diagonally.

You are passing in 0 and 2 as the dimensions.

Your width and height are then (2 - 0) -> 2.

You then loop from i = 0 to i < 2 -> meaning you only check 0 and 1.

So, the value at (2, 2) does not affect your result.

### #6shinypixel  Members

226
Like
0Likes
Like

Posted 26 May 2014 - 07:17 PM

Yeah, I had to rearrange the algorithm a bit, and got it working finally. Thanks everyone.

Edited by shinypixel, 26 May 2014 - 07:18 PM.

### #7Pink Horror  Members

2459
Like
0Likes
Like

Posted 26 May 2014 - 10:10 PM

Yeah, I had to rearrange the algorithm a bit, and got it working finally. Thanks everyone.

Your newer function doesn't even have a loop in it. I cannot understand how that would work either. It's only checking one location.

### #8shinypixel  Members

226
Like
0Likes
Like

Posted 27 May 2014 - 07:54 PM

I know, which is why I had the TODO comment in there, but now it's implemented.

### #9ankhd  Members

2284
Like
0Likes
Like

Posted 28 May 2014 - 06:05 AM

Try this.

// use Bresenham-like algorithm to print a line from (y1,x1) to (y2,x2)
// The difference with Bresenham is that ALL the points of the line are
//   printed, not only one per x coordinate.
// Principles of the Bresenham's algorithm (heavily modified) were taken from:
//   http://www.intranet.ca/~sshah/waste/art7.html
void useVisionLine (int y1, int x1, int y2, int x2)
{
int i;               // loop counter
int ystep, xstep;    // the step on y and x axis
int error;           // the error accumulated during the increment
int errorprev;       // *vision the previous value of the error variable
int y = y1, x = x1;  // the line points
int ddy, ddx;        // compulsory variables: the double values of dy and dx
int dx = x2 - x1;
int dy = y2 - y1;
POINT (y1, x1);  // first point
// NB the last point can't be here, because of its previous point (which has to be verified)
if (dy < 0){
ystep = -1;
dy = -dy;
}else
ystep = 1;
if (dx < 0){
xstep = -1;
dx = -dx;
}else
xstep = 1;
ddy = 2 * dy;  // work with double values for full precision
ddx = 2 * dx;
if (ddx >= ddy){  // first octant (0 <= slope <= 1)
// compulsory initialization (even for errorprev, needed when dx==dy)
errorprev = error = dx;  // start in the middle of the square
for (i=0 ; i < dx ; i++){  // do not use the first point (already done)
x += xstep;
error += ddy;
if (error > ddx){  // increment y if AFTER the middle ( > )
y += ystep;
error -= ddx;
// three cases (octant == right->right-top for directions below):
if (error + errorprev < ddx)  // bottom square also
POINT (y-ystep, x);
else if (error + errorprev > ddx)  // left square also
POINT (y, x-xstep);
else{  // corner: bottom and left squares also
POINT (y-ystep, x);
POINT (y, x-xstep);
}
}
POINT (y, x);
errorprev = error;
}
}else{  // the same as above
errorprev = error = dy;
for (i=0 ; i < dy ; i++){
y += ystep;
error += ddx;
if (error > ddy){
x += xstep;
error -= ddy;
if (error + errorprev < ddy)
POINT (y, x-xstep);
else if (error + errorprev > ddy)
POINT (y-ystep, x);
else{
POINT (y, x-xstep);
POINT (y-ystep, x);
}
}
POINT (y, x);
errorprev = error;
}
}
// assert ((y == y2) && (x == x2));  // the last point (y2,x2) has to be the same with the last point of the algorithm
}



Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.