I have a 2D array like this:
const matrix = [
[0, 0, 0, 0],
[0, 2, 1, 0],
[0, 1, 2, 0],
[0, 0, 0, 0]
]
If the x
and y
of a cell is given, for example 2
and 1
(matrix[2][1]
is 1
), how can I find all diagonal lines (two arrays, each of which contain the points of every cell in the diagonal in proper order) which pass through the provided cell? The matrix (array) will always be symmetrical and square (To be exact, I have an 8x8 grid).
Example:
const matrix = [
[0, 0, 0, 0],
[0, 2, 1, 0],
[0, 1, 2, 0],
[0, 0, 0, 0]
];
const cellX = 2, cellY = 1;
// Code ...
const diagonalFromTopLeft = [0, 1, 0];
const diagonalFromTopRight = [0, 1, 1, 0]
Bonus points if you are able to find every line, not just the diagonals (vertical and horizontal, but I can figure that out myself).