0

I have a 2d matrix (x/y-matrix) like this:

var matrix=[
  '111111',
  '100001',
  '101101',
  '100001',
  '111111'
]

Id like to find all connected '1'-characters (ones) starting from any point and push the result of the connected positions to an array.

Calling my function with e.g (X:2, Y:2) will return the positions I have marked with the '2'-char below:

var matrix=[
  '111111',
  '100001',
  '102201',
  '100001',
  '111111'
 ]

Calling my function with e.g (X:4, Y:4) will return this coordinates also marked with '2':

var matrix=[
  '222222',
  '200002',
  '201102',
  '200002',
  '222222'
]

So I've already written some code but it doesn't seems to work that way & Im absolutely not sure if this would be the most efficient solution. So I hope somebody like to share some code matching my problem, thanks a million in advance,

-- Jonas


Array.prototype.extract_specific_components_positions = function(x_position, y_position, selector) {
 var matrix = this.map(str => Array.from(str, Number)),
  result_array = []

 function test_connection(array, i, j, value, callback) {
  try {
   if (array[i][j].toString() == selector) {
    result_array.push([j, i]);
    array[i][j] = value;
    test_connection(array, i - 1, j, value, callback);
    test_connection(array, i + 1, j, value, callback);
    test_connection(array, i, j - 1, value, callback);
    test_connection(array, i, j + 1, value, callback);
   }
   else {
    callback(j, i)
   }
  }
  catch (e) {
   callback(j, i)
  }
 }
 matrix.forEach(function(a, i, aa) {
  a.forEach(function(b, j, bb) {
   if (i == y_position && j == x_position) {
    status = true
   }
   if (!status) return false
   test_connection(aa, i, j, 2, function(j, i) {
    status = false;
   })
  })
 })
 matrix = matrix.map(a => [a.join('')]).concat.apply([], matrix.map(a => [a.join('')]));
 return [matrix, result_array]
}


var matrix=[
  '111111',
  '100001',
  '101101',
  '100001',
  '111111'
]

var result = matrix.extract_specific_components_positions(0,0,'1')
console.log(result[0].join('\n'))
console.log(result[1])
Jonas0000
  • 1,085
  • 1
  • 12
  • 36
  • Technically, you do not have a 2d array. You have a 1d array of strings. – Taplar Dec 06 '17 at 19:34
  • *"but it doesn't seems to work that way"* -> what way? – janos Dec 06 '17 at 19:36
  • Are diagonal "connections" not considered valid? – Taplar Dec 06 '17 at 19:38
  • Yeah absolutely right Taplar. But just Imagine, a 1D Array could be a 2D Matrix! – Jonas0000 Dec 06 '17 at 19:38
  • Yeah should be valid also, haven‘t added them til now. @Taplar – Jonas0000 Dec 06 '17 at 19:39
  • The was I was using in my code... @janos – Jonas0000 Dec 06 '17 at 19:40
  • Just pointing out that your usage of 2d doesn't match your data. It would be more accurate to split each string up into an array themselves. Then it would be a true 2d array, or a matrix. – Taplar Dec 06 '17 at 19:40
  • Yeah sound fine but however it is not fixing my problem:/ – Jonas0000 Dec 06 '17 at 19:41
  • There are [some](https://stackoverflow.com/questions/47641162/javascript-fast-way-to-find-connected-components-in-array-looking-for-speed-up) possible [duplicates](https://stackoverflow.com/questions/47271406/javascript-arrays-get-most-left-and-most-right-connected-character) – James Dec 06 '17 at 19:45
  • This is absolutely NO duplicate.. Please just read carefully James – Jonas0000 Dec 06 '17 at 19:53
  • @Jonas0000 Why does [pete's question](https://stackoverflow.com/questions/47271406/javascript-arrays-get-most-left-and-most-right-connected-character) use the unusual term "thanks a million in advance" just like you do? Coincidence? – James Dec 06 '17 at 20:00
  • Sorry Sherlock, but theres no „coincidende“. But next Time I will use just thanks, because every user who has ever written „thanks“ in his question is my Account.. keep in mind boy! – Jonas0000 Dec 06 '17 at 20:11

0 Answers0