-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractales.js
1200 lines (1150 loc) · 33.9 KB
/
fractales.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
"use strict";
var canvas = document.getElementById("canvas");
var cwidth = canvas.width = canvas.offsetWidth;
var cheight = canvas.height = canvas.offsetHeight;
var xmin = -2;
var xmax = 1;
var ymin = -1.2;
var ymax = 1.2;
var nmax = 1000;
var l = 4;
var rad = 2;
var frct = 11;
var cr = 0.28;
var ci = 0.01;
var t0,t1;
var coul=[];
function calCoul() {
coul[0]=0;
for (let i=1; i<nmax; i++){
coul[i]=Math.floor(128*Math.log10(i));
}
}
calCoul();
function coord(x, w, c) { // x[x, y], w[xmin, xmax, ymin, ymax], c[cwidth, cheight]
let xs = (w[1]-w[0])/c[0];
let ys = (w[3]-w[2])/c[1];
return [w[0]+x[0]*xs, w[2]+x[1]*ys];
}
function initGPU() {
try {
return new window.GPU.GPU();
} catch (e) {
return new GPU();
}
}
const gpu = initGPU();
gpu.addFunction(coord);
gpu.addFunction(mod_cc);
gpu.addFunction(mod2_cc);
gpu.addFunction(add_cc);
gpu.addFunction(sub_cc);
gpu.addFunction(mul_cc);
gpu.addFunction(div_cc);
function mandelbrot(){
let context,img,r,v,b,a=255;
context = canvas.getContext("2d");
let xs = (xmax-xmin)/cwidth;
let ys = (ymax-ymin)/cheight;
img = context.createImageData(cwidth,cheight);
for (let y=0,j=0;y<cheight;y++){
let cy = ymax - y*ys;
for (let x=0;x<cwidth;x++){
let cx = xmin + x*xs;
let x1 = cx;
let y1 = cy;
let xx = x1**2;
let yy = y1**2;
let i = 0;
let p = Math.sqrt((cx-.25)**2+cy**2); // speed optimisation
if (cx < p-2*p*p+.25) i = nmax; // skip the cardioid
if ((cx+1)**2+cy**2 < .0625) i = nmax; // skip first bulb
for(;i<nmax && xx+yy<=l;i++){
xx = x1**2;
yy = y1**2;
y1 = 2*x1*y1+cy;
x1 = xx-yy+cx;
}
if (i === nmax){
r=v=b=0;
} else {
r=coul[i];
v=i*2;
b=i;
}
img.data[j++] = r;
img.data[j++] = v;
img.data[j++] = b;
img.data[j++] = a;
}
}
context.putImageData(img, 0, 0);
}
var mandelbrotGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = x[1];
let x1 = cx;
let y1 = cy;
let xx = x1**2;
let yy = y1**2;
let i = 0;
for(;i<nmax && xx+yy<=4;i++){
xx = x1**2;
yy = y1**2;
y1 = 2*x1*y1+cy;
x1 = xx-yy+cx;
}
if (i !== nmax){
this.color(Math.log10(i)/2, i*2/256, i/256);
} else {
this.color(0, 0, 0);
}
})
// .setPrecision('single')
// .setTactic('precision')
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
function mandelsinh(){
let context,img,r,v,b,a=255;
context = canvas.getContext("2d");
let xs = (xmax-xmin)/cwidth;
let ys = (ymax-ymin)/cheight;
img = context.createImageData(cwidth,cheight);
for (let y=0,j=0;y<cheight;y++){
let cy = ymax - y*ys;
for (let x=0;x<cwidth;x++){
let cx = xmin + x*xs;
let x1 = 1;
let y1 = .1;
let xx = x1**2;
let yy = y1**2;
let i = 0;
let ca = cx**2/(cx**2+cy**2)**2-cy**2/(cx**2+cy**2)**2;
let cb = -2*cx*cy/(cx**2+cy**2)**2;
for(;i<nmax && xx+yy<=40000;i++){
xx = x1**2;
yy = y1**2;
let yt = Math.cosh(x1)*Math.sin(y1)+ca;
x1 = Math.sinh(x1)*Math.cos(y1)+cb;
y1 = yt;
}
if (i === nmax){
r=v=b=0;
} else {
r=128-256*Math.sin(i);
v=i*26;
b=i*26;
}
img.data[j++] = r;
img.data[j++] = v;
img.data[j++] = b;
img.data[j++] = a;
}
}
context.putImageData(img, 0, 0);
}
var mandelsinhGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = x[1];
let x1 = 1;
let y1 = 0.1;
let xx = x1**2;
let yy = y1**2;
let i = 0;
let ca = cx**2/(cx**2+cy**2)**2-cy**2/(cx**2+cy**2)**2;
let cb = -2*cx*cy/(cx**2+cy**2)**2;
for(;i<nmax && xx+yy<=40000;i++){
xx = x1**2;
yy = y1**2;
let yt = Math.cosh(x1)*Math.sin(y1)+ca;
x1 = Math.sinh(x1)*Math.cos(y1)+cb;
y1 = yt;
}
if (i !== nmax){
this.color(0.5-Math.sin(i), i/10, i/10);
//this.color(i/10, i/10, i/10);
} else {
this.color(0, 0, 0);
}
})
// .setPrecision('single')
// .setTactic('precision')
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
var mandelGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax, rad, cr, ci,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = x[1];
let x1 = cr;
let y1 = ci;
let xx = x1**2;
let yy = y1**2;
let rad2 = rad**2;
let i = 0;
for(;i<nmax && xx+yy<=rad2;i++){
xx = x1**2;
yy = y1**2;
y1 = 2*x1*y1+cy;
x1 = xx-yy+cx;
}
if (i !== nmax){
this.color(Math.log10(i)/2, i*2/256, i/256);
} else {
this.color(0, 0, 0);
}
})
// .setPrecision('single')
// .setTactic('precision')
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
function burnShip(){
let context,img,r,v,b,a=255;
context = canvas.getContext("2d");
let xs = (xmax-xmin)/cwidth;
let ys = (ymax-ymin)/cheight;
img = context.createImageData(cwidth,cheight);
for (let y=0,j=0;y<cheight;y++){
let cy = y*ys - ymax; // invert y axis to see the ship
for (let x=0;x<cwidth;x++){
let cx = xmin + x*xs;
let x1 = cx;
let y1 = cy;
let i=0;
for(;i<nmax;i++){
let xx = x1**2;
let yy = y1**2;
if (xx+yy>l){break;}
y1 = Math.abs(2*x1*y1)+cy;
x1 = xx-yy+cx;
}
if (i === nmax){
r=v=b=0;
} else {
r=coul[i];
v=i*2;
b=i;
}
img.data[j++] = r;
img.data[j++] = v;
img.data[j++] = b;
img.data[j++] = a;
}
}
context.putImageData(img, 0, 0);
}
var burningShipGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = -x[1];
let x1 = cx;
let y1 = cy;
let out = false;
let i = 0;
for(;i<nmax;i++){
let xx = x1**2;
let yy = y1**2;
if (xx+yy>4){out=true;break;}
y1 = Math.abs(2*x1*y1)+cy;
x1 = xx-yy+cx;
}
if (out){
this.color(Math.log10(i)/2, i*2/256, i/256);
} else {
this.color(0, 0, 0);
}
})
// .setPrecision('single')
// .setTactic('precision')
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
function julia(n){
let context,img,r,v,b,a=255;
context = canvas.getContext("2d");
img = context.createImageData(cwidth,cheight);
let xs = (xmax-xmin)/cwidth;
let ys = (ymax-ymin)/cheight;
for (let y=0,j=0;y<cheight;y++){
let cy = ymax - y*ys;
for (let x=0;x<cwidth;x++){
let cr,ci;
if (n === 1) {
cr = 0.285;
ci = 0.01;
}else if (n === 2) {
cr = 0.28;
ci = -0.02;
}else{
cr = -0.15;
ci = 1.03;
}
let cx = xmin + x*xs;
let x1 = cx;
let y1 = cy;
let i=0
for(;i<nmax;i++){
let xx = x1**2;
let yy = y1**2;
if (xx+yy>l){break;}
y1 = 2*x1*y1+ci;
x1 = xx-yy+cr;
}
if (i === nmax){
r=0;
v=0;
b=0;
} else {
r=Math.floor(128*Math.log(i)/Math.log(10));
v=i*2;
b=i;
}
img.data[j++] = r;
img.data[j++] = v;
img.data[j++] = b;
img.data[j++] = a;
}
}
context.putImageData(img, 0, 0);
}
var juliaGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight,cr,ci) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = x[1];
let x1 = cx;
let y1 = cy;
let out = false;
let i = 0;
for(;i<nmax;i++){
let xx = x1**2;
let yy = y1**2;
if (xx+yy>4){out=true;break;}
y1 = 2*x1*y1+ci;
x1 = xx-yy+cr;
}
if (out){
this.color(Math.log10(i)/2, i*2/256, i/256);
} else {
this.color(0, 0, 0);
}
})
// .setPrecision('single')
// .setTactic('precision')
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
function collatz() {
let context,img,r,v,b,a=255;
context = canvas.getContext("2d");
let xs = (xmax-xmin)/cwidth;
let ys = (ymax-ymin)/cheight;
img = context.createImageData(cwidth,cheight);
for (let y=0,j=0;y<cheight;y++){
let cy = ymax - y*ys;
for (let x=0;x<cwidth;x++){
let cx = xmin + x*xs;
let x1 = cx;
let y1 = cy;
let i=0
let norm = 0;
for(;i<nmax && norm<10000;i++){
let cosr = Math.cos(Math.PI*x1)*Math.cosh(Math.PI*y1);
let cosi = Math.sin(Math.PI*x1)*Math.sinh(Math.PI*y1);
let re = (2-(2+5*x1)*cosr+7*x1-5*y1*cosi)/4;
let im = ((2+5*x1)*cosi+7*y1-5*y1*cosr)/4;
// let re = (1-(1+2*x1)*cosr+4*x1-2*y1*cosi)/4;
// let im = ((1+2*x1)*cosi+4*y1-2*y1*cosr)/4;
x1 = re;
y1 = im;
norm = re*re+im*im;
// if (){break;}
}
if (i === nmax){
r=v=b=0;
} else {
b=i*60;
v=(i-4)*80;
r=(i-4)*10;
}
img.data[j++] = r;
img.data[j++] = v;
img.data[j++] = b;
img.data[j++] = a;
}
}
context.putImageData(img, 0, 0);
}
var collGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = x[1];
let x1 = cx;
let y1 = cy;
let i = 0;
let norm=0;
for(;i<nmax && norm<10000;i++){
let cosr = Math.cos(Math.PI*x1)*Math.cosh(Math.PI*y1);
let cosi = Math.sin(Math.PI*x1)*Math.sinh(Math.PI*y1);
let re = (2-(2+5*x1)*cosr+7*x1-5*y1*cosi)/4;
let im = ((2+5*x1)*cosi+7*y1-5*y1*cosr)/4;
x1 = re;
y1 = im;
norm = re*re+im*im;
}
if (i === nmax){
this.color(0, 0, 0);
} else {
let b=.234*i;
let v=.313*(i-4);
let r=.039*(i-4);
this.color(r, v, b);
}
})
// .setPrecision('single')
// .setTactic('precision')
// .setFixIntegerDivisionAccuracy(true)
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
var coll2GPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let cx = x[0];
let cy = x[1];
let x1 = cx;
let y1 = cy;
let i = 0;
let norm=0;
for(;i<nmax && norm<10000;i++){
let cosr = Math.cos(Math.PI*x1)*Math.cosh(Math.PI*y1);
let cosi = Math.sin(Math.PI*x1)*Math.sinh(Math.PI*y1);
let re = (1-(1+2*x1)*cosr+4*x1-2*y1*cosi)/4;
let im = ((1+2*x1)*cosi+4*y1-2*y1*cosr)/4;
x1 = re;
y1 = im;
norm = re*re+im*im;
}
if (i === nmax){
this.color(0, 0, 0);
} else {
let b=.234*i;
let v=.313*(i-4);
let r=.039*(i-4);
this.color(r, v, b);
}
})
// .setPrecision('single')
// .setTactic('precision')
// .setFixIntegerDivisionAccuracy(true)
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
function add_c(a, b) {
return {r: a.r+b.r, i: a.i+b.i};
}
function sub_c(a, b) {
return {r: a.r-b.r, i: a.i-b.i};
}
function mul_c(a, b) {
return {r:a.r*b.r-a.i*b.i, i:a.r*b.i+a.i*b.r};
}
function div_c(a, b) {
let mod2 = mod2_c(b);
if (mod2 === 0) // division par zéro
return {r:0, i:0};
return {r:(a.r*b.r+a.i*b.i)/mod2, i:(a.i*b.r-a.r*b.i)/mod2};
}
function mod_c(a){
return Math.sqrt(a.r*a.r+a.i*a.i);
}
function mod2_c(a){
return a.r*a.r+a.i*a.i;
}
function newton () {
let context,img,r,v,b,a=255;
context = canvas.getContext("2d");
let xs = (xmax-xmin)/cwidth;
let ys = (ymax-ymin)/cheight;
let p1 = {r: 1, i: 0}, // les 3 racines
p2 = {r: -.5, i: Math.sqrt(3)/2},
p3 = {r:-.5, i: -Math.sqrt(3)/2};
let threshold = .001;
img = context.createImageData(cwidth,cheight);
for (let y=0,j=0;y<cheight;y++){
let cy = ymax - y*ys;
for (let x=0;x<cwidth;x++){
let cx = xmin + x*xs;
let z = {r:cx, i:cy};
let d1 = mod_c(p1);
let d2 = mod_c(p2);
let d3 = mod_c(p3);
let dmin = Math.min(Math.min(d1,d2),d3);
// nmax = 100;
let i=0;
for(;i<nmax && dmin>threshold;i++){
let num = mul_c(mul_c(sub_c(z, p1),sub_c(z, p2)), sub_c(z, p3));
let den = add_c(add_c(mul_c(sub_c(z, p2), sub_c(z, p3)), mul_c(sub_c(z, p1), sub_c(z, p3))), mul_c(sub_c(z, p1), sub_c(z, p2)));
z = sub_c(z, div_c(num, den));
d1 = mod_c(sub_c(z, p1));
d2 = mod_c(sub_c(z, p2));
d3 = mod_c(sub_c(z, p3));
dmin = Math.min(Math.min(d1,d2),d3);
}
let coul = 153 + 102*Math.cos(.25* (i - Math.log2(Math.log(dmin) / Math.log(threshold))));
// let coul = 153 + 102*Math.cos(.25*i);
// let coul = 153 + 10*i;
if (i === nmax){
r=v=b=0;
} else if (d1<d2 && d1<d3) { // on converge vers d1
r=coul;
v=0;
b=.3*coul;
} else if (d2<d1 && d2<d3) { // on converge vers d2
r=0;
v=coul;
b=.3*coul;
} else { // on converge vers d3
r=0;
v=.3*coul;
b=coul;
}
img.data[j++] = r;
img.data[j++] = v;
img.data[j++] = b;
img.data[j++] = a;
}
}
context.putImageData(img, 0, 0);
}
function mod_cc(a){
return Math.sqrt(a[0]*a[0]+a[1]*a[1]);
}
function mod2_cc(a){
return a[0]*a[0]+a[1]*a[1];
}
function add_cc(a, b) {
return [a[0]+b[0], a[1]+b[1]];
}
function sub_cc(a, b) {
return [a[0]-b[0], a[1]-b[1]];
}
function mul_cc(a, b) {
return [a[0]*b[0]-a[1]*b[1], a[0]*b[1]+a[1]*b[0]];
}
function div_cc(a, b) {
let mod2 = mod2_cc(b);
if (mod2 === 0) // division par zéro
return [0, 0];
return [(a[0]*b[0]+a[1]*b[1])/mod2, (a[1]*b[0]-a[0]*b[1])/mod2];
}
var newtonGPU = gpu.createKernel(function(xmin,xmax,ymin,ymax,nmax,cwidth,cheight) {
let x = [0,0];
x = coord([this.thread.x, this.thread.y], [xmin, xmax, ymin, ymax], [cwidth, cheight]);
let z = [x[0], x[1]];
let p1 = [1, 0], // les 3 racines
p2 = [-.5, Math.sqrt(3)/2],
p3 = [-.5, -Math.sqrt(3)/2];
let threshold = .001;
let d1 = mod_cc(p1);
let d2 = mod_cc(p2);
let d3 = mod_cc(p3);
let dmin = Math.min(Math.min(d1,d2),d3)
let i = 0;
for(;i<nmax && dmin>threshold;i++){
let num = mul_cc(mul_cc(sub_cc(z, p1),sub_cc(z, p2)), sub_cc(z, p3));
let den = add_cc(add_cc(mul_cc(sub_cc(z, p2), sub_cc(z, p3)), mul_cc(sub_cc(z, p1), sub_cc(z, p3))), mul_cc(sub_cc(z, p1), sub_cc(z, p2)));
z = sub_cc(z, div_cc(num, den));
d1 = mod_cc(sub_cc(z, p1));
d2 = mod_cc(sub_cc(z, p2));
d3 = mod_cc(sub_cc(z, p3));
dmin = Math.min(Math.min(d1,d2),d3);
}
let vc = .24*(Math.log2(Math.log(dmin) / Math.log(threshold)));
let coul = .6 + .4*Math.cos(.25*i-vc);
if (i===nmax){
this.color(0, 1, 0);
} else {
let r=0, v=0, b=0;
if (d1<d2 && d1<d3) { // on converge vers d1
r=coul;
b=.3*coul;
} else if (d2<d1 && d2<d3) { // on converge vers d2
v=coul;
b=.3*coul;
} else { // on converge vers d3
v=.3*coul;
b=coul;
}
this.color(r, v, b);
}
})
// .setPrecision('single')
// .setTactic('precision')
.setLoopMaxIterations(10000)
.setOutput([cwidth, cheight])
.setGraphical(true);
var cosa = Math.cos(Math.PI/3);
var sina = Math.sin(Math.PI/3);
function koch(x1,y1,x2,y2,i,ctx){
if ((i>=nmax)||(i>=9)){
let xs = cwidth/(xmax-xmin);
let ys = cheight/(ymax-ymin);
let xa = (x1-xmin)*xs;
let ya = (-y1+ymax)*ys;
let xb = (x2-xmin)*xs;
let yb = (-y2+ymax)*ys;
ctx.lineJoin = "bevel";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(xa,ya);
ctx.lineTo(xb,yb);
ctx.stroke();
}else{
let xa = x1+(x2-x1)/3;
let ya = y1+(y2-y1)/3;
let xc = x1+2*(x2-x1)/3;
let yc = y1+2*(y2-y1)/3;
let xb = cosa*(xc-xa) - sina*(yc-ya) + xa;
let yb = sina*(xc-xa) + cosa*(yc-ya) + ya;
i++;
koch(x1,y1,xa,ya,i,ctx);
koch(xa,ya,xb,yb,i,ctx);
koch(xb,yb,xc,yc,i,ctx);
koch(xc,yc,x2,y2,i,ctx);
}
}
function vankoch(){
context = canvas.getContext("2d");
if (nmax>9) {nmax = 9;document.getElementById("formiter").value=nmax;}
context.strokeStyle = "#fff";
koch(-1,1,1,1,0,context);
koch(1,1,0,-0.732,0,context);
koch(0,-0.732,-1,1,0,context);
}
function koch2(x1,y1,x2,y2,i,ctx){
if ((i>=nmax)||(i>=5)){
let xs = cwidth/(xmax-xmin);
let ys = cheight/(ymax-ymin);
let xa = (x1-xmin)*xs;
let ya = (-y1+ymax)*ys;
let xb = (x2-xmin)*xs;
let yb = (-y2+ymax)*ys;
ctx.lineJoin = "bevel";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(xa,ya);
ctx.lineTo(xb,yb);
ctx.stroke();
}else{
let xs = x2-x1;
let ys = y2-y1;
let xa = x1+xs/4;
let ya = y1+ys/4;
let xd = x1+2*xs/4;
let yd = y1+2*ys/4;
let xg = x1+3*xs/4;
let yg = y1+3*ys/4;
let xb = xa-(yd-ya);
let yb = ya+(xd-xa);
let xc = xd-(yg-yd);
let yc = yd+(xg-xd);
let xe = xd-(xc-xd);
let ye = yd-(yc-yd);
let xf = xg-(yd-yg);
let yf = yg+(xd-xg);
i++;
koch2(x1,y1,xa,ya,i,ctx);
koch2(xa,ya,xb,yb,i,ctx);
koch2(xb,yb,xc,yc,i,ctx);
koch2(xc,yc,xd,yd,i,ctx);
koch2(xd,yd,xe,ye,i,ctx);
koch2(xe,ye,xf,yf,i,ctx);
koch2(xf,yf,xg,yg,i,ctx);
koch2(xg,yg,x2,y2,i,ctx);
}
}
function vankoch2(){
context = canvas.getContext("2d");
if (nmax>5) {nmax = 5;document.getElementById("formiter").value=nmax;}
context.strokeStyle = "#fff";
koch2(-1,1,1,1,0,context);
koch2(1,1,1,-1,0,context);
koch2(1,-1,-1,-1,0,context);
koch2(-1,-1,-1,1,0,context);
}
function sierp(x1,y1,x2,y2,x3,y3,i,ctx){
if ((i>=nmax)||(i>=12)){
let xs = cwidth/(xmax-xmin);
let ys = cheight/(ymax-ymin);
let xa = (x1-xmin)*xs;
let ya = (-y1+ymax)*ys;
let xb = (x2-xmin)*xs;
let yb = (-y2+ymax)*ys;
let xc = (x3-xmin)*xs;
let yc = (-y3+ymax)*ys;
ctx.lineJoin = "bevel";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(xa,ya);
ctx.lineTo(xb,yb);
ctx.lineTo(xc,yc);
ctx.closePath();
ctx.fill();
}else{
let a = x1+(x3-x1)/4;
let b = x1+2*(x3-x1)/4;
let c = x1+3*(x3-x1)/4;
let d = y1+(y2-y1)/2;
i++;
sierp(x1,y1,a,d,b,y1,i,ctx);
sierp(a,d,x2,y2,c,d,i,ctx);
sierp(b,y1,c,d,x3,y3,i,ctx);
}
}
function sierpinski(){
context = canvas.getContext("2d");
if (nmax>12) {nmax = 12;document.getElementById("formiter").value=nmax;}
context.fillStyle = "#eee";
sierp(-1,-0.732,0,1,1,-0.732,0,context);
}
function dragon(x1,y1,x2,y2,i,ctx){
if ((i>=nmax)||(i>=18)){
let xs = cwidth/(xmax-xmin);
let ys = cheight/(ymax-ymin);
let xa = (x1-xmin)*xs;
let ya = (-y1+ymax)*ys;
let xb = (x2-xmin)*xs;
let yb = (-y2+ymax)*ys;
ctx.lineJoin = "bevel";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(xa,ya);
ctx.lineTo(xb,yb);
ctx.stroke();
}else{
let xs = x2-x1;
let ys = y2-y1;
let x = x1+xs/2;
let y = y1+ys/2;
i++;
x = (x2-x1)/2 + (y2-y1)/2 + x1;
y = (y2-y1)/2 - (x2-x1)/2 + y1;
dragon(x1,y1,x,y,i,ctx);
dragon(x2,y2,x,y,i,ctx);
}
}
function heightway(){
context = canvas.getContext("2d");
if (nmax>18) {nmax = 18;document.getElementById("formiter").value=nmax;}
context.strokeStyle = "red";
dragon(-1,0,1,0,0,context);
context.strokeStyle = "green";
dragon(1,0,-1,0,0,context);
context.strokeStyle = "blue";
dragon(1,0,3,0,0,context);
context.strokeStyle = "purple";
dragon(3,0,1,0,0,context);
}
function rotate(M, O, angle, h=.67) {
let xM, yM, x, y;
angle *= Math.PI / 180;
xM = M.x - O.x;
yM = M.y - O.y;
xM *= h;yM *= h;
x = xM * Math.cos (angle) + yM * Math.sin (angle) + O.x;
y = - xM * Math.sin (angle) + yM * Math.cos (angle) + O.y;
return ({x:Math.round (x), y:Math.round (y)});
}
function tree() {
let ctx = canvas.getContext("2d");
ctx.strokeStyle = "brown";
ctx.lineJoin = "bevel";
ctx.lineWidth = 2;
ctx.beginPath();
let x1 = 0, y1 = -1, x2 = 0, y2 = -.3;
let xs = cwidth/(xmax-xmin);
let ys = cheight/(ymax-ymin);
let xa = (x1-xmin)*xs;
let ya = (-y1+ymax)*ys;
let xb = (x2-xmin)*xs;
let yb = (-y2+ymax)*ys;
branch(xa, ya, xb, yb, 0, ctx);
}
function branch(x0, y0, x1, y1, i, ctx) {
if ((i++>nmax) || (i>20)) return;
if (i>5) ctx.strokeStyle = "Green";
else ctx.strokeStyle = "SaddleBrown";
ctx.beginPath();
ctx.moveTo(x0,y0);
ctx.lineTo(x1,y1);
ctx.lineWidth = 9-i;
ctx.stroke();
let b1 = rotate({x:x0, y:y0}, {x:x1, y:y1}, -150)
let b2 = rotate({x:x0, y:y0}, {x:x1, y:y1}, 130)
branch(x1, y1, b1.x, b1.y, i, ctx);
branch(x1, y1, b2.x, b2.y, i, ctx);
}
function fern() {
let ctx = canvas.getContext("2d");
ctx.strokeStyle = "brown";
ctx.lineJoin = "bevel";
ctx.lineWidth = 2;
ctx.beginPath();
let x1 = 0, y1 = -1, x2 = 0, y2 = -.3;
let xs = cwidth/(xmax-xmin);
let ys = cheight/(ymax-ymin);
let xa = (x1-xmin)*xs;
let ya = (-y1+ymax)*ys;
let xb = (x2-xmin)*xs;
let yb = (-y2+ymax)*ys;
leaf(xa, ya, xb, yb, 0, ctx);
}
function leaf(x0, y0, x1, y1, i, ctx) {
if ((i++>nmax) || (i>11)) return;
if (i>1) ctx.strokeStyle = "Green";
else ctx.strokeStyle = "SaddleBrown";
ctx.beginPath();
ctx.moveTo(x0,y0);
ctx.lineTo(x1,y1);
ctx.lineWidth = 9-i;
ctx.stroke();
let b1 = rotate({x:x0, y:y0}, {x:x1, y:y1}, -118,.35)
let b2 = rotate({x:x0, y:y0}, {x:x1, y:y1}, 108,.25)
let b3 = rotate({x:x0, y:y0}, {x:x1, y:y1}, 175,.85)
leaf(x1, y1, b1.x, b1.y, i, ctx);
leaf(x1, y1, b2.x, b2.y, i, ctx);
leaf(x1, y1, b3.x, b3.y, i, ctx);
}
function draw(){
cwidth = canvas.width = canvas.offsetWidth;
cheight = canvas.height = canvas.offsetHeight;
t0 = performance.now();
fractale();
t1 = performance.now();
let d = (t1-t0)/1000; // durée d'execution en secondes
document.getElementById("disp").innerHTML = "temps de calcul :<br>"+d.toFixed(5)+" sec";
}
function selectFract(f) {
frct = parseInt(f);
let iter = document.getElementById("formiter");
switch(frct){
case 1: // mandelbrot
default:
case 2: // julia 1
case 3: // julia 2
case 4: // julia 3
case 13: // burning ship
case 16: // newton
iter.min = 100;
iter.max = 10000;
iter.step = 100;
iter.value = 1000;
break;
case 18: // mandelsinh
iter.min = 10;
iter.max = 1000;
iter.step = 10;
iter.value = 100;
break;
case 22: // mandelbrot GPU parametrable
cr=ci=0;
case 11: // mandelbrot GPU
case 12: // julia GPU
case 14: // burning ship GPU
case 17: // newton GPU
case 19: // mandelsinh GPU
iter.min = 1000;
iter.max = 10000;
iter.step = 1000;
iter.value = 1000;
break;
case 10: // Collatz
iter.min = 10;
iter.max = 1000;
iter.step = 10;
iter.value = 100;
break;
case 20: // Collatz GPU
case 21: // Collatz 2 GPU
iter.min = 10;
iter.max = 1000;
iter.step = 10;
iter.value = 100;
break;
case 5: // Koch 1
iter.min = 0;
iter.max = 9;
iter.step = 1;
iter.value = 4;
break;
case 6: // koch 2
iter.min = 0;
iter.max = 5;
iter.step = 1;
iter.value = 3;
break;
case 7: // sierpinski
iter.min = 0;
iter.max = 12;
iter.step = 1;
iter.value = 5;
break;
case 8: // heighway
iter.min = 0;
iter.max = 18;
iter.step = 1;
iter.value = 15;
break;
case 9: // tree
iter.min = 0;
iter.max = 18;
iter.step = 1;
iter.value = 14;
break;
case 15: // fern
iter.min = 0;
iter.max = 12;
iter.step = 1;
iter.value = 10;
break;
}
nmax = parseInt(iter.value);
document.getElementById("formiter2").value = iter.value;
draw();
}
function fractale(){
document.getElementById("juliacri").style="display:none;";
document.getElementById("mandelp").style="display:none;";
switch(frct){
case 1:
default:
mandelbrot();
break;
case 11:
mandelbrotGPU(xmin,xmax,ymin,ymax,nmax,cwidth,cheight);
document.getElementById('canvas').getContext('2d').drawImage(gpu.canvas, 0, 0);
break;
case 22:
document.getElementById("mandelp").style="display:block;";
mandelGPU(xmin,xmax,ymin,ymax,nmax,rad,cr,ci,cwidth,cheight);
document.getElementById('canvas').getContext('2d').drawImage(gpu.canvas, 0, 0);
break;
case 10:
collatz();
break;
case 20:
collGPU(xmin,xmax,ymin,ymax,nmax,cwidth,cheight);
document.getElementById('canvas').getContext('2d').drawImage(gpu.canvas, 0, 0);
break;
case 21:
coll2GPU(xmin,xmax,ymin,ymax,nmax,cwidth,cheight);
document.getElementById('canvas').getContext('2d').drawImage(gpu.canvas, 0, 0);
break;
case 13:
burnShip();
break;
case 14:
burningShipGPU(xmin,xmax,ymin,ymax,nmax,cwidth,cheight);
document.getElementById('canvas').getContext('2d').drawImage(gpu.canvas, 0, 0);
break;
case 2:
julia(1);
break;
case 16: