Skip to content

Instantly share code, notes, and snippets.

@dylan-k
Last active October 9, 2018 13:36
Show Gist options
  • Save dylan-k/b8901e243f8ec63e108f2d40e103d9ba to your computer and use it in GitHub Desktop.
Save dylan-k/b8901e243f8ec63e108f2d40e103d9ba to your computer and use it in GitHub Desktop.
Margin Calculator
// MARGIN CALCULATOR
// source: https://goo.gl/D3k5Fz
/* here's some javascript, courtesy John Smith, to calculate the margins */
var width = 5.5;
var height = 8.5;
// and here are all the paper sizes you could ever need
// as inputs to the script above to calculate the margins:
// https://en.wikipedia.org/wiki/Paper_size
/* y = mx + b (b = 0) */
var slope = height / width;
/* (x-a)^2 + (y-b)^2 = r^2 */
var radius = width / 2;
var a = radius;
var b = height - radius;
/* ex^2 + fx + g = 0 */
var e = (1 + Math.pow(slope, 2));
var f = (2 * slope * (-b) - 2 * a);
var g = Math.pow(a, 2) + Math.pow(-b, 2) - Math.pow(radius, 2);
/* x1 = (-f + sqrt(f^2 - 4eg)) / 2e */
var x1 = (-f + Math.sqrt(Math.pow(f, 2) - (4 * e * g))) / (2 * e);
/* x1 = (-f - sqrt(f^2 - 4eg)) / 2e */
var x2 = (-f - Math.sqrt(Math.pow(f, 2) - (4 * e * g))) / (2 * e);
/* y1 = mx1 + b (b = 0) */
var y1 = slope * x1;
document.write('height: ' + height + '');
document.write('width: ' + width + '');
document.write('slope: ' + slope + '');
document.write('radius: ' + radius + '');
document.write('a: ' + a + '');
document.write('b: ' + b + '');
document.write('e: ' + e + '');
document.write('f: ' + f + '');
document.write('g: ' + g + '');
document.write('x1: ' + x1 + '');
document.write('x2: ' + x2 + '');
document.write('y1: ' + y1 + '');
document.write('top and bottom margin: ' + (height - y1) + '');
document.write('left and right margin: ' + (width - x1) + '');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment