Find diagonals in an array representing a 2d array

I converted my 2d array to a 1d array. For example: (starts with 0, not 1); 00 01 02 03 04
05 06 07 08 09
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24


was converted to a 1d array. [0, 1, 2, 3, 4 ..... 23, 24].

Now I'm trying to create a function that finds every spot that is “connected” or next to a specific element of the array. This includes elements that are diagonal from it. Therefore, using the 2d array above, if I want an array of elements associated with 0, I expect the function to return an array [1, 5, 6].

The problem I am facing is finding the diagonals. This is my JS code for an array to be returned.

var poss = [Number(num+1),Number(num-1),Number(num+col),Number(num-col),Number((num+col) + 1),Number((num+col) - 1),Number((num-col) + 1),Number((num-col) - 1)];

This returns [1, 5, 6, 4]. I have code that excludes negative numbers. However, 4 should not be. I understand that this is because it is a boundary case, and it is not registered as a transcendental one, because it is not a negative number. Is there any formula that will find the elements associated with it diagonally? Remember that I use a 1d array. This program also runs regardless of the size of the array. Thus, this will also work for 4x4 or 5x4 boards. Therefore, the use of field strings and numbers is ideal.

+4
source share
4 answers

, , . , . , , . , . , . if, .

var x = poss[i]
if((Number(num) % col == 0 && Number(num-1) == Number(x)) || 
 (Number(num+1) % col == 0 && Number(num+1) == Number(x)) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num-col) -1) == Number(x))) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num+col) -1) == Number(x))) ||
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num-col) +1) == Number(x))) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num+col) +1) == Number(x)))) 
{//exclude number from results}

num - , . x .

.

+1

, , .

var cols = 5;
var rows = 5;

function connectedPoints(point) {
  var connectedPoints = [];

  // First test if the point is on an edge
  var topEdge = point/cols < 1;
  var leftEdge = point%cols == 0;
  var rightEdge = point%cols == cols-1;
  var bottomEdge = point/cols >= rows-1;

  // Add points that are above the point
  if (!topEdge) {
    if (!leftEdge) {
      connectedPoints.push(returnIfNotNegative(point-cols-1));
    }

    connectedPoints.push(returnIfNotNegative(point-cols));

    if (!rightEdge) {
      connectedPoints.push(returnIfNotNegative(point-cols+1));
    }
  }

  // Add points that are to the left or right of the point
  if (!leftEdge) {
    connectedPoints.push(returnIfNotNegative(point-1));
  }
  if (!rightEdge) {
    connectedPoints.push(returnIfNotNegative(point+1));
  }

  // Add points that are below the point
  if (!bottomEdge) {
    if (!leftEdge) {
      connectedPoints.push(returnIfNotNegative(point+cols-1));
    }

    connectedPoints.push(returnIfNotNegative(point+cols));

    if (!rightEdge) {
      connectedPoints.push(returnIfNotNegative(point+cols+1));
    }
  }

  console.log(connectedPoints);
}

function returnIfNotNegative(point) {
  if (point < 0) {
    return null;
  }
  return point;
}

connectedPoints(0);
+1

. .

, .

var arr = [
  [00, 01, 02, 03, 04],
  [05, 06, 07, 08, 09],
  [10, 11, 12, 13, 14],
  [15, 16, 17, 18, 19],
  [20, 21, 22, 23, 24]
]

function getNeighbours(x, y) {
  var result = [];
  for (var i = x - 1; i <= x + 1; i++) {
    for (var j = y - 1; j <= y + 1; j++) {
      if (arr[i] && arr[i][j]) {
        if (!(x === i && y === j))
          result.push(arr[i][j]);
      }
    }
  }
  return result;
}

console.log(getNeighbours(0, 0));
console.log(getNeighbours(3, 3));
Hide result
+1

. , , .

var arr = [
  0, 1, 2, 3, 4,
  5, 6, 7, 8, 9,
  10, 11, 12, 13, 14,
  15, 16, 17, 18, 19,
  20, 21, 22, 23, 24
]

function getNeighbours(col, row)
{
  var w = 5;
  
  var start = (row > 0 ? row * w - w : row * w) + (col > 0 ? col - 1 : col);
  var rowSpan = ((row > 0 ? 2 : 1) + (row < (w - 1) ? 1 : 0)) * w;
  var colSpan = (col > 0 ? 2 : 1) + (col < (w -1) ? 1 : 0);
  
  var center = col + row * w;
  var result = [];
  for (var r = start; r < start + rowSpan; r += w)
    for (var i = r; i < r + colSpan; i++)
      if (!(i === center))
         result.push(arr[i]);
      
  return result;
}


console.log(getNeighbours(0,0));
console.log(getNeighbours(3,3));
console.log(getNeighbours(3,4));
console.log(getNeighbours(4,3));
console.log(getNeighbours(4,4));
 
/*
[1, 5, 6]
[12, 13, 14, 17, 19, 22, 23, 24]
[17, 18, 19, 22, 24]
[13, 14, 18, 23, 24]
[18, 19, 23]
*/
Hide result
+1
source

Source: https://habr.com/ru/post/1629746/


All Articles