Skip to content

Instantly share code, notes, and snippets.

View dbrockman's full-sized avatar

David Brockman dbrockman

View GitHub Profile
@dbrockman
dbrockman / countTilesInRegion.m
Created February 11, 2014 08:56
How to count the number of tiles in a map region with min/max zoom level
- (NSUInteger)countTilesInRegionSouthWest:(CLLocationCoordinate2D)southWest
northEast:(CLLocationCoordinate2D)northEast
minZoom:(int)minZoom
maxZoom:(int)maxZoom
{
double minLat = southWest.latitude;
double maxLat = northEast.latitude;
double minLon = southWest.longitude;
double maxLon = northEast.longitude;
var digits = '0123456789' +
'abcdefghijklmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'-_';
var radix = digits.length;
function shorten_number(n) {
var s = '';
while (n > 0) {
s = digits.charAt(n % radix) + s;
@dbrockman
dbrockman / flatten2D.js
Created December 16, 2013 14:46
Flatten 2D array
function flatten2D(array) {
return array.reduce(function (result, item) {
return result.concat(item);
}, []);
}
@dbrockman
dbrockman / getBoundsAtLatLngWithZoom.js
Created December 10, 2013 22:03
Google maps latlng bounds with center and zoom level in map viewport
function getBoundsAtLatLngWithZoom(map, center, zoom) {
var p = map.getProjection();
if (!p) {
return null;
}
var el = $(map.getDiv());
var zf = Math.pow(2, zoom) * 2;
var dw = (el.width() | 0) / zf;
var dh = (el.height() | 0) / zf;
var cpx = p.fromLatLngToPoint(center);
@dbrockman
dbrockman / randomString.js
Created June 6, 2013 18:26
Function that returns a random string of specified length.
function randomString(length) {
var s = ((performance.now() % 1) + Math.random()).toString(35);
return s.substr(-Math.max(0, Math.min(length, s.length - 2)));
}
@dbrockman
dbrockman / jenkins-hash.js
Last active December 15, 2015 21:39
Jenkins hash function on strings
function jenkins_hash(s) {
var hash = 0, i = 0, l = s.length;
for (; i < l; ++i) {
hash += s.charCodeAt(i);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
function cachedAsyncFn(fn) {
var cached, queue;
return function (callback) {
if (cached) {
process.nextTick(function () {
callback.apply(null, cached);
});
} else if (queue) {
queue.push(callback);
} else {
@interface NSIndexSet (Operations)
// http://en.wikipedia.org/wiki/Union_(set_theory)
- (NSIndexSet *)unionWith:(NSIndexSet *)other;
// http://en.wikipedia.org/wiki/Intersection_(set_theory)
- (NSIndexSet *)intersectionWith:(NSIndexSet *)other;
// http://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement
- (NSIndexSet *)relativeComplementIn:(NSIndexSet *)universe;
function fibonacci(n) {
return n < 2 ? n : fibonacci(n - 2) + fibonacci(n - 1);
}
function fibonacci(n) {
var prev, curr = 0, next = 1;
while (n-- > 0) {
prev = curr;
curr = next;
next = prev + curr;
@dbrockman
dbrockman / insertAt-removeAt.js
Created March 7, 2013 18:29
JS array insertAt and removeAt function
function insertAt(arr, val, i) {
arr.splice(i, 0, val);
}
function removeAt(arr, i) {
return arr.splice(i, 1).length === 1;
}
function genericInsertAt(arr, val, i) {
Array.prototype.splice.call(arr, i, 0, val);
}