Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Gerjo / gist:7400106
Created November 10, 2013 16:13
JavaScript float... fail.
var a = 33.3, b = 43.7;
var n = new Float32Array(2);
n[0] = a;
n[1] = b;
console.log(a / b, n[0] / n[1]);
// Console: 0.7620137299771166, 0.7620136992148795
@Gerjo
Gerjo / gist:7327444
Last active December 27, 2015 12:39
Ezelsbruggetje
We remove the operators, just keep the order of digits.
0123 1230 2013
0321 0123 2301
1302 0123 0123
Instant memorize the repeated numbers, so you are left with
1230 2013
0321 2301
1302
@Gerjo
Gerjo / template specialisation
Created November 4, 2013 14:45
Javascript template specialisation.
function GenMatrix(rows, cols) {
function Matrix() {
// Whatever prefered way of storing you may have.
this.nums = new Array(rows * cols);
}
if(rows == 2 && cols == 2) {
Matrix.prototype.inverse = function() {
// optimize matrix inverse code for 2x2
}
class actor {
x = 100;
y = 100;
rotate = 45 degrees;
scale = 2;
draw(renderer) {
renderer.drawrectangle(0, 0, 100, 100);
}
}
class actor {
x = 100;
y = 100;
draw(renderer) {
renderer.drawpixel(0, 0);
}
}
@Gerjo
Gerjo / preamble.tex
Created October 21, 2013 19:44
Latex boilerplate preamble. Dump file for my common settings. Doesn't compile.
\documentclass[11pt, a4paper, twoside, onecolumn]{book}
%\columnsep +30pt
\usepackage[margin=70pt]{geometry}
\usepackage{graphicx}
\usepackage{verbatim}
\usepackage{wrapfig}
\usepackage[font={it}]{caption}
\usepackage{pgf-pie}
\usepackage{tabu}
\usepackage{url}
function DeltaRelativeRadians(a, b) {
var delta = b - a;
return delta + ((delta > Math.PI) ? -Math.TwoPI : (delta < -Math.PI) ? Math.TwoPI : 0)
}
@Gerjo
Gerjo / gist:6870967
Created October 7, 2013 16:38
function with methods
Function.prototype.derp = function() {
console.log("derp!");
return this;
}
// A functions with methods? Sure.
var m = function () {
console.log("whoop");
}.derp(); // prints "derp!"
@Gerjo
Gerjo / gist:6870915
Created October 7, 2013 16:34
Function with properties.
function MehBuilder() {
// Ok... using an array b.c. strings are immutable.
var foo = Meh.foo = ["derp"];
function Meh() {
console.log(foo[0]);
}
return Meh;
@Gerjo
Gerjo / HTML colors
Created August 27, 2013 13:42
Optimising a HTML particle system, when suddenly everything must be stringly typed.
color = "rgba(" + lerpInt(particle.colorA[0], particle.colorB[0], t) + ", " +
lerpInt(particle.colorA[1], particle.colorB[1], t) + ", " +
lerpInt(particle.colorA[2], particle.colorB[2], t) + ", " +
lerpFloat(particle.colorA[3], particle.colorB[3], t) + ")";