javascript - Check nearby point in 2d array with boundary conditions -


i have program checks coordinate in array of arrays, , searches nearby coordinates find nearest 5 'event'. however, when @ edges of grid, in example (0,0), face problem of same event being returned multiple times different 'distance' away (this distance manhattan distance).

i think occurs because have parameters set if coordinate wants check outside of grid (less 0) it's value changed match boundary (0).

let check = (x, y, d) => {         if (x > 20) {             x = 20;         }         if (x < 0) {             x = 0;         }         if (y > 20) {             y = 20;         }         if (y < 0) {             y = 0;         }          if (main[x][y].event) {             let info = {                 x: x - (xrange/2),                 y: y - (xrange/2),                 event: main[x][y].event,                 distance: d,                 ticket: main[x][y].tickets[0],             }             return info;         } else {             return false;         }     }  let findevents = (x, y) => {         let nearby = [];         let info;          // check point x, y         if (main[x][y].event) {             info = {                     x: x - (xrange/2),                     y: y - (xrange/2),                     event: main[x][y].event,                     distance: 0,                     tickets: main[x][y].tickets,             }             nearby.push(info);         }          (let d = 1; d <= 40; d++) {             (let = 0; < d + 1; i++) {                  info = check(x - d + i, y - i, d);                  if (info) {                     nearby.push(info);                 }                 if ((nearby.length > 5) &&                     (nearby[(nearby.length-1)].distance !=                         nearby[(nearby.length-2)].distance)) {                     return nearby.slice(0,-1);                 }                  info = check(x + d - i, y + i, d);                  if (info) {                     nearby.push(info);                 }                 if ((nearby.length > 5) &&                     (nearby[(nearby.length-1)].distance !=                         nearby[(nearby.length-2)].distance)) {                     return nearby.slice(0,-1);                 }             }              (let = 1; < d; i++) {                  info = check(x - i, y + d - i, d);                  if (info) {                     nearby.push(info);                 }                 if ((nearby.length > 5) &&                     (nearby[(nearby.length-1)].distance !=                         nearby[(nearby.length-2)].distance)) {                         return nearby.slice(0,-1);                 }                  info = check(x + d - i, y - i, d);                  if (info) {                     nearby.push(info);                 }                 if ((nearby.length > 5) &&                     (nearby[(nearby.length-1)].distance !=                         nearby[(nearby.length-2)].distance)) {                         return nearby.slice(0,-1);                 }             }         }         return nearby;     } 

any tips on how avoid this? (or clean code in general :d )

i don't understand find_events method yet, seems avoid problem described, should return false check whenever point outside grid. thus:

let check = (x, y, d) => {   if (x > 20 || x < 0 || y > 20 ||y < 0) {     return false;   } ... 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -