forked from github/game-off-2012
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoonening.js
2968 lines (2859 loc) · 125 KB
/
moonening.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function initMoonening() {
(function initModCompilation(window) {
var modules = {};/// Vector from bang::Geometry/Vector.js
modules.Vector = (function initVector() {
/** * *
* Creates a new instance of vector.
* * **/
function Vector() {
this.length = arguments.length;
for (var i=0; i < arguments.length; i++) {
this[i] = arguments[i];
}
}
Vector.prototype = [];
Vector.prototype.constructor = Vector;
//--------------------------------------
// METHODS
//--------------------------------------
/** * *
* Returns the string representation of the Vector.
* @return {string}
* * **/
Vector.prototype.toString = function Vector_toString() {
return 'Vector['+Array.prototype.toString.call(this)+']';
};
/** * *
* Folds left (starting at zero) along the vector using function f,
* which takes an accumulator and an element and returns
* a new accumulator value - acc is the initial accumulator
* value. Returns the resulting accumulator.
* @param function (accumulator, element)
* @param object
* @nosideeffects
* * **/
Vector.prototype.foldl = function Vector_foldl(f, acc) {
for (var i=0; i < this.length; i++) {
acc = f(acc, this[i]);
}
return acc;
};
/** * *
* Folds right (starting at this.length) along the vector
* using function f, which takes an element and an accumulator
* and returns a new accumulator value - acc is the initial accumulator
* value. Returns the resulting accumulator.
* @param function (element, accumulator)
* @param object
* @nosideeffects
* * **/
Vector.prototype.foldr = function Vector_foldr(f, acc) {
for (var i = this.length - 1; i >= 0; i--){
acc = f(this[i], acc);
}
return acc;
};
/** * *
* Maps a function over the vector, creating a new vector
* with the return values.
* @param {function(*, number, Array.<*>)}
* @return {Vector}
* @nosideeffects
* * **/
Vector.prototype.map = function Vector_map(f) {
var copy = new this.constructor();
for (var i = this.length - 1; i >= 0; i--){
copy[i] = f(this[i],i,this);
}
copy.length = this.length;
return copy;
};
/** * *
* Maps function f over all elements, collecting elements
* in a new array that return true when fed to f, until f returns false.
* @param {function(*, number): *}
* @return {Vector}
* @nosideeffects
* * **/
Vector.prototype.takeWhile = function Vector_takeWhile(f) {
var a = new this.constructor();
var i = 0;
for (i; i < this.length; i++) {
var element = this[i];
if (f(element, i)) {
a[i] = this[i];
} else {
break;
}
}
a.length = i;
return a;
};
/** * *
* Returns whether or not array a is equal to this.
* @param {Vector}
* @return {boolean}
* @nosideeffects
* * **/
Vector.prototype.isEqualTo = function Vector_isEqualTo(a) {
if (this.length !== a.length) {
return false;
}
var b = this.takeWhile(function (el, ndx) {
return el === a[ndx];
});
return this.length === b.length;
};
/** * *
* Returns a copy of this vector.
* @return {Vector}
* @nosideeffects
* * **/
Vector.prototype.copy = function Vector_copy() {
return this.takeWhile(function (el) {
return true;
});
};
/** * *
* Returns the addition of Vector v and this.
* @param {Vector}
* @return {Vector}
* @nosideeffects
* * **/
Vector.prototype.add = function Vector_add(v) {
return this.foldl(function (acc, element) {
acc.push(element + v[acc.length]);
return acc;
}, new this.constructor());
};
/** * *
* Returns the subtraction of Vector v from this.
* @param {Vector}
* @return {Vector}
* @nosideeffects
* * **/
Vector.prototype.subtract = function Vector_subtract(v) {
return this.foldl(function (acc, element) {
acc.push(element - v[acc.length]);
return acc;
}, new this.constructor());
};
/** * *
* Gets and sets the x value.
* @param {number}
* @return {number}
* * **/
Vector.prototype.x = function Vector_x(x) {
if (arguments.length) {
this[0] = x;
}
return this[0];
};
/** * *
* Gets and sets the y value.
* @param {number}
* @return {number}
* * **/
Vector.prototype.y = function Vector_y(y) {
if (arguments.length) {
this[1] = y;
}
return this[1];
};
/** * *
* Returns the magnitude of this vector.
* @return {number}
* @nosideeffects
* * **/
Vector.prototype.magnitude = function Vector_magnitude() {
var total = this.foldr(function (element, acc) {
return element*element+acc;
}, 0);
return Math.sqrt(total);
};
/** * *
* Returns the unit vector of this vector.
* @return {Vector}
* @nosideeffects
* * **/
Vector.prototype.normalize = function Vector_normalize() {
var magnitude = this.magnitude();
return this.foldr(function (element, acc) {
acc.unshift(element/magnitude);
return acc;
}, new this.constructor());
};
/** * *
* Returns the point at a given index. Zero indexed.
* For example, pointAt(1) would return the second point.
* @param {number}
* @return {Vector}
* * **/
Vector.prototype.pointAt = function Vector_pointAt(n) {
var ndx = 2*n;
return new Vector(this[ndx], this[ndx+1]);
};
/** * *
* Returns the intersection point of two lines or false
* if they do not intersect.
* @param {Vector}
* @param {Vector}
* @return {Vector|false}
* * **/
Vector.lineIntersection = function Vector_lineIntersection(vec1, vec2) {
var p1 = vec1.pointAt(0);
var p2 = vec1.pointAt(1);
var p3 = vec2.pointAt(0);
var p4 = vec2.pointAt(1);
/** * *
* Returns the equation of a line segment formed by p1->p2.
* @return {Object}
* * **/
function getLineEquation(p1, p2) {
// y = mx + b
// b = y - mx
// x = (y - b) / m
var m = (p2.y() - p1.y()) / (p2.x() - p1.x());
var b = p2.y() - m*p2.x();
return {
// slope, rise over run...
m : m,
b : b,
xAty : function(y) {
return (y - b) / m;
},
yAtx : function(x) {
return m*x + b;
}
};
}
var e1,e2,x,y;
e1 = getLineEquation(p1, p2);
e2 = getLineEquation(p3, p4);
if (e1.m === e2.m) {
// These lines are co-linear and do not intersect...
return false;
} else if (Math.abs(e1.m) === Number.POSITIVE_INFINITY) {
x = p2.x();
y = e2.yAtx(x);
} else if (Math.abs(e2.m) === Number.POSITIVE_INFINITY) {
x = p4.x();
y = e1.yAtx(x);
} else {
x = e2.b / (e1.m + e1.b - e2.m);
y = e2.yAtx(x);
}
return new Vector(x, y);
};
return Vector;
})();
/// Polygon from bang::Geometry/Polygon.js
modules.Polygon = (function initPolygon(Vector) {
/** * *
* Creates a new polygon
* @param ...
* @return {Polygon}
* @constructor
* * **/
function Polygon() {
Vector.apply(this, Array.prototype.slice.call(arguments));
}
Polygon.prototype = new Vector();
Polygon.prototype.constructor = Polygon;
//--------------------------------------
// METHODS
//--------------------------------------
/** * *
* Returns the string representation of the polygon.
* @return {string}
* * **/
Polygon.prototype.toString = function() {
return 'Polygon['+Array.prototype.toString.call(this)+']';
};
/** * *
* Returns whether or not the polygon contains the point p.
* @param {Vector}
* @returns {boolean}
* @nosideeffects
* * **/
Polygon.prototype.containsPoint = function Polygon_containsPoint(p) {
var x = p[0];
var y = p[1];
var xp = [];
var yp = [];
var ncomp = this.length;
var npol = ncomp/2;
for (var i = 0; i < ncomp; i+=2) {
xp.push(this[i]);
yp.push(this[i+1]);
}
var j = npol - 1;
var c = false;
for (i = 0; i < npol; j = i++) {
if ((((yp[i] <= y) && (y < yp[j])) ||
((yp[j] <= y) && (y < yp[i]))) &&
(x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i])) {
c = !c;
}
}
return c;
};
return Polygon;
})(modules.Vector);
/// Rectangle from bang::Geometry/Rectangle.js
modules.Rectangle = (function initRectangle(Vector, Polygon) {
/** * *
* Creates a new rectangle at (x,y) with width w and height h.
* @param {number}
* @param {number}
* @param {number}
* @param {number}
* @return Rectangle
* @nosideeffects
* @constructor
* * **/
function Rectangle(x,y,w,h) {
x = x || 0;
y = y || 0;
w = w || 0;
h = h || 0;
this.left(x);
this.top(y);
this.width(w);
this.height(h);
this.length = 8;
}
Rectangle.prototype = new Polygon();
Rectangle.prototype.constructor = Rectangle;
//--------------------------------------
// METHODS
//--------------------------------------
/** * *
* Returns the string representation of this Rectangle.
* @return {string}
* * **/
Rectangle.prototype.toString = function Rectangle_toString() {
return 'Rectangle['+this.left()+','+this.top()+','+this.width()+','+this.height()+']';
};
/** * *
* Gets and sets the left edge x value.
* @param {number}
* @return {number}
** * */
Rectangle.prototype.left = function Rectangle_left(l) {
if (typeof l === 'number') {
this[0] = l;
this[6] = l;
}
return this[0];
};
/** * *
* Gets and sets the top edge y value.
* @param {number}
* @return {number}
** * */
Rectangle.prototype.top = function Rectangle_top(t) {
if (typeof t === 'number') {
this[1] = t;
this[3] = t;
}
return this[1];
};
/** * *
* Gets and sets the right edge x value.
* @param {number}
* @return {number}
* * **/
Rectangle.prototype.right = function Rectangle_right(r) {
if (typeof r === 'number') {
this[2] = r;
this[4] = r;
}
return this.left() + this.width();
};
/** * *
* Gets and sets the bottom edge y value.
* @param {number}
* @return {number}
* * **/
Rectangle.prototype.bottom = function Rectangle_bottom(b) {
if (typeof b === 'number') {
this[5] = b;
this[7] = b;
}
return this.top() + this.height();
};
/** * *
* Gets and sets the width of this Rectangle.
* @param {number}
* @return {number}
* * **/
Rectangle.prototype.width = function Rectangle_width(w) {
if (typeof w === 'number') {
var x = this.left() + w;
this[2] = x;
this[4] = x;
}
return this[2] - this[0];
};
/** * *
* Returns the height of this Rectangle
* @param {number}
* @return {number}
* * **/
Rectangle.prototype.height = function Rectangle_height(h) {
if (typeof h === 'number') {
var y = this.top() + h;
this[5] = y;
this[7] = y;
}
return this[5] - this[1];
};
/** * *
* Returns the area (in pixels^2) of this rectangle
* @return {number}
* @nosideeffects
* * **/
Rectangle.prototype.area = function Rectangle_area () {
return this.width() * this.height();
};
/** * *
* Returns whether or not this rectangle intersects rectangle r.
* Rectangles that share the same edge are considerend NOT intersecting.
* @param {Rectangle}
* @return {boolean}
* @nosideeffects
* * **/
Rectangle.prototype.intersectsRectangle = function Rectangle_intersectsRectangle(r) {
return !(this.left() >= r.right() ||
r.left() >= this.right() ||
this.top() >= r.bottom() ||
r.top() >= this.bottom());
};
/** * *
* Returns whether or not this rectangle contains rectangle r.
* If rectangle r is equal it is considered contained within this rectangle (and visa versa).
* @param {Rectangle}
* @return {boolean}
* @nosideeffects
* * **/
Rectangle.prototype.containsRectangle = function Rectangle_contains(r) {
return this.left() <= r.left() && this.top() <= r.top() && this.right() >= r.right() && this.bottom() >= r.bottom();
};
/** * *
* Returns the intersection of this rectangle with rectangle r. If the rectangles
* Returns false if r does not intersect.
* @param {Rectangle}
* @return {Rectangle|false}
* * **/
Rectangle.prototype.intersectionWith = function Rectangle_intersectionWith(r) {
if (this.intersectsRectangle(r)) {
return new Rectangle();
}
return false;
};
/** * *
* Returns whether or not this rectangle contains point.
* @param {Vector}
* @return {boolean}
* * **/
Rectangle.prototype.containsPoint = function Rectangle_containsPoint(point) {
return (this.left() <= point.x() && this.right() >= point.x() &&
this.top() <= point.y() && this.bottom() >= point.y());
};
/** * *
* Returns a polygon clipped by this rectangle using a Sutherland-Hodgeman Algorithm.
* https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
* http://www.aftermath.net/articles/clippoly/index.html
* @param {Polygon}
* @return {Polygon}
* * **/
Rectangle.prototype.clipPolygon = function Rectangle_clipPolygon(polygon) {
var CP_LEFT = 0;
var CP_RIGHT = 1;
var CP_TOP = 2;
var CP_BOTTOM = 3;
function cp_inside(p, r, side) {
switch(side) {
case CP_LEFT:
return p.x() >= r.left();
case CP_RIGHT:
return p.x() <= r.right();
case CP_TOP:
return p.y() >= r.top();
case CP_BOTTOM:
return p.y() <= r.bottom();
}
}
function cp_intersect(p, q, r, side) {
function isfinite(n) {
return Math.abs(n) !== Number.POSITIVE_INFINITY;
}
var t = new Vector(0, 0);
var a, b;
/* find slope and intercept of segment pq */
a = ( q.y() - p.y() ) / ( q.x() - p.x() );
b = p.y() - p.x() * a;
switch(side) {
case CP_LEFT:
t.x(r.left());
t.y(t.x() * a + b);
break;
case CP_RIGHT:
t.x(r.right());
t.y(t.x() * a + b);
break;
case CP_TOP:
t.y(r.top());
t.x(isfinite(a) ? ( t.y() - b ) / a : p.x());
break;
case CP_BOTTOM:
t.y(r.bottom());
t.x(isfinite(a) ? ( t.y() - b ) / a : p.x());
break;
}
return t;
}
function cp_clipplane(vec, r, side) {
var n, i, j=0, s, p, out, intersection;
n = vec.length/2;
out = new Polygon();
s = vec.pointAt(n-1);
for(i = 0 ; i < n; i++ ) {
p = vec.pointAt(i);
if( cp_inside( p, r, side ) ) {
// Point p is inside...
if( !cp_inside( s, r, side ) ) {
// p is inside and s is outside
intersection = cp_intersect( p, s, r, side );
if (!intersection.isEqualTo(p)) {
// We don't want doubles in our output set...
out.push(intersection[0]);
out.push(intersection[1]);
}
}
out.push(p[0]);
out.push(p[1]);
} else if( cp_inside( s, r, side ) ) {
// s is inside and p is outside
intersection = cp_intersect( p, s, r, side );
out.push(intersection[0]);
out.push(intersection[1]);
}
s = p;
}
return out;
}
function clippoly(poly, r) {
poly = cp_clipplane(poly, r, CP_LEFT);
poly = cp_clipplane(poly, r, CP_RIGHT);
poly = cp_clipplane(poly, r, CP_TOP);
poly = cp_clipplane(poly, r, CP_BOTTOM);
return poly;
}
return clippoly(polygon, this);
};
/** * *
* Returns a new set of rectangles that do not intersect, that occupy the
* same space as the original.
* @param {Array}
* @return {Array}
* @nosideeffects
* * **/
Rectangle.reduceRectangles = function Rectangle_reduceRectangles(rectangles) {
/** * *
* Bundles and sorts rectangles by vertical edges.
* @param {Array}
* @nosideeffects
* * **/
function bundleRectangles(rectangles) {
var output = [];
for (var i=0; i < rectangles.length; i++) {
var rect = rectangles[i];
output.push({
id : i,
type : 's',
x : rect.left(),
intersections : [],
rectangle : rect
});
output.push({
id : i,
type : 'e',
x : rect.right(),
intersections : [],
rectangle : rect
});
}
function compareX(n1, n2) {
var diff = n1.x - n2.x;
if (diff === 0) {
return n1.top - n2.top;
}
return diff;
}
output.sort(compareX);
return output;
}
// We're going to need this function during end events...
function mapIntersectionToScan(el) {
var newScan = new Rectangle(e.x, el.top(), 0, el.height());
newScan.intersects = [el];
return newScan;
}
var input = rectangles.slice();
var events = bundleRectangles(input);
var current = [];
var scans = [];
var output = [];
var i,j,k,l,scan,currentRect;
for (i=0; i < events.length; i++) {
var e = events[i];
// Update the current scans...
for (j=0; j < scans.length; j++) {
scan = scans[j];
scan.right(e.x);
}
if (e.type === 's') {
var contained = false;
for (j=0; j < current.length; j++) {
currentRect = current[j];
if (currentRect.containsRectangle(e.rectangle)) {
contained = true;
break;
}
}
if (!contained) {
// This event should start a new rectangle (at some point)...
current.push(e.rectangle);
var intersected = false;
for (j=0; j < scans.length; j++) {
scan = scans[j];
if (!(e.rectangle.top() >= scan.bottom() || scan.top() >= e.rectangle.bottom())) {
// The scan and event intersect in y...
if ((e.rectangle.top() >= scan.top() && e.rectangle.bottom() <= scan.bottom())) {
// This scan contains the event's rectangle, so we don't need to make a new scan
// but we will need to know that it intersects...
scan.intersects.push(e.rectangle);
contained = true;
continue;
}
// We have to check if it has a width, if not, it was added by an earlier intersection
// during this event...
if (scan.width()) {
// This scan was from an earlier event, so output a rectangle...
output.push(scan);
}
// Replace this scan with a scan that represents the intersection...
var t = Math.min(e.rectangle.top(), scan.top());
var b = Math.max(e.rectangle.bottom(), scan.bottom());
if (intersected) {
// We've already created one intersection, so group it with this one...
intersected.top(Math.min(intersected.top(), t));
intersected.bottom(Math.max(intersected.bottom(), b));
intersected.intersects = intersected.intersects.concat(scan.intersects);
} else {
intersected = new Rectangle(e.x, t, 0, b - t);
intersected.intersects = scan.intersects.concat([e.rectangle]);
}
// Get rid of the current scan...
scans.splice(j--, 1);
}
}
if (!contained) {
// This rectangle is not contained by a current scan either...
if (!intersected) {
// This event starts a new non-overlapping rectangle...
var r = e.rectangle.copy();
r.intersects = [e.rectangle];
scans.push(r);
} else {
scans.push(intersected);
}
}
}
} else {
// This is an end event...
for (j=0; j < current.length; j++) {
currentRect = current[j];
if (currentRect.isEqualTo(e.rectangle)) {
current.splice(j, 1);
break;
}
}
for (j=0; j < scans.length; j++) {
scan = scans[j];
// Find the scan that intersects this end event...
if (scan.intersects.length === 1 && scan.top() === e.rectangle.top() && scan.bottom() === e.rectangle.bottom()) {
// This end event corresponds specifically to this scan, so output it...
output.push(scan);
scans.splice(j--, 1);
} else {
// We're going to have to break this scan up into subscans...
var ndx = scan.intersects.indexOf(e.rectangle);
if (ndx !== -1) {
// Output the scan, because we have to change it...
output.push(scan);
// Remove this rectangle from the intersecting scan...
scan.intersects.splice(ndx, 1);
// Remove the scan from our scans...
scans.splice(j, 1);
if (scan.intersects.length) {
// Make some new scans to replace the old one...
var newScans = scan.intersects.map(mapIntersectionToScan);
// Iterate through the new scans and collect them together if they overlap...
var newScanNdx = 0;
while (newScans.length > 1 && newScanNdx < newScans.length) {
var intersection = newScans[newScanNdx++];
for (l=0; l < newScans.length; l++) {
if (l === newScanNdx-1) {
// We don't want to compare a scan to itthis...
continue;
}
var newScan = newScans[l];
if (!(newScan.top() > intersection.bottom() || intersection.top() > newScan.bottom())) {
// The two overlap, so absorb the intersection scan into this scan...
intersection.top(Math.min(newScan.top(), intersection.top()));
intersection.bottom(Math.max(newScan.bottom(), intersection.bottom()));
intersection.intersects = newScan.intersects.concat(intersection.intersects);
// Now we have to test this scan against the others again, since it has changed...
newScans.splice(l, 1);
newScanNdx = 0;
break;
}
}
}
// Add the new scans to scans...
scans = scans.concat(newScans);
}
break;
}
}
}
}
}
// Concat the remaining scans in there...
return output.concat(scans);
};
return Rectangle;
})(modules.Vector,modules.Polygon);
/// Matrix from bang::Geometry/Matrix.js
modules.Matrix = (function initMatrix(Vector) {
/** * *
* Creates a new Matrix object. You can optionally provide the number
* of rows and columns as an array, and the elements as numbers.
* @type {Array=} rowsAndColumns The number of rows and columns. (optional)
* @type {...number=} elements The matrix elements. (optional)
* * **/
function Matrix() {
var args = Array.prototype.slice.call(arguments);
var numberOfRowsAndColumns = [0,0];
if (typeof args[0] === 'object') {
numberOfRowsAndColumns = args.shift();
} else if (args.length) {
var sqrtLen = Math.sqrt(args.length);
numberOfRowsAndColumns = [sqrtLen, sqrtLen];
}
/** * *
* The number of columns.
* @type {number}
* * **/
this.numColumns = numberOfRowsAndColumns[0];
/** * *
* The number of rows.
* @type {number}
* * **/
this.numRows = numberOfRowsAndColumns[1];
this.length = this.numRows*this.numColumns;
if (args.length) {
this.length = 0;
for (var i=0; i < args.length; i++) {
this.push(args[i]);
}
} else if (this.length === 0) {
this.length = 9;
this.numColumns = 3;
this.numRows = 3;
this.loadIdentity();
} else {
this.loadIdentity();
}
}
Matrix.prototype = new Vector();
Matrix.prototype.constructor = Matrix;
//--------------------------------------
// CLASS METHODS
//--------------------------------------
/** * *
* Returns the determinant of a given 2x2 matrix.
* @param {Vector}
* @return {number}
* @nosideeffects
* * **/
Matrix.det2x2 = function Matrix_det2x2(matrix) {
if (matrix.length !== 4) {
throw new Error('Matrix must be 2x2.');
}
var a = matrix[0];
var b = matrix[1];
var c = matrix[2];
var d = matrix[3];
return a*d - b*c;
};
//--------------------------------------
// METHODS
//--------------------------------------
/** * *
* Returns the string representation of this Matrix.
* @return {string}
* * **/
Matrix.prototype.toString = function Matrix_toString() {
return 'Matrix['+Array.prototype.toString.call(this)+']';
};
/** * *
* Returns a pretty string value.
* @return string
* @nosideeffects
* * **/
Matrix.prototype.toPrettyString = function Matrix_toPrettyString() {
function fixed(el) {
var s = el.toFixed(3);
if (el >= 0) {
s = ' '+s;
}
return s;
}
var s = '\n' + fixed(this[0]) + ' ' + fixed(this[1]) + ' ' + fixed(this[2]) + '\n';
s += fixed(this[3]) + ' ' + fixed(this[4]) + ' ' + fixed(this[5]) + '\n';
s += fixed(this[6]) + ' ' + fixed(this[7]) + ' ' + fixed(this[8]);
return s;
};
/** * *
* Returns a copy of this matrix.
* @return {Matrix}
* @nosideeffects
* * **/
Matrix.prototype.copy = function Matrix_copy() {
var copy = Vector.prototype.copy.call(this);
copy.numRows = this.numRows;
copy.numColumns = this.numColumns;
return copy;
};
/** * *
* Returns row n of this matrix.
* @return Vector
* @nosideeffects
* * **/
Matrix.prototype.row = function Matrix_row(n) {
var elementsInRow = this.numColumns;
var start = n*elementsInRow;
var row = new Vector();
for (var i=0; i < elementsInRow; i++) {
row.push(this[start+i]);
}
return row;
};
/** * *
* Returns column n of this matrix.
* @return {Vector}
* @nosideeffects
* * **/
Matrix.prototype.column = function Matrix_column(n) {
var elementsInColumn = this.numRows;
var start = n;
var column = new Vector();
for (var i=0; i < elementsInColumn; i++) {
column.push(this[start+this.numRows*i]);
}
return column;
};
/** * *
* Returns the identity matrix.
* @return Matrix
* @nosideeffects
* * **/
Matrix.prototype.identity = function Matrix_identity() {
var m = new this.constructor().loadIdentity();
return m;
};
/** * *
* Loads the elements of the identity matrix. Returns itself.
* @return {Matrix}
* * **/
Matrix.prototype.loadIdentity = function Matrix_loadIdentity() {
if (this.numColumns !== this.numRows) {
// This is not a square matrix and does not have an identity...
throw new Error('Matrix must be square (nxn) to have an identity.');
}
this.length = this.numColumns*this.numRows;
for (var i=0; i < this.length; i++) {
this[i] = (i%(this.numRows+1)) === 0 ? 1 : 0;
}
return this;
};
/** * *
* Returns a new matrix with row x and column y deleted.
* Calling this on a 4x4 matrix will return a 3x3 matrix.
* @param {number} c The column to delete
* @param {number} r The row to delete
* @return {Matrix}
* @nosideeffects
* * **/
Matrix.prototype.deleteRowAndColumnAt = function Matrix_deleteRowAndColumnAt(c, r) {
var alias = this;
/** * *
* Returns whether the given index is in a given row.
* @param {number} ndx The index.
* @param {number} row The row.
* @return {boolean}
* @nosideeffects
* * **/
function isIndexInRow(ndx, row) {
var startNdx = alias.numColumns*row;
var endNdx = startNdx + alias.numColumns;
return startNdx <= ndx && ndx < endNdx;
}
/** * *
* Returns whether the given index is in a given column.
* @param {number} ndx The index.
* @param {number} col The column.
* @return {boolean}
* @nosideeffects
* * **/
function isIndexInColumn(ndx, col) {
return (ndx - col)%alias.numColumns === 0;
}
var matrix = new Matrix([this.numRows-1, this.numColumns-1]);
for (var ndx=0,i=0; ndx < this.length; ndx++) {
if (isIndexInRow(ndx, r) || isIndexInColumn(ndx, c)) {
continue;
}
matrix[i++] = this[ndx];
}
return matrix;
};
/** * *
* Returns the minor for an element at row x and column y.
* @param {number} x The row.
* @param {number} y The column.
* @return {number} The minor for the element.
* * **/
Matrix.prototype.minorAt = function Matrix_minorAt(x, y) {
var ndx = y*this.numColumns + x;
var minorMatrix = this.deleteRowAndColumnAt(x, y);
return minorMatrix.determinant();
};
/** * *
* Returns the cofactor for an element at row x and column y.
* @param {number} x The row.
* @param {number} y The column.
* @return {number} The cofactor for the element.
* * **/
Matrix.prototype.cofactorAt = function Matrix_cofactorAt(x, y) {
var ndx = y*this.numColumns + x;
var sign = ((ndx%2) ? -1 : 1);
if (this.numColumns%2 === 0) {
sign *= (y%2 ? -1 : 1);
}
return this.minorAt(x, y)*sign;
};