Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
Sort Coordinates by Distance to Origin Point - JavaScript

I have random rectangles on canvas stored in an array like so:

var rectangles = [
     {x: 10, y: 10},
     {x: 40, y: 50},
     {x: 1, y: 70},
     {x: 80, y: 5},
     {x: 30, y: 60}
];

I now want to label these rectangles based on their proximity to the origin point (0, 0).

My first thought was to loop through the x and y axis in different patterns, one example would be:

// 100 is the width and height of the canvas

for(var x = 0; x < 100; x++){

   for(var y = 0; y < 100; y++){

       // "intersects" loops through the array and returns the matching index or -1 if no match

       if(intersects(rectangles, x, y) > -1){

            console.log('Rectangle' + (intersects(rectangles, x, y) + 1));

       }

   }

}

The issue i am having, is that no matter the pattern of the loop the result is not as expected.

enter image description here

My second thought was to draw rectangles to the origin point (seen on the last image) and sort the by the size of the rectangle. However, this (and calculating the line distance for that matter) also did not produce the expected result. This can be seen with the green rectangle, that is very close to X0, but should be last.

For example this should return the same result:

enter image description here

Does anyone know how I can achieve the correct labeling result? Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's how to compare distances of coordinates against the origin and sort them (closest to furthest).

var rectangles = [
     {x: 10, y: 10},
     {x: 40, y: 50},
     {x: 1, y: 70},
     {x: 80, y: 5},
     {x: 30, y: 60}
];

const sumOfSquares = (x, y) => {
  return Math.pow(x, 2) + Math.pow(y, 2);
};

rectangles.sort((a, b) => {
  const sumA = sumOfSquares(a.x, a.y);
  const sumB = sumOfSquares(b.x, b.y);
  return sumA - sumB;
});

console.log(rectangles);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda