Referencing some of the math code from HTML5 Rocks.