I would like to simplify and optimize this function which finds me the neighbors of a cell thanks to cartesian coordinates of a cell Id
const getNeighborsCellId = cellId => { const neighbors = [] const {x, y} = getCoordinatesFromCellId(cellId) // LOL -> const isStaggered = y % 2 if (isStaggered){ neighbors.push(getCellIdFromCoordinates(x, y - 1)) neighbors.push(getCellIdFromCoordinates(x, y - 2)) neighbors.push(getCellIdFromCoordinates(x + 1, y - 1)) neighbors.push(getCellIdFromCoordinates(x - 1, y)) neighbors.push(getCellIdFromCoordinates(x + 1, y)) neighbors.push(getCellIdFromCoordinates(x, y + 1)) neighbors.push(getCellIdFromCoordinates(x, y + 2)) neighbors.push(getCellIdFromCoordinates(x + 1, y + 1)) } else { neighbors.push(getCellIdFromCoordinates(x - 1, y - 1)) neighbors.push(getCellIdFromCoordinates(x, y - 2)) neighbors.push(getCellIdFromCoordinates(x, y - 1)) neighbors.push(getCellIdFromCoordinates(x - 1, y)) neighbors.push(getCellIdFromCoordinates(x + 1, y)) neighbors.push(getCellIdFromCoordinates(x - 1, y + 1)) neighbors.push(getCellIdFromCoordinates(x, y + 2)) neighbors.push(getCellIdFromCoordinates(x, y + 1)) } return neighbors }
Thank you