export default function within(ids, coords, qx, qy, r, nodeSize) { const stack = [0, ids.length - 1, 0]; const result = []; const r2 = r * r; while (stack.length) { const axis = stack.pop(); const right = stack.pop(); const left = stack.pop(); if (right - left <= nodeSize) { for (let i = left; i <= right; i++) { if (sqDist(coords[2 * i], coords[2 * i + 1], qx, qy) <= r2) result.push(ids[i]); } continue; } const m = Math.floor((left + right) / 2); const x = coords[2 * m]; const y = coords[2 * m + 1]; if (sqDist(x, y, qx, qy) <= r2) result.push(ids[m]); const nextAxis = (axis + 1) % 2; if (axis === 0 ? qx - r <= x : qy - r <= y) { stack.push(left); stack.push(m - 1); stack.push(nextAxis); } if (axis === 0 ? qx + r >= x : qy + r >= y) { stack.push(m + 1); stack.push(right); stack.push(nextAxis); } } return result; } function sqDist(ax, ay, bx, by) { const dx = ax - bx; const dy = ay - by; return dx * dx + dy * dy; }