Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
¿Cómo traducir el índice de la lista de matrices en coordenadas de matriz?

Digamos que tienes una matriz 2d:

 const matrix = [ [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5] ];

Que se representa como una lista:

 const list = [0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5];

Puede generar fácilmente el índice de la lista en la lista usando lo siguiente:

 function coordsToListIndex({ x, y }, width) { return x + (y * width); } // index = 7 const index = coordsToListIndex({ x: 1, y: 1 }, 6);

¿Cuál es la mejor manera de traducir en la dirección opuesta, del índice de la lista a las coordenadas?

Lo que se me ocurrió es lo siguiente, pero sospecho que podría haber una mejor manera.

 function listIndexToCoords(index, width) { let count = 0; let x = 0; let y = 0; for (let i = 0; i < width; i++) { count++; x++; if (count === index) { break; } else if (i == width - 1) { x = 0; y++; i = 0; } } return { x, y }; } // cords = { x: 1, y: 1 } const coords = listIndexToCoords(7, 6);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Basado en aportes y soluciones de
Mike 'Pomax' Kamermans y shawn_halayka en los comentarios de arriba.

 function coordsToListIndex({ x, y }, width) { return x + (y * width); } function listIndexToCoords(index, width) { return { x: index % width, y: Math.floor(index / width) }; }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!