Home > Software design >  retrieve x and y coordinates from array indices
retrieve x and y coordinates from array indices

Time:11-15

I am trying to convert points in an array to an object of lines with start and endpoints.

This is my array:

let icon = [
  [ [], [] ,[] , [], ["a"]],
  [ [],[] ,[] ,[] ,[] ],
  [ [],[] , ["a","b"], ["b","c"],[] ],
  [ [],[] ,[] ,[] ,[] ],
  [ [], ["c","d"],[] , ["d","e"], [] ],
  [ [], [], ["e","f"], ["f","g"], []],
  [ [], [], [], ["g","h"], ["h"]]
];

This function is supposed to iterate through the array and populate the empty vertex list object with the x/y coordinates of the points:

let getVertices= function(icon){

  for(let g=0; g<icon.length; g  ){
    for (let u=0; u<icon[0].length; u  ){

      let foundPoints = icon[g][u];
      
      if(foundPoints.length==1){
        vertexList[foundPoints[0]]=[[u,g]];
        
        if(g==(icon.length-1)){
          console.log("last Point?: "   foundPoints );
        let lastVertix = vertexList[foundPoints[0]];
        console.log("lastVertix: "   lastVertix);
        let lastLetterIndex = vertexLetters.indexOf(foundPoints);
                
        let previousLetter=vertexLetters[(lastLetterIndex-1)];
        console.log("previousLetter: "   previousLetter);
        console.log("last point: "   [vertexList[previousLetter].slice(-1)]);
        vertexList[foundPoints[0]].splice(0, 0, vertexList[previousLetter].slice(-1));
        }
        
        }
      
      if(foundPoints.length==2){
        vertexList[foundPoints[1]]=[[u,g]];
        vertexList[foundPoints[0]].push([u,g]);
        vertexLetters =foundPoints.at(-1);
      }
  }
}

As expected, I get these values for all points:

enter image description here

But the last vertex doesn't work the same way:

enter image description here

I am having trouble handling the last point, the issue must lie here:

if (g == icon.length - 1) {
  console.log('last Point?: '   foundPoints);
  let lastVertix = vertexList[foundPoints[0]];
  console.log('lastVertix: '   lastVertix);
  let lastLetterIndex = vertexLetters.indexOf(foundPoints);

  let previousLetter = vertexLetters[lastLetterIndex - 1];
  console.log('previousLetter: '   previousLetter);
  console.log('last point: '   [vertexList[previousLetter].slice(-1)]);
  vertexList[foundPoints[0]].splice(
    0,
    0,
    vertexList[previousLetter].slice(-1),
  );
}

CodePudding user response:

Typically I try not to completely rewrite a user's code (as it is easier to teach when you are just fixing/modifying a few small things), but I think your code could be significantly reduced and simplified.

There is also some code missing so I'm not entirely sure what some variables are like vertexList and vertexLetters (the vertexList I assume is what you are returning to show the coordinates).

That being said, I think using .forEach() to loop through your array and simply checking if each of these nested arrays contains your letter via .includes() should give you the desired result. Because I'm not sure what vertexLetters is or how it factors into your code I do not have that included in my example.

let icon = [
  [ [], [] ,[] , [], ["a"]],
  [ [],[] ,[] ,[] ,[] ],
  [ [],[] , ["a","b"], ["b","c"],[] ],
  [ [],[] ,[] ,[] ,[] ],
  [ [], ["c","d"],[] , ["d","e"], [] ],
  [ [], [], ["e","f"], ["f","g"], []],
  [ [], [], [], ["g","h"], ["h"]]
],
vertexList = []

const _FindCoordinates = letter => {
  vertexList = []
  icon.forEach((a, y) => {
    a.forEach((b, x) => {
      if(icon[y][x].includes(letter)) vertexList = [...vertexList, [x, y]]
    })
  })
  return vertexList
}

console.log(_FindCoordinates("a"))
console.log(_FindCoordinates("h"))

NOTE

This works for the provided input/example, but depending on how many nested arrays there are, this obviously would not work.

  • Related