forked from processing-js/processing-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.js
More file actions
executable file
·21527 lines (19874 loc) · 780 KB
/
processing.js
File metadata and controls
executable file
·21527 lines (19874 loc) · 780 KB
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(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
// build script for generating processing.js
var Browser = {
isDomPresent: true,
navigator: navigator,
window: window,
document: document,
ajax: function(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
if (xhr.overrideMimeType) {
xhr.overrideMimeType("text/plain");
}
xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT");
xhr.send(null);
// failed request?
if (xhr.status !== 200 && xhr.status !== 0) { throw ("XMLHttpRequest failed, status code " + xhr.status); }
return xhr.responseText;
}
};
window.Processing = require('./src/')(Browser);
},{"./src/":2}],2:[function(require,module,exports){
// Base source files
var source = {
virtEquals: require("./Helpers/virtEquals"),
virtHashCode: require("./Helpers/virtHashCode"),
ObjectIterator: require("./Helpers/ObjectIterator"),
PConstants: require("./Helpers/PConstants"),
ArrayList: require("./Objects/ArrayList"),
HashMap: require("./Objects/HashMap"),
PVector: require("./Objects/PVector"),
PFont: require("./Objects/PFont"),
Char: require("./Objects/Char"),
XMLAttribute: require("./Objects/XMLAttribute"),
XMLElement: require("./Objects/XMLElement"),
PMatrix2D: require("./Objects/PMatrix2D"),
PMatrix3D: require("./Objects/PMatrix3D"),
PShape: require("./Objects/PShape"),
colors: require("./Objects/webcolors"),
PShapeSVG: require("./Objects/PShapeSVG"),
CommonFunctions: require("./P5Functions/commonFunctions"),
defaultScope: require("./Helpers/defaultScope"),
Processing: require("./Processing"),
setupParser: require("./Parser/Parser"),
finalize: require("./Helpers/finalizeProcessing")
};
// Additional code that gets tacked onto "p" during
// instantiation of a Processing sketch.
source.extend = {
withMath: require("./P5Functions/Math.js"),
withProxyFunctions: require("./P5Functions/JavaProxyFunctions")(source.virtHashCode, source.virtEquals),
withTouch: require("./P5Functions/touchmouse"),
withCommonFunctions: source.CommonFunctions.withCommonFunctions
};
/**
* Processing.js building function
*/
module.exports = function buildProcessingJS(Browser, testHarness) {
var noop = function(){},
virtEquals = source.virtEquals,
virtHashCode = source.virtHashCode,
PConstants = source.PConstants,
CommonFunctions = source.CommonFunctions,
ObjectIterator = source.ObjectIterator,
Char = source.Char,
XMLAttribute = source.XMLAttribute(),
ArrayList = source.ArrayList({
virtHashCode: virtHashCode,
virtEquals: virtEquals
}),
HashMap = source.HashMap({
virtHashCode: virtHashCode,
virtEquals: virtEquals
}),
PVector = source.PVector({
PConstants: PConstants
}),
PFont = source.PFont({
Browser: Browser,
noop: noop
}),
XMLElement = source.XMLElement({
Browser:Browser,
XMLAttribute: XMLAttribute
}),
PMatrix2D = source.PMatrix2D({
p:CommonFunctions
}),
PMatrix3D = source.PMatrix3D({
p:CommonFunctions
}),
PShape = source.PShape({
PConstants: PConstants,
PMatrix2D: PMatrix2D,
PMatrix3D: PMatrix3D
}),
PShapeSVG = source.PShapeSVG({
CommonFunctions:CommonFunctions,
PConstants:PConstants,
PShape:PShape,
XMLElement:XMLElement,
colors: source.colors
}),
defaultScope = source.defaultScope({
ArrayList: ArrayList,
HashMap: HashMap,
PVector: PVector,
PFont: PFont,
PShapeSVG: PShapeSVG,
ObjectIterator: ObjectIterator,
PConstants: PConstants,
Char: Char,
XMLElement: XMLElement,
XML: XMLElement
}),
Processing = source.Processing({
defaultScope:defaultScope,
Browser:Browser,
extend:source.extend,
noop:noop
});
// set up the Processing syntax parser
Processing = source.setupParser(Processing, {
aFunctions: testHarness,
defaultScope: defaultScope
});
// finalise the Processing object
Processing = source.finalize(Processing, {
isDomPresent: false || Browser.isDomPresent,
window: Browser.window,
document: Browser.document,
noop: noop
});
// done.
return Processing;
};
},{"./P5Functions/Math.js":3,"./Helpers/virtEquals":4,"./Helpers/virtHashCode":5,"./Objects/ArrayList":6,"./Objects/HashMap":7,"./Helpers/PConstants":8,"./Objects/PFont":9,"./Objects/Char":10,"./Objects/PVector":11,"./Objects/XMLElement":12,"./Objects/XMLAttribute":13,"./Objects/PMatrix2D":14,"./Objects/PMatrix3D":15,"./Objects/PShape":16,"./P5Functions/commonFunctions":17,"./Objects/webcolors":18,"./Objects/PShapeSVG":19,"./Helpers/defaultScope":20,"./Processing":21,"./Parser/Parser":22,"./Helpers/finalizeProcessing":23,"./P5Functions/JavaProxyFunctions":24,"./P5Functions/touchmouse":25,"./Helpers/ObjectIterator":26}],3:[function(require,module,exports){
/**
* For many "math" functions, we can delegate
* to the Math object. For others, we can't.
*/
module.exports = function withMath(p, undef) {
var currentRandom = Math.random;
/**
* Calculates the absolute value (magnitude) of a number. The absolute value of a number is always positive.
*
* @param {int|float} value int or float
*
* @returns {int|float}
*/
p.abs = Math.abs;
/**
* Calculates the closest int value that is greater than or equal to the value of the parameter.
* For example, ceil(9.03) returns the value 10.
*
* @param {float} value float
*
* @returns {int}
*
* @see floor
* @see round
*/
p.ceil = Math.ceil;
/**
* Returns Euler's number e (2.71828...) raised to the power of the value parameter.
*
* @param {int|float} value int or float: the exponent to raise e to
*
* @returns {float}
*/
p.exp = Math.exp;
/**
* Calculates the closest int value that is less than or equal to the value of the parameter.
*
* @param {int|float} value the value to floor
*
* @returns {int|float}
*
* @see ceil
* @see round
*/
p.floor = Math.floor;
/**
* Calculates the natural logarithm (the base-e logarithm) of a number. This function
* expects the values greater than 0.0.
*
* @param {int|float} value int or float: number must be greater then 0.0
*
* @returns {float}
*/
p.log = Math.log;
/**
* Facilitates exponential expressions. The pow() function is an efficient way of
* multiplying numbers by themselves (or their reciprocal) in large quantities.
* For example, pow(3, 5) is equivalent to the expression 3*3*3*3*3 and pow(3, -5)
* is equivalent to 1 / 3*3*3*3*3.
*
* @param {int|float} num base of the exponential expression
* @param {int|float} exponent power of which to raise the base
*
* @returns {float}
*
* @see sqrt
*/
p.pow = Math.pow;
/**
* Calculates the integer closest to the value parameter. For example, round(9.2) returns the value 9.
*
* @param {float} value number to round
*
* @returns {int}
*
* @see floor
* @see ceil
*/
p.round = Math.round;
/**
* Calculates the square root of a number. The square root of a number is always positive,
* even though there may be a valid negative root. The square root s of number a is such
* that s*s = a. It is the opposite of squaring.
*
* @param {float} value int or float, non negative
*
* @returns {float}
*
* @see pow
* @see sq
*/
p.sqrt = Math.sqrt;
// Trigonometry
/**
* The inverse of cos(), returns the arc cosine of a value. This function expects the
* values in the range of -1 to 1 and values are returned in the range 0 to PI (3.1415927).
*
* @param {float} value the value whose arc cosine is to be returned
*
* @returns {float}
*
* @see cos
* @see asin
* @see atan
*/
p.acos = Math.acos;
/**
* The inverse of sin(), returns the arc sine of a value. This function expects the values
* in the range of -1 to 1 and values are returned in the range -PI/2 to PI/2.
*
* @param {float} value the value whose arc sine is to be returned
*
* @returns {float}
*
* @see sin
* @see acos
* @see atan
*/
p.asin = Math.asin;
/**
* The inverse of tan(), returns the arc tangent of a value. This function expects the values
* in the range of -Infinity to Infinity (exclusive) and values are returned in the range -PI/2 to PI/2 .
*
* @param {float} value -Infinity to Infinity (exclusive)
*
* @returns {float}
*
* @see tan
* @see asin
* @see acos
*/
p.atan = Math.atan;
/**
* Calculates the angle (in radians) from a specified point to the coordinate origin as measured from
* the positive x-axis. Values are returned as a float in the range from PI to -PI. The atan2() function
* is most often used for orienting geometry to the position of the cursor. Note: The y-coordinate of the
* point is the first parameter and the x-coordinate is the second due the the structure of calculating the tangent.
*
* @param {float} y y-coordinate of the point
* @param {float} x x-coordinate of the point
*
* @returns {float}
*
* @see tan
*/
p.atan2 = Math.atan2;
/**
* Calculates the cosine of an angle. This function expects the values of the angle parameter to be provided
* in radians (values from 0 to PI*2). Values are returned in the range -1 to 1.
*
* @param {float} value an angle in radians
*
* @returns {float}
*
* @see tan
* @see sin
*/
p.cos = Math.cos;
/**
* Calculates the sine of an angle. This function expects the values of the angle parameter to be provided in
* radians (values from 0 to 6.28). Values are returned in the range -1 to 1.
*
* @param {float} value an angle in radians
*
* @returns {float}
*
* @see cos
* @see radians
*/
p.sin = Math.sin;
/**
* Calculates the ratio of the sine and cosine of an angle. This function expects the values of the angle
* parameter to be provided in radians (values from 0 to PI*2). Values are returned in the range infinity to -infinity.
*
* @param {float} value an angle in radians
*
* @returns {float}
*
* @see cos
* @see sin
* @see radians
*/
p.tan = Math.tan;
/**
* Constrains a value to not exceed a maximum and minimum value.
*
* @param {int|float} value the value to constrain
* @param {int|float} value minimum limit
* @param {int|float} value maximum limit
*
* @returns {int|float}
*
* @see max
* @see min
*/
p.constrain = function(aNumber, aMin, aMax) {
return aNumber > aMax ? aMax : aNumber < aMin ? aMin : aNumber;
};
/**
* Calculates the distance between two points.
*
* @param {int|float} x1 int or float: x-coordinate of the first point
* @param {int|float} y1 int or float: y-coordinate of the first point
* @param {int|float} z1 int or float: z-coordinate of the first point
* @param {int|float} x2 int or float: x-coordinate of the second point
* @param {int|float} y2 int or float: y-coordinate of the second point
* @param {int|float} z2 int or float: z-coordinate of the second point
*
* @returns {float}
*/
p.dist = function() {
var dx, dy, dz;
if (arguments.length === 4) {
dx = arguments[0] - arguments[2];
dy = arguments[1] - arguments[3];
return Math.sqrt(dx * dx + dy * dy);
}
if (arguments.length === 6) {
dx = arguments[0] - arguments[3];
dy = arguments[1] - arguments[4];
dz = arguments[2] - arguments[5];
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
};
/**
* Calculates a number between two numbers at a specific increment. The amt parameter is the
* amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very
* near the first point, 0.5 is half-way in between, etc. The lerp function is convenient for
* creating motion along a straight path and for drawing dotted lines.
*
* @param {int|float} value1 float or int: first value
* @param {int|float} value2 float or int: second value
* @param {int|float} amt float: between 0.0 and 1.0
*
* @returns {float}
*
* @see curvePoint
* @see bezierPoint
*/
p.lerp = function(value1, value2, amt) {
return ((value2 - value1) * amt) + value1;
};
/**
* Calculates the magnitude (or length) of a vector. A vector is a direction in space commonly
* used in computer graphics and linear algebra. Because it has no "start" position, the magnitude
* of a vector can be thought of as the distance from coordinate (0,0) to its (x,y) value.
* Therefore, mag() is a shortcut for writing "dist(0, 0, x, y)".
*
* @param {int|float} a float or int: first value
* @param {int|float} b float or int: second value
* @param {int|float} c float or int: third value
*
* @returns {float}
*
* @see dist
*/
p.mag = function(a, b, c) {
if (c) {
return Math.sqrt(a * a + b * b + c * c);
}
return Math.sqrt(a * a + b * b);
};
/**
* Re-maps a number from one range to another. In the example above, the number '25' is converted from
* a value in the range 0..100 into a value that ranges from the left edge (0) to the right edge (width) of the screen.
* Numbers outside the range are not clamped to 0 and 1, because out-of-range values are often intentional and useful.
*
* @param {float} value The incoming value to be converted
* @param {float} istart Lower bound of the value's current range
* @param {float} istop Upper bound of the value's current range
* @param {float} ostart Lower bound of the value's target range
* @param {float} ostop Upper bound of the value's target range
*
* @returns {float}
*
* @see norm
* @see lerp
*/
p.map = function(value, istart, istop, ostart, ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
};
/**
* Determines the largest value in a sequence of numbers.
*
* @param {int|float} value1 int or float
* @param {int|float} value2 int or float
* @param {int|float} value3 int or float
* @param {int|float} array int or float array
*
* @returns {int|float}
*
* @see min
*/
p.max = function() {
if (arguments.length === 2) {
return arguments[0] < arguments[1] ? arguments[1] : arguments[0];
}
var numbers = arguments.length === 1 ? arguments[0] : arguments; // if single argument, array is used
if (! ("length" in numbers && numbers.length > 0)) {
throw "Non-empty array is expected";
}
var max = numbers[0],
count = numbers.length;
for (var i = 1; i < count; ++i) {
if (max < numbers[i]) {
max = numbers[i];
}
}
return max;
};
/**
* Determines the smallest value in a sequence of numbers.
*
* @param {int|float} value1 int or float
* @param {int|float} value2 int or float
* @param {int|float} value3 int or float
* @param {int|float} array int or float array
*
* @returns {int|float}
*
* @see max
*/
p.min = function() {
if (arguments.length === 2) {
return arguments[0] < arguments[1] ? arguments[0] : arguments[1];
}
var numbers = arguments.length === 1 ? arguments[0] : arguments; // if single argument, array is used
if (! ("length" in numbers && numbers.length > 0)) {
throw "Non-empty array is expected";
}
var min = numbers[0],
count = numbers.length;
for (var i = 1; i < count; ++i) {
if (min > numbers[i]) {
min = numbers[i];
}
}
return min;
};
/**
* Normalizes a number from another range into a value between 0 and 1.
* Identical to map(value, low, high, 0, 1);
* Numbers outside the range are not clamped to 0 and 1, because out-of-range
* values are often intentional and useful.
*
* @param {float} aNumber The incoming value to be converted
* @param {float} low Lower bound of the value's current range
* @param {float} high Upper bound of the value's current range
*
* @returns {float}
*
* @see map
* @see lerp
*/
p.norm = function(aNumber, low, high) {
return (aNumber - low) / (high - low);
};
/**
* Squares a number (multiplies a number by itself). The result is always a positive number,
* as multiplying two negative numbers always yields a positive result. For example, -1 * -1 = 1.
*
* @param {float} value int or float
*
* @returns {float}
*
* @see sqrt
*/
p.sq = function(aNumber) {
return aNumber * aNumber;
};
/**
* Converts a radian measurement to its corresponding value in degrees. Radians and degrees are two ways of
* measuring the same thing. There are 360 degrees in a circle and 2*PI radians in a circle. For example,
* 90 degrees = PI/2 = 1.5707964. All trigonometric methods in Processing require their parameters to be specified in radians.
*
* @param {int|float} value an angle in radians
*
* @returns {float}
*
* @see radians
*/
p.degrees = function(aAngle) {
return (aAngle * 180) / Math.PI;
};
/**
* Generates random numbers. Each time the random() function is called, it returns an unexpected value within
* the specified range. If one parameter is passed to the function it will return a float between zero and the
* value of the high parameter. The function call random(5) returns values between 0 and 5 (starting at zero,
* up to but not including 5). If two parameters are passed, it will return a float with a value between the
* parameters. The function call random(-5, 10.2) returns values starting at -5 up to (but not including) 10.2.
* To convert a floating-point random number to an integer, use the int() function.
*
* @param {int|float} value1 if one parameter is used, the top end to random from, if two params the low end
* @param {int|float} value2 the top end of the random range
*
* @returns {float}
*
* @see randomSeed
* @see noise
*/
p.random = function() {
if(arguments.length === 0) {
return currentRandom();
}
if(arguments.length === 1) {
return currentRandom() * arguments[0];
}
var aMin = arguments[0], aMax = arguments[1];
return currentRandom() * (aMax - aMin) + aMin;
};
// Pseudo-random generator
function Marsaglia(i1, i2) {
// from http://www.math.uni-bielefeld.de/~sillke/ALGORITHMS/random/marsaglia-c
var z=i1 || 362436069, w= i2 || 521288629;
var nextInt = function() {
z=(36969*(z&65535)+(z>>>16)) & 0xFFFFFFFF;
w=(18000*(w&65535)+(w>>>16)) & 0xFFFFFFFF;
return (((z&0xFFFF)<<16) | (w&0xFFFF)) & 0xFFFFFFFF;
};
this.nextDouble = function() {
var i = nextInt() / 4294967296;
return i < 0 ? 1 + i : i;
};
this.nextInt = nextInt;
}
Marsaglia.createRandomized = function() {
var now = new Date();
return new Marsaglia((now / 60000) & 0xFFFFFFFF, now & 0xFFFFFFFF);
};
/**
* Sets the seed value for random(). By default, random() produces different results each time the
* program is run. Set the value parameter to a constant to return the same pseudo-random numbers
* each time the software is run.
*
* @param {int|float} seed int
*
* @see random
* @see noise
* @see noiseSeed
*/
p.randomSeed = function(seed) {
currentRandom = (new Marsaglia(seed)).nextDouble;
};
// Random
// We have two random()'s in the code... what does this do ? and which one is current ?
p.Random = function(seed) {
var haveNextNextGaussian = false, nextNextGaussian, random;
this.nextGaussian = function() {
if (haveNextNextGaussian) {
haveNextNextGaussian = false;
return nextNextGaussian;
}
var v1, v2, s;
do {
v1 = 2 * random() - 1; // between -1.0 and 1.0
v2 = 2 * random() - 1; // between -1.0 and 1.0
s = v1 * v1 + v2 * v2;
}
while (s >= 1 || s === 0);
var multiplier = Math.sqrt(-2 * Math.log(s) / s);
nextNextGaussian = v2 * multiplier;
haveNextNextGaussian = true;
return v1 * multiplier;
};
// by default use standard random, otherwise seeded
random = (seed === undef) ? Math.random : (new Marsaglia(seed)).nextDouble;
};
// Noise functions and helpers
function PerlinNoise(seed) {
var rnd = seed !== undef ? new Marsaglia(seed) : Marsaglia.createRandomized();
var i, j;
// http://www.noisemachine.com/talk1/17b.html
// http://mrl.nyu.edu/~perlin/noise/
// generate permutation
var perm = new Uint8Array(512);
for(i=0;i<256;++i) { perm[i] = i; }
for(i=0;i<256;++i) { var t = perm[j = rnd.nextInt() & 0xFF]; perm[j] = perm[i]; perm[i] = t; }
// copy to avoid taking mod in perm[0];
for(i=0;i<256;++i) { perm[i + 256] = perm[i]; }
function grad3d(i,x,y,z) {
var h = i & 15; // convert into 12 gradient directions
var u = h<8 ? x : y,
v = h<4 ? y : h===12||h===14 ? x : z;
return ((h&1) === 0 ? u : -u) + ((h&2) === 0 ? v : -v);
}
function grad2d(i,x,y) {
var v = (i & 1) === 0 ? x : y;
return (i&2) === 0 ? -v : v;
}
function grad1d(i,x) {
return (i&1) === 0 ? -x : x;
}
function lerp(t,a,b) { return a + t * (b - a); }
this.noise3d = function(x, y, z) {
var X = Math.floor(x)&255, Y = Math.floor(y)&255, Z = Math.floor(z)&255;
x -= Math.floor(x); y -= Math.floor(y); z -= Math.floor(z);
var fx = (3-2*x)*x*x, fy = (3-2*y)*y*y, fz = (3-2*z)*z*z;
var p0 = perm[X]+Y, p00 = perm[p0] + Z, p01 = perm[p0 + 1] + Z,
p1 = perm[X + 1] + Y, p10 = perm[p1] + Z, p11 = perm[p1 + 1] + Z;
return lerp(fz,
lerp(fy, lerp(fx, grad3d(perm[p00], x, y, z), grad3d(perm[p10], x-1, y, z)),
lerp(fx, grad3d(perm[p01], x, y-1, z), grad3d(perm[p11], x-1, y-1,z))),
lerp(fy, lerp(fx, grad3d(perm[p00 + 1], x, y, z-1), grad3d(perm[p10 + 1], x-1, y, z-1)),
lerp(fx, grad3d(perm[p01 + 1], x, y-1, z-1), grad3d(perm[p11 + 1], x-1, y-1,z-1))));
};
this.noise2d = function(x, y) {
var X = Math.floor(x)&255, Y = Math.floor(y)&255;
x -= Math.floor(x); y -= Math.floor(y);
var fx = (3-2*x)*x*x, fy = (3-2*y)*y*y;
var p0 = perm[X]+Y, p1 = perm[X + 1] + Y;
return lerp(fy,
lerp(fx, grad2d(perm[p0], x, y), grad2d(perm[p1], x-1, y)),
lerp(fx, grad2d(perm[p0 + 1], x, y-1), grad2d(perm[p1 + 1], x-1, y-1)));
};
this.noise1d = function(x) {
var X = Math.floor(x)&255;
x -= Math.floor(x);
var fx = (3-2*x)*x*x;
return lerp(fx, grad1d(perm[X], x), grad1d(perm[X+1], x-1));
};
}
// processing defaults
var noiseProfile = { generator: undef, octaves: 4, fallout: 0.5, seed: undef};
/**
* Returns the Perlin noise value at specified coordinates. Perlin noise is a random sequence
* generator producing a more natural ordered, harmonic succession of numbers compared to the
* standard random() function. It was invented by Ken Perlin in the 1980s and been used since
* in graphical applications to produce procedural textures, natural motion, shapes, terrains etc.
* The main difference to the random() function is that Perlin noise is defined in an infinite
* n-dimensional space where each pair of coordinates corresponds to a fixed semi-random value
* (fixed only for the lifespan of the program). The resulting value will always be between 0.0
* and 1.0. Processing can compute 1D, 2D and 3D noise, depending on the number of coordinates
* given. The noise value can be animated by moving through the noise space as demonstrated in
* the example above. The 2nd and 3rd dimension can also be interpreted as time.
* The actual noise is structured similar to an audio signal, in respect to the function's use
* of frequencies. Similar to the concept of harmonics in physics, perlin noise is computed over
* several octaves which are added together for the final result.
* Another way to adjust the character of the resulting sequence is the scale of the input
* coordinates. As the function works within an infinite space the value of the coordinates
* doesn't matter as such, only the distance between successive coordinates does (eg. when using
* noise() within a loop). As a general rule the smaller the difference between coordinates, the
* smoother the resulting noise sequence will be. Steps of 0.005-0.03 work best for most applications,
* but this will differ depending on use.
*
* @param {float} x x coordinate in noise space
* @param {float} y y coordinate in noise space
* @param {float} z z coordinate in noise space
*
* @returns {float}
*
* @see random
* @see noiseDetail
*/
p.noise = function(x, y, z) {
if(noiseProfile.generator === undef) {
// caching
noiseProfile.generator = new PerlinNoise(noiseProfile.seed);
}
var generator = noiseProfile.generator;
var effect = 1, k = 1, sum = 0;
for(var i=0; i<noiseProfile.octaves; ++i) {
effect *= noiseProfile.fallout;
switch (arguments.length) {
case 1:
sum += effect * (1 + generator.noise1d(k*x))/2; break;
case 2:
sum += effect * (1 + generator.noise2d(k*x, k*y))/2; break;
case 3:
sum += effect * (1 + generator.noise3d(k*x, k*y, k*z))/2; break;
}
k *= 2;
}
return sum;
};
/**
* Adjusts the character and level of detail produced by the Perlin noise function.
* Similar to harmonics in physics, noise is computed over several octaves. Lower octaves
* contribute more to the output signal and as such define the overal intensity of the noise,
* whereas higher octaves create finer grained details in the noise sequence. By default,
* noise is computed over 4 octaves with each octave contributing exactly half than its
* predecessor, starting at 50% strength for the 1st octave. This falloff amount can be
* changed by adding an additional function parameter. Eg. a falloff factor of 0.75 means
* each octave will now have 75% impact (25% less) of the previous lower octave. Any value
* between 0.0 and 1.0 is valid, however note that values greater than 0.5 might result in
* greater than 1.0 values returned by noise(). By changing these parameters, the signal
* created by the noise() function can be adapted to fit very specific needs and characteristics.
*
* @param {int} octaves number of octaves to be used by the noise() function
* @param {float} falloff falloff factor for each octave
*
* @see noise
*/
p.noiseDetail = function(octaves, fallout) {
noiseProfile.octaves = octaves;
if(fallout !== undef) {
noiseProfile.fallout = fallout;
}
};
/**
* Sets the seed value for noise(). By default, noise() produces different results each
* time the program is run. Set the value parameter to a constant to return the same
* pseudo-random numbers each time the software is run.
*
* @param {int} seed int
*
* @returns {float}
*
* @see random
* @see radomSeed
* @see noise
* @see noiseDetail
*/
p.noiseSeed = function(seed) {
noiseProfile.seed = seed;
noiseProfile.generator = undef;
};
};
},{}],5:[function(require,module,exports){
/**
* Returns Java hashCode() result for the object. If the object has the "hashCode" function,
* it preforms the call of this function. Otherwise it uses/creates the "$id" property,
* which is used as the hashCode.
*
* @param {Object} obj The object.
* @returns {int} The object's hash code.
*/
module.exports = function virtHashCode(obj, undef) {
if (typeof(obj) === "string") {
var hash = 0;
for (var i = 0; i < obj.length; ++i) {
hash = (hash * 31 + obj.charCodeAt(i)) & 0xFFFFFFFF;
}
return hash;
}
if (typeof(obj) !== "object") {
return obj & 0xFFFFFFFF;
}
if (obj.hashCode instanceof Function) {
return obj.hashCode();
}
if (obj.$id === undef) {
obj.$id = ((Math.floor(Math.random() * 0x10000) - 0x8000) << 16) | Math.floor(Math.random() * 0x10000);
}
return obj.$id;
};
},{}],6:[function(require,module,exports){
/**
* An ArrayList stores a variable number of objects.
*
* @param {int} initialCapacity optional defines the initial capacity of the list, it's empty by default
*
* @returns {ArrayList} new ArrayList object
*/
module.exports = function(options) {
var virtHashCode = options.virtHashCode,
virtEquals = options.virtEquals;
function Iterator(array) {
var index = -1;
this.hasNext = function() {
return (index + 1) < array.length;
};
this.next = function() {
return array[++index];
};
this.remove = function() {
array.splice(index--, 1);
};
}
function ArrayList(a) {
var array = [];
if (a && a.toArray) {
array = a.toArray();
}
/**
* @member ArrayList
* ArrayList.get() Returns the element at the specified position in this list.
*
* @param {int} i index of element to return
*
* @returns {Object} the element at the specified position in this list.
*/
this.get = function(i) {
return array[i];
};
/**
* @member ArrayList
* ArrayList.contains() Returns true if this list contains the specified element.
*
* @param {Object} item element whose presence in this List is to be tested.
*
* @returns {boolean} true if the specified element is present; false otherwise.
*/
this.contains = function(item) {
return this.indexOf(item)>-1;
};
/**
* @member ArrayList
* ArrayList.indexOf() Returns the position this element takes in the list, or -1 if the element is not found.
*
* @param {Object} item element whose position in this List is to be tested.
*
* @returns {int} the list position that the first match for this element holds in the list, or -1 if it is not in the list.
*/
this.indexOf = function(item) {
for (var i = 0, len = array.length; i < len; ++i) {
if (virtEquals(item, array[i])) {
return i;
}
}
return -1;
};
/**
* @member ArrayList
* ArrayList.lastIndexOf() Returns the index of the last occurrence of the specified element in this list,
* or -1 if this list does not contain the element. More formally, returns the highest index i such that
* (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
*
* @param {Object} item element to search for.
*
* @returns {int} the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
*/
this.lastIndexOf = function(item) {
for (var i = array.length-1; i >= 0; --i) {
if (virtEquals(item, array[i])) {
return i;
}
}
return -1;
};
/**
* @member ArrayList
* ArrayList.add() Adds the specified element to this list.
*
* @param {int} index optional index at which the specified element is to be inserted
* @param {Object} object element to be added to the list
*/
this.add = function() {
if (arguments.length === 1) {
array.push(arguments[0]); // for add(Object)
} else if (arguments.length === 2) {
var arg0 = arguments[0];
if (typeof arg0 === 'number') {
if (arg0 >= 0 && arg0 <= array.length) {
array.splice(arg0, 0, arguments[1]); // for add(i, Object)
} else {
throw(arg0 + " is not a valid index");
}
} else {
throw(typeof arg0 + " is not a number");
}
} else {
throw("Please use the proper number of parameters.");
}
};
/**
* @member ArrayList
* ArrayList.addAll(collection) appends all of the elements in the specified
* Collection to the end of this list, in the order that they are returned by
* the specified Collection's Iterator.
*
* When called as addAll(index, collection) the elements are inserted into
* this list at the position indicated by index.
*
* @param {index} Optional; specifies the position the colletion should be inserted at
* @param {collection} Any iterable object (ArrayList, HashMap.keySet(), etc.)
* @throws out of bounds error for negative index, or index greater than list size.
*/
this.addAll = function(arg1, arg2) {
// addAll(int, Collection)
var it;
if (typeof arg1 === "number") {
if (arg1 < 0 || arg1 > array.length) {
throw("Index out of bounds for addAll: " + arg1 + " greater or equal than " + array.length);
}
it = new ObjectIterator(arg2);
while (it.hasNext()) {
array.splice(arg1++, 0, it.next());
}
}
// addAll(Collection)
else {
it = new ObjectIterator(arg1);
while (it.hasNext()) {
array.push(it.next());
}
}
};
/**