buildPath: function() { var buildPathDeferred = Q.defer(); var path = this; Q.allSettled([ Point.getPoint(this.origin), Point.getPoint(this.destination) ]).then(function(resolves) { var origin = resolves[0].value, destination = resolves[1].value; var directions = origin.directionsTo(destination), distance = origin.distanceTo(destination); path.zoom = origin.arbitraryZoom(destination, path.width, path.height, 2000, 2000); return path.getTiles(directions); }).then(function(options) { var grid = new Grid(options.tiles); grid.fill(options); }).catch(function() { console.log(arguments); }); return buildPathDeferred.promise; }
getTiles: function(directions) { var getTilesDeferred = Q.defer(); var path = this; Q.allSettled([ Point.getPoint(this.origin), Point.getPoint(this.destination) ]).then(function(resolves) { var origin = resolves[0].value, destination = resolves[1].value; if (path.adjacent > 0) { var newOrigin = origin; for (var tile = 0; tile < path.adjacent; tile++) { var α; if (directions.west && directions.north) { α = 225; } else if (directions.west && directions.south) { α = 45; } else if (directions.east && directions.north) { α = 315; } else { α = 135; } newOrigin = newOrigin.adjacent(α, path.width, path.height, path.zoom); } getTilesDeferred.resolve({ path: path, directions: directions, tiles: newOrigin.tilesInZoom(destination, path.width, path.height, path.zoom), }); } else { getTilesDeferred.resolve({ path: path, directions: directions, tiles: origin.tilesInZoom(destination, path.width, path.height, path.zoom) }); } }); return getTilesDeferred.promise; },
capillaryWaves: function(k, width, height, zoom) { var capillaryWavesDeferred = Q.defer(); var promises = []; var Point = require(global.APP_DIR + '/models/Point'); var capillaryArray = []; var total = k * 2; for (var y = 0; y < total; y++) { capillaryArray[y] = []; for (var x = 0; x < total; x++) { var Δx = ((k - y) * -width) + width / 2, Δy = ((k - x) * -height) + height / 2; var point = new Point(); point.location = [this.adjustLatByPixels(Δx, zoom), this.adjustLonByPixels(Δy, zoom)]; point.lat = point.location[0]; point.lon = point.location[1]; capillaryArray[y][x] = point; promises.push(point.getPoint().then(function(point) { capillaryArray[this.y][this.x] = point; }.bind({ x: x, y: y }))); } } Q.allSettled(promises) .then(function(resolves) { var promisesAfter = []; resolves.forEach(function(resolve) { if (resolve.state === 'fulfilled') { var deferred = Q.defer(); deferred.resolve(resolve.value); promisesAfter.push(deferred.promise); } else { promisesAfter.push(Q.nbind(resolve.reason.save, resolve.reason)()); } }); return Q.allSettled(promisesAfter); }) .then(function() { capillaryWavesDeferred.resolve(capillaryArray); }); return capillaryWavesDeferred.promise; },