Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Daniel-Hug / Geometry.md
Last active August 29, 2015 13:56
Geometry of 2D & 3D figures with π/4

#Geometry of 2D & 3D figures with $\frac{\pi}{4}$

$$ \frac{\pi}{4} = \sum\limits_{k=0}^\infty \frac{(-1)^k}{2k + 1} = \frac{1}{1}-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}\ldots\approx 78.5% $$

##2D figures

Area Circumference
//addEventListener polyfill 1.0 / Eirik Backer / MIT Licence
(function(win, doc){
if(win.addEventListener)return; //No need to polyfill
function docHijack(p){var old = doc[p];doc[p] = function(v){return addListen(old(v))}}
function addEvent(on, fn, self){
return (self = this).attachEvent('on' + on, function(e){
e = e || win.event;
e.preventDefault = e.preventDefault || function(){e.returnValue = false}
e.stopPropagation = e.stopPropagation || function(){e.cancelBubble = true}
@Daniel-Hug
Daniel-Hug / badchar.json
Last active August 29, 2015 14:10
This is a valid JSON file, but it wont convert to valid jsonp with https://github.com/maxogden/jsonpify
{
"content": "Wierd Char --> 
 <--"
}
function arrayTo2d(items, width) {
var rows = [];
var lastRow;
items.forEach(function(item, i) {
// add another row when cell index is divisble by width
if ((i) % width === 0) {
lastRow = [];
rows.push(lastRow);
}
@Daniel-Hug
Daniel-Hug / ajax.js
Last active October 8, 2015 18:53
ajax GET and POST for JSON using a fetch() wrapper
// ajax.js
// ajax GET and POST for JSON using a fetch() wrapper
//
// By Daniel Hug: https://gist.github.com/Daniel-Hug/dd703e0805364a70ba11
// MIT license: http://hug.mit-license.org/
//
// use:
// var request = ajax.get({
// url: 'http://example.com/people.json',
// success: cb,
@Daniel-Hug
Daniel-Hug / removeChilds.js
Created October 28, 2015 23:39
empty element of its child nodes with pure JS
// removes all of an element's childNodes
function removeChilds(el) {
var last;
while ((last = el.lastChild)) el.removeChild(last);
}
@Daniel-Hug
Daniel-Hug / exclude.js
Created November 29, 2015 06:38
exclude / subtract items from an array or collection
// remove collection b's items from collection a
function exclude(a, b) {
var arr = [];
a = arr.slice.call(a);
for (var i = 0, l = a.length; i < l; i++) {
if (arr.indexOf.call(b, a[i]) >= 0) arr.splice.call(a, i, 1);
}
return a;
}
@Daniel-Hug
Daniel-Hug / loop.js
Created December 4, 2015 03:20
JS each and map helper functions
// Loop through collections:
function each(arr, fn, scope) {
for (var i = 0, l = arr.length; i < l; i++) {
fn.call(scope, arr[i], i, arr);
}
}
function map(arr, fn, scope) {
var l = arr.length, newArr = [];
for (var i = 0; i < l; i++) {
newArr[i] = fn.call(scope, arr[i], i, arr);
@Daniel-Hug
Daniel-Hug / storage.js
Created December 4, 2015 03:21
localStorage + JSON wrapper
// localStorage + JSON wrapper:
var storage = {
get: function(prop) {
return JSON.parse(localStorage.getItem(prop));
},
set: function(prop, val) {
localStorage.setItem(prop, JSON.stringify(val));
},
has: function(prop) {
return localStorage.hasOwnProperty(prop);
@Daniel-Hug
Daniel-Hug / table-to-array.js
Created December 4, 2015 08:23
JS: convert 2D array to array of objects
// convert 2D array to array of objects
function tableToArray(columnNames, columns) {
var outputArray = [];
// loop through array of columns
columns.forEach(function(col, colI) {
var columnName = columnNames[colI];
// loop through cells in each column array
col.forEach(function(cellVal, cellI) {