forked from malgorithms/affine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
affine.js
508 lines (413 loc) · 13.8 KB
/
affine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
(function(/*! Stitch !*/) {
if (!this.require) {
var modules = {}, cache = {}, require = function(name, root) {
var path = expand(root, name), module = cache[path], fn;
if (module) {
return module.exports;
} else if (fn = modules[path] || modules[path = expand(path, './index')]) {
module = {id: path, exports: {}};
try {
cache[path] = module;
fn(module.exports, function(name) {
return require(name, dirname(path));
}, module);
return module.exports;
} catch (err) {
delete cache[path];
throw err;
}
} else {
throw 'module \'' + name + '\' not found';
}
}, expand = function(root, name) {
var results = [], parts, part;
if (/^\.\.?(\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part == '..') {
results.pop();
} else if (part != '.' && part != '') {
results.push(part);
}
}
return results.join('/');
}, dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};
this.require = function(name) {
return require(name, '');
}
this.require.define = function(bundle) {
for (var key in bundle)
modules[key] = bundle[key];
};
}
return this.require.define;
}).call(this)({"affine": function(exports, require, module) {// Generated by CoffeeScript 1.4.0
(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; };
exports.compose = function(a1, a2) {
var res;
res = a2.copy();
res.rightComposeWith(a1);
return res;
};
affine2d = (function() {
function affine2d() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (args.length === 0) {
this.m00 = 1;
this.m01 = 0;
this.m10 = 0;
this.m11 = 1;
this.v0 = 0;
this.v1 = 0;
} else if (args.length === 1) {
this.m00 = args[0].m00;
this.m01 = args[0].m01;
this.m10 = args[0].m10;
this.m11 = args[0].m11;
this.v0 = args[0].v0;
this.v1 = args[0].v1;
} else {
this.m00 = args[0];
this.m01 = args[1];
this.m10 = args[2];
this.m11 = args[3];
this.v0 = args[4];
this.v1 = args[5];
}
}
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);
};
affine2d.prototype.copy = function() {
return new affine2d(this);
};
affine2d.prototype.setCtxTransform = function(ctx) {
/*
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
*/
return ctx.transform(this.m00, this.m10, this.m01, this.m11, this.v0, this.v1);
};
affine2d.prototype.transformPair = function(v0, v1) {
var t0, t1;
t0 = this.m00 * v0 + this.m01 * v1 + this.v0;
t1 = this.m10 * v0 + this.m11 * v1 + this.v1;
return [t0, t1];
};
affine2d.prototype.transformVec = function(a) {
var t0, t1;
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;
};
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.
*/
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;
t_v1 = a.v0 * this.m10 + a.v1 * this.m11 + this.v1;
t_m00 = a.m00 * this.m00 + a.m10 * this.m01;
t_m01 = a.m01 * this.m00 + a.m11 * this.m01;
t_v0 = a.v0 * this.m00 + a.v1 * this.m01 + this.v0;
this.m00 = t_m00;
this.m01 = t_m01;
this.m10 = t_m10;
this.m11 = t_m11;
this.v0 = t_v0;
return this.v1 = t_v1;
};
affine2d.prototype.leftComposeWith = function(a) {
/*
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;
t_v1 = this.v0 * a.m10 + this.v1 * a.m11 + a.v1;
t_m00 = this.m00 * a.m00 + this.m10 * a.m01;
t_m01 = this.m01 * a.m00 + this.m11 * a.m01;
t_v0 = this.v0 * a.m00 + this.v1 * a.m01 + a.v0;
this.m00 = t_m00;
this.m01 = t_m01;
this.m10 = t_m10;
this.m11 = t_m11;
this.v0 = t_v0;
return this.v1 = t_v1;
/*
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
*/
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;
yshear = (this.m00 * this.m01 + this.m10 * this.m11) / (this.m00 * this.m11 - this.m10 * this.m01);
rotation = Math.atan2(this.m10, this.m00);
xtranslation = this.v0;
ytranslation = this.v1;
return [xscale, yscale, yshear, rotation, xtranslation, ytranslation];
};
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).
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
*/
var xscale;
xscale = Math.sqrt(this.m00 * this.m00 + this.m10 * this.m10);
return {
pos: [this.v0, this.v1],
rot: Math.atan2(this.m10, this.m00),
scale: [xscale, (this.m00 * this.m11 - this.m10 * this.m01) / xscale]
};
};
affine2d.prototype.getXCenter = function() {
return this.v0;
};
affine2d.prototype.getYCenter = function() {
return this.v1;
};
affine2d.prototype.scale = function(scale1, scale2) {
if (!(scale2 != null)) {
scale2 = scale1;
}
return this.rightComposeWith(new scaling(scale1, scale2));
};
affine2d.prototype.rotate = function(r) {
return this.rightComposeWith(new rotation(r));
};
affine2d.prototype.translate = function(x, y) {
return this.rightComposeWith(new translation(x, y));
};
affine2d.prototype.flipX = function() {
return this.rightComposeWith(new flipX());
};
affine2d.prototype.flipY = function() {
return this.rightComposeWith(new flipY());
};
affine2d.prototype.reflect = function(angle_or_unit_x, unit_y) {
if (unit_y != null) {
return this.rightComposeWith(new reflectionUnit(angle_or_unit_x, unit_y));
} else {
return this.rightComposeWith(new reflection(angle_or_unit_x));
}
};
return affine2d;
})();
rotation = (function(_super) {
__extends(rotation, _super);
function rotation(r) {
rotation.__super__.constructor.call(this, Math.cos(r), -Math.sin(r), Math.sin(r), Math.cos(r), 0, 0);
}
return rotation;
})(affine2d);
scaling = (function(_super) {
__extends(scaling, _super);
function scaling(sx, sy) {
scaling.__super__.constructor.call(this, sx, 0, 0, sy, 0, 0);
}
return scaling;
})(affine2d);
translation = (function(_super) {
__extends(translation, _super);
function translation(x, y) {
translation.__super__.constructor.call(this, 1, 0, 0, 1, x, y);
}
return translation;
})(affine2d);
reflectionUnit = (function(_super) {
__extends(reflectionUnit, _super);
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);
}
return reflectionUnit;
})(affine2d);
reflection = (function(_super) {
__extends(reflection, _super);
function reflection(r) {
reflection.__super__.constructor.call(this, Math.cos(r, Math.sin(r)));
}
return reflection;
})(reflectionUnit);
flipX = (function(_super) {
__extends(flipX, _super);
function flipX() {
flipX.__super__.constructor.call(this, -1, 0, 0, 1, 0, 0);
}
return flipX;
})(affine2d);
flipY = (function(_super) {
__extends(flipY, _super);
function flipY() {
flipY.__super__.constructor.call(this, 1, 0, 0, -1, 0, 0);
}
return flipY;
})(affine2d);
posRotScale = (function(_super) {
__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) {
var pos, rot, scale;
pos = _arg.pos, rot = _arg.rot, scale = _arg.scale;
posRotScale.__super__.constructor.call(this);
this.translate(pos[0], pos[1]);
this.rotate(rot);
if ((typeof scale) === "number") {
this.scale(scale);
} else if (scale.length === 1) {
this.scale(scale[0]);
} else {
this.scale(scale[0], scale[1]);
}
}
return posRotScale;
})(affine2d);
exports.affine2d = affine2d;
exports.rotation = rotation;
exports.scaling = scaling;
exports.translation = translation;
exports.reflectionUnit = reflectionUnit;
exports.reflection = reflection;
exports.flipX = flipX;
exports.flipY = flipY;
exports.posRotScale = posRotScale;
}).call(this);
}, "polygon": function(exports, require, module) {// Generated by CoffeeScript 1.4.0
(function() {
var affine, polygon;
affine = require('./affine');
polygon = (function() {
function polygon(vertices) {
if (vertices != null) {
this.vertices = vertices;
} else {
this.vertices = [];
}
}
polygon.prototype.copy = function() {
var new_v, v, _i, _len, _ref;
new_v = [];
_ref = this.vertices;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
v = _ref[_i];
new_v.push(v.copy());
}
return new polygon(new_v);
};
polygon.prototype.addVertex = function(v) {
return this.vertices.push(v);
};
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));
}
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;
x = null;
_ref = this.vertices;
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
v = _ref[i];
if (i === 0) {
x = [[v[0], v[1]], [v[0], v[1]]];
} else {
if (v[0] < x[0][0]) {
x[0][0] = v[0];
}
if (v[0] > x[1][0]) {
x[1][0] = v[0];
}
if (v[1] < x[0][1]) {
x[0][1] = v[1];
}
if (v[1] > x[1][1]) {
x[1][1] = v[1];
}
}
}
return x;
};
return polygon;
})();
exports.polygon = polygon;
exports.factory = {
unitSquare: function() {
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;
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) {
radians += radian_increment;
p.addVertex([0.5 * Math.cos(radians), 0.5 * Math.sin(radians)]);
}
return p;
}
};
}).call(this);
}});