Skip to content

Instantly share code, notes, and snippets.

View GeorgeGardiner's full-sized avatar

George Gardiner GeorgeGardiner

View GitHub Profile
@GeorgeGardiner
GeorgeGardiner / workingday.php
Last active December 18, 2017 13:51
Find out if DateTime is a working day in the United Kingdom (PHP)
public function isWorkingDay(DateTime $dt)
{
// Weekends
if($dt->format('N') > 5) return false;
// New Year's Day
if($dt->format('d-m') == '01-01') return false;
// Early May Bank Holiday
if($dt->format('d-m') == '07-05') return false;
// Spring Bank Holiday
if($dt->format('d-m') == '28-05') return false;
@GeorgeGardiner
GeorgeGardiner / lzw_compress.js
Created February 24, 2015 13:08
Simple Javascript compression algorithm for crunching large strings. Useful for sending big JSON structures to and from a server where bandwidth is an issue.
var LZWEncode = function(s) {
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i=1; i<data.length; i++) {
currChar=data[i];
if (dict[phrase + currChar] != null) {
@GeorgeGardiner
GeorgeGardiner / excel_column_letters.js
Last active August 29, 2015 14:15
Convert integer to Excel style column letters (1=A, 2=B, 27=AA, 30=AD, 189=GG)
var excelLetters = function(num) {
var mod = num % 26;
var pow = num / 26 | 0;
var out = mod ? String.fromCharCode(64 + mod) : (pow--, 'Z');
return pow ? excelLetters(pow) + out : out;
};
console.log(excelLetters(1)); // A
console.log(excelLetters(30)); // AD
console.log(excelLetters(189)); // GG
@GeorgeGardiner
GeorgeGardiner / rotate_polygon.js
Created February 20, 2015 09:30
Rotate a polygon
var irregularPolygon = [
{x: 2.66, y: 4.71},
{x: 0.72, y:2.28},
{x: 1.9, y: 1},
{x: 4, y: 1.6},
{x:3.63, y:2.52},
{x:5, y:3.5}
];
var rotatePolygon = function(polygon, angle, originX, originY) {
@GeorgeGardiner
GeorgeGardiner / is_x_y_coordinate_inside_polygon.js
Created February 20, 2015 09:12
Find out if an (x, y) coordinate is inside any any irregular polygon. Very useful for tasks such as determining if a mouse click is inside a rotated rectangle or other shape.
// Adapted from: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
var irregularPolygon = [
{x: 2.66, y: 4.71},
{x: 0.72, y:2.28},
{x: 1.9, y: 1},
{x: 4, y: 1.6},
{x:3.63, y:2.52},
{x:5, y:3.5}
];