1 var Point, _slope, math, normals, unit, util;
3 Point = require('./shapes').Point;
5 util = require('./util');
9 math.toPoly = function(line) {
10 var i, index, len, n, point, polyLeft, polyRight;
14 for (i = 0, len = line.length; i < len; i++) {
16 n = normals(point, _slope(line, index));
17 polyLeft = polyLeft.concat([n[0]]);
18 polyRight = [n[1]].concat(polyRight);
21 return polyLeft.concat(polyRight);
24 _slope = function(line, index) {
26 if (line.length < 3) {
33 point = _slope(line, index + 1);
34 } else if (index === line.length - 1) {
35 point = _slope(line, index - 1);
37 point = math.diff(line[index - 1], line[index + 1]);
42 math.diff = function(a, b) {
49 unit = function(vector) {
51 length = math.len(vector);
58 normals = function(p, slope) {
60 slope.x = slope.x * p.size / 2;
61 slope.y = slope.y * p.size / 2;
75 math.len = function(vector) {
76 return Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
79 math.scalePositionScalar = function(val, viewportSize, oldScale, newScale) {
81 oldSize = viewportSize * oldScale;
82 newSize = viewportSize * newScale;
83 return val + (oldSize - newSize) / 2;
86 module.exports = math;