diff --git a/index.js b/index.js index 1e160e9..ba01cdd 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.9.3 (function() { - exports.affine = require('./lib/affine'); exports.polygon = require('./lib/polygon'); diff --git a/lib/affine.js b/lib/affine.js index 3f125bc..896fd56 100644 --- a/lib/affine.js +++ b/lib/affine.js @@ -1,9 +1,9 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.9.3 (function() { var affine2d, flipX, flipY, posRotScale, reflection, reflectionUnit, rotation, scaling, translation, - __slice = [].slice, - __hasProp = {}.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + slice = [].slice, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; exports.compose = function(a1, a2) { var res; @@ -13,10 +13,9 @@ }; affine2d = (function() { - function affine2d() { var args; - args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + args = 1 <= arguments.length ? slice.call(arguments, 0) : []; if (args.length === 0) { this.m00 = 1; this.m01 = 0; @@ -42,7 +41,7 @@ } affine2d.prototype.oneLineSummary = function() { - return ("M = [" + (this.m00.toPrecision(3))) + (" " + (this.m01.toPrecision(3))) + (" " + (this.m10.toPrecision(3))) + (" " + (this.m11.toPrecision(3)) + "] V = (") + ("" + (this.v0.toPrecision(3)) + ", ") + (" " + (this.v1.toPrecision(3)) + ") scale = ") + this.getXScale().toPrecision(3) + " x " + this.getYScale().toPrecision(3); + return ("M = [" + (this.m00.toPrecision(3))) + (" " + (this.m01.toPrecision(3))) + (" " + (this.m10.toPrecision(3))) + (" " + (this.m11.toPrecision(3)) + "] V = (") + ((this.v0.toPrecision(3)) + ", ") + (" " + (this.v1.toPrecision(3)) + ") scale = ") + this.getXCenter().toPrecision(3) + " x " + this.getYCenter().toPrecision(3); }; affine2d.prototype.copy = function() { @@ -50,18 +49,20 @@ }; affine2d.prototype.setCtxTransform = function(ctx) { + /* - if you have an HTML5 canvas and want to **set** - its transform to match this affine, use this - */ + if you have an HTML5 canvas and want to **set** + its transform to match this affine, use this + */ return ctx.setTransform(this.m00, this.m10, this.m01, this.m11, this.v0, this.v1); }; affine2d.prototype.applyToCtx = function(ctx) { + /* - if you have an HTML5 canvas and want to **apply** - this transform to its existing one, use this - */ + if you have an HTML5 canvas and want to **apply** + this transform to its existing one, use this + */ return ctx.transform(this.m00, this.m10, this.m01, this.m11, this.v0, this.v1); }; @@ -77,18 +78,19 @@ t0 = this.m00 * a[0] + this.m01 * a[1] + this.v0; t1 = this.m10 * a[0] + this.m11 * a[1] + this.v1; a[0] = t0; - return a[1] = t1; + a[1] = t1; + return a; }; affine2d.prototype.rightComposeWith = function(a) { - /* - Typically when you have an affine A and you want to - perform another affine on it, use this. - In other words: - A.rightComposeWith(B) - performs the composition B(A) and replaces A with the results. - */ + /* + Typically when you have an affine A and you want to + perform another affine on it, use this. + In other words: + A.rightComposeWith(B) + performs the composition B(A) and replaces A with the results. + */ var t_m00, t_m01, t_m10, t_m11, t_v0, t_v1; t_m10 = a.m00 * this.m10 + a.m10 * this.m11; t_m11 = a.m01 * this.m10 + a.m11 * this.m11; @@ -105,11 +107,11 @@ }; affine2d.prototype.leftComposeWith = function(a) { - /* - A.leftComposeWith(B) - performs the composition A(B) and replaces A with the results - */ + /* + A.leftComposeWith(B) + performs the composition A(B) and replaces A with the results + */ var t_m00, t_m01, t_m10, t_m11, t_v0, t_v1; t_m10 = this.m00 * a.m10 + this.m10 * a.m11; t_m11 = this.m01 * a.m10 + this.m11 * a.m11; @@ -123,21 +125,21 @@ this.m11 = t_m11; this.v0 = t_v0; return this.v1 = t_v1; - /* - a = m00 - b = m10 - d = m01 - e = m11 - */ + /* + a = m00 + b = m10 + d = m01 + e = m11 + */ }; affine2d.prototype.deconstruct = function() { - /* - returns [xscale, yscale, yshearing, rotation, xtranslation, ytranslation] - assuming they are applied in that order - */ + /* + returns [xscale, yscale, yshearing, rotation, xtranslation, ytranslation] + assuming they are applied in that order + */ var rotation, xscale, xtranslation, yscale, yshear, ytranslation; xscale = Math.sqrt(this.m00 * this.m00 + this.m10 * this.m10); yscale = (this.m00 * this.m11 - this.m10 * this.m01) / xscale; @@ -149,7 +151,8 @@ }; affine2d.prototype.toPosRotScale = function() { - /* + + /* this only makes sense if you know your affine only consists of a position, rotation, and scaling (i.e., no shearing). @@ -162,8 +165,7 @@ - they're applied in the above order, which is a natural way of thinking of thinking of object placement Note that you can then extract the pos, rot, and scaling from a posRotScale (or modified one) by using super's toPosRotScale function - */ - + */ var xscale; xscale = Math.sqrt(this.m00 * this.m00 + this.m10 * this.m10); return { @@ -182,7 +184,7 @@ }; affine2d.prototype.scale = function(scale1, scale2) { - if (!(scale2 != null)) { + if (scale2 == null) { scale2 = scale1; } return this.rightComposeWith(new scaling(scale1, scale2)); @@ -216,9 +218,8 @@ })(); - rotation = (function(_super) { - - __extends(rotation, _super); + rotation = (function(superClass) { + extend(rotation, superClass); function rotation(r) { rotation.__super__.constructor.call(this, Math.cos(r), -Math.sin(r), Math.sin(r), Math.cos(r), 0, 0); @@ -228,9 +229,8 @@ })(affine2d); - scaling = (function(_super) { - - __extends(scaling, _super); + scaling = (function(superClass) { + extend(scaling, superClass); function scaling(sx, sy) { scaling.__super__.constructor.call(this, sx, 0, 0, sy, 0, 0); @@ -240,9 +240,8 @@ })(affine2d); - translation = (function(_super) { - - __extends(translation, _super); + translation = (function(superClass) { + extend(translation, superClass); function translation(x, y) { translation.__super__.constructor.call(this, 1, 0, 0, 1, x, y); @@ -252,9 +251,8 @@ })(affine2d); - reflectionUnit = (function(_super) { - - __extends(reflectionUnit, _super); + reflectionUnit = (function(superClass) { + extend(reflectionUnit, superClass); function reflectionUnit(ux, uy) { reflectionUnit.__super__.constructor.call(this, 2.0 * ux * ux - 1.0, 2.0 * ux * uy, 2.0 * ux * uy, 2.0 * uy * uy - 1.0, 0.0, 0.0); @@ -264,9 +262,8 @@ })(affine2d); - reflection = (function(_super) { - - __extends(reflection, _super); + reflection = (function(superClass) { + extend(reflection, superClass); function reflection(r) { reflection.__super__.constructor.call(this, Math.cos(r, Math.sin(r))); @@ -276,9 +273,8 @@ })(reflectionUnit); - flipX = (function(_super) { - - __extends(flipX, _super); + flipX = (function(superClass) { + extend(flipX, superClass); function flipX() { flipX.__super__.constructor.call(this, -1, 0, 0, 1, 0, 0); @@ -288,9 +284,8 @@ })(affine2d); - flipY = (function(_super) { - - __extends(flipY, _super); + flipY = (function(superClass) { + extend(flipY, superClass); function flipY() { flipY.__super__.constructor.call(this, 1, 0, 0, -1, 0, 0); @@ -300,26 +295,25 @@ })(affine2d); - posRotScale = (function(_super) { + posRotScale = (function(superClass) { + extend(posRotScale, superClass); - __extends(posRotScale, _super); /* - a posRotScale consists of a: - 1. translation - 2. rotation - 3. scaling - i.e. - - no shearing - - they're applied in the above order, which is a natural way of thinking of thinking of object placement - Note that you can then extract the pos, rot, and scaling from a posRotScale (or modified one) - by using super's toPosRotScale function - */ - - - function posRotScale(_arg) { + a posRotScale consists of a: + 1. translation + 2. rotation + 3. scaling + i.e. + - no shearing + - they're applied in the above order, which is a natural way of thinking of thinking of object placement + Note that you can then extract the pos, rot, and scaling from a posRotScale (or modified one) + by using super's toPosRotScale function + */ + + function posRotScale(arg) { var pos, rot, scale; - pos = _arg.pos, rot = _arg.rot, scale = _arg.scale; + pos = arg.pos, rot = arg.rot, scale = arg.scale; posRotScale.__super__.constructor.call(this); this.translate(pos[0], pos[1]); this.rotate(rot); diff --git a/lib/polygon.js b/lib/polygon.js index 9ce8f10..7a53eb1 100644 --- a/lib/polygon.js +++ b/lib/polygon.js @@ -1,11 +1,10 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.9.3 (function() { var affine, polygon; affine = require('./affine'); polygon = (function() { - function polygon(vertices) { if (vertices != null) { this.vertices = vertices; @@ -15,11 +14,11 @@ } polygon.prototype.copy = function() { - var new_v, v, _i, _len, _ref; + var j, len, new_v, ref, v; new_v = []; - _ref = this.vertices; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - v = _ref[_i]; + ref = this.vertices; + for (j = 0, len = ref.length; j < len; j++) { + v = ref[j]; new_v.push(v.copy()); } return new polygon(new_v); @@ -30,30 +29,30 @@ }; polygon.prototype.transform = function(aff) { - var v, _i, _len, _ref, _results; - _ref = this.vertices; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - v = _ref[_i]; - _results.push(aff.transformVec(v)); + var j, len, ref, results, v; + ref = this.vertices; + results = []; + for (j = 0, len = ref.length; j < len; j++) { + v = ref[j]; + results.push(aff.transformVec(v)); } - return _results; + return results; }; polygon.prototype.getBoundingRectangle = function() { - /* - returns a pair of pairs; - for example: [[1,2],[3,5]] - means that 1 <= x <= 3 - and 2 <= y <= 5 - for all points - */ - var i, v, x, _i, _len, _ref; + /* + returns a pair of pairs; + for example: [[1,2],[3,5]] + means that 1 <= x <= 3 + and 2 <= y <= 5 + for all points + */ + var i, j, len, ref, v, x; x = null; - _ref = this.vertices; - for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { - v = _ref[i]; + ref = this.vertices; + for (i = j = 0, len = ref.length; j < len; i = ++j) { + v = ref[i]; if (i === 0) { x = [[v[0], v[1]], [v[0], v[1]]]; } else { @@ -85,11 +84,11 @@ return new polygon([[0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5], [0.5, -0.5]]); }, unitCircleApprox: function(num) { - var i, p, radian_increment, radians, _i; + var i, j, p, radian_increment, radians, ref; radian_increment = 2.0 * Math.PI / num; radians = 0; p = new polygon(); - for (i = _i = 0; 0 <= num ? _i < num : _i > num; i = 0 <= num ? ++_i : --_i) { + for (i = j = 0, ref = num; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) { radians += radian_increment; p.addVertex([0.5 * Math.cos(radians), 0.5 * Math.sin(radians)]); } diff --git a/src/affine.coffee b/src/affine.coffee index 017799c..5133b18 100644 --- a/src/affine.coffee +++ b/src/affine.coffee @@ -10,7 +10,7 @@ # which executes like this: # # [ px' ] [ M00 M01 ] [ px ] [ v0 ] -# [ ] = [ ] * [ ] + [ ] +# [ ] = [ ] * [ ] + [ ] # [ py' ] [ M10 M11 ] [ py ] [ v1 ] # # Commonly we represent the transformation as one matrix, like this: @@ -64,8 +64,8 @@ class affine2d " #{@m11.toPrecision(3)}] V = (" + "#{@v0.toPrecision(3)}, " + " #{@v1.toPrecision(3)}) scale = " + - @getXScale().toPrecision(3) + " x " + - @getYScale().toPrecision(3) + @getXCenter().toPrecision(3) + " x " + + @getYCenter().toPrecision(3) copy: -> new affine2d @ @@ -95,14 +95,15 @@ class affine2d t1 = @m10 * a[0] + @m11 * a[1] + @v1 a[0] = t0 a[1] = t1 + a rightComposeWith: (a) -> ### - Typically when you have an affine A and you want to + Typically when you have an affine A and you want to perform another affine on it, use this. In other words: A.rightComposeWith(B) - performs the composition B(A) and replaces A with the results. + performs the composition B(A) and replaces A with the results. ### t_m10 = a.m00 * @m10 + a.m10 * @m11 t_m11 = a.m01 * @m10 + a.m11 * @m11 @@ -163,7 +164,7 @@ class affine2d ] toPosRotScale: -> - ### + ### this only makes sense if you know your affine only consists of a position, rotation, and scaling (i.e., no shearing). @@ -175,7 +176,7 @@ class affine2d - no shearing - they're applied in the above order, which is a natural way of thinking of thinking of object placement Note that you can then extract the pos, rot, and scaling from a posRotScale (or modified one) - by using super's toPosRotScale function + by using super's toPosRotScale function ### xscale = Math.sqrt (@m00 * @m00 + @m10 * @m10) @@ -251,7 +252,7 @@ class posRotScale extends affine2d - no shearing - they're applied in the above order, which is a natural way of thinking of thinking of object placement Note that you can then extract the pos, rot, and scaling from a posRotScale (or modified one) - by using super's toPosRotScale function + by using super's toPosRotScale function ### constructor: ({pos, rot, scale})-> super()