Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //To run, call getNonEmptyCells with desired integer N.
- //Grab results from foundCells var.
- //If needed, add new non-empty points to getCell method;
- var foundCells = {};
- var processedCells = {};
- function getNonEmptyCells(N) {
- var processingQueue = [];
- processingQueue.push(new Point(0, 0));
- while(processingQueue.length > 0) {
- processNeighbours(processingQueue.pop(), processingQueue, N);
- }
- //Change to other output if needed
- //console.log(foundCells);
- }
- function processNeighbours(point, queue, N) {
- var searchAreaLeftBound = point.x - N - 1;
- var searchAreaRightBound = point.x + N + 1;
- var searchAreaUpperBound = point.y - N - 1;
- var searchAreaLowerBound = point.y + N + 1;
- for (var pX = searchAreaLeftBound; pX <= searchAreaRightBound; pX++) {
- for(var pY = searchAreaUpperBound; pY <= searchAreaLowerBound; pY++) {
- var potentialPoint = new Point(pX, pY);
- if (!processedCells[potentialPoint.toString()]) {
- processedCells[potentialPoint.toString()] = true;
- if(getCellByPoint(potentialPoint)) {
- foundCells[potentialPoint.toString()] = true;
- queue.push(potentialPoint);
- }
- }
- }
- }
- }
- ///// HELPER STRUCTURES /////
- function Point(x, y) {
- this.x = x;
- this.y = y;
- }
- Point.prototype.toString = function() {
- return this.x + ";" + this.y;
- }
- function getCellByPoint(point) { return getCell(point.x, point.y); }
- function getCell(x, y) {
- //Black box, returns true for predifined pairs of x and y
- switch (x + ";" + y) {
- case "-2;2": return true;
- case "1;6": return true;
- case "8;9": return true;
- case "-8;0": return true;
- case "-7;0": return true;
- case "-6;0": return true;
- case "-5;0": return true;
- case "-4;0": return true;
- case "-3;0": return true;
- case "-2;0": return true;
- case "-1;0": return true;
- case "0;0": return true;
- case "1;0": return true;
- case "2;0": return true;
- case "3;0": return true;
- case "4;0": return true;
- case "5;0": return true;
- case "6;0": return true;
- case "7;0": return true;
- case "8;0": return true;
- case "0;8": return true;
- case "0;7": return true;
- case "0;6": return true;
- case "0;5": return true;
- case "0;4": return true;
- case "0;3": return true;
- case "0;2": return true;
- case "0;1": return true;
- case "0;-1": return true;
- case "0;-2": return true;
- case "0;-2": return true;
- case "0;-3": return true;
- case "0;-4": return true;
- case "0;-5": return true;
- case "0;-6": return true;
- case "0;-7": return true;
- case "0;-8": return true;
- default: return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement