summaryrefslogtreecommitdiff
path: root/docs/posts/2023-04-30-n-body-simulation.html
blob: 658300461223acd6bbd6dd195f454d2bab246b1d (plain)
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
<!DOCTYPE html>
<html lang="en">
<head>
    
    <link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
    <link rel="stylesheet" href="/assets/main.css" />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>n-body solution generator</title>
    <meta name="og:site_name" content="Navan Chauhan" />
    <link rel="canonical" href="https://web.navan.dev/posts/2023-04-30-n-body-simulation.html" />
    <meta name="twitter:url" content="https://web.navan.dev/posts/2023-04-30-n-body-simulation.html />
    <meta name="og:url" content="https://web.navan.dev/posts/2023-04-30-n-body-simulation.html" />
    <meta name="twitter:title" content="n-body solution generator" />
    <meta name="og:title" content="n-body solution generator" />
    <meta name="description" content="n-body solution generator and solver" />
    <meta name="twitter:description" content="n-body solution generator and solver" />
    <meta name="og:description" content="n-body solution generator and solver" />
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="shortcut icon" href="/images/favicon.png" type="image/png" />
    <link rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan" />
    <meta name="twitter:image" content="https://web.navan.dev/images/opengraph/posts/2023-04-30-n-body-simulation.png" />
    <meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2023-04-30-n-body-simulation.png" />
    <meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k" />
    <script data-goatcounter="https://navanchauhan.goatcounter.com/count"
        async src="//gc.zgo.at/count.js"></script>
    <script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
    <link rel="manifest" href="manifest.json" />
    
</head>
<body>
    <center><nav style="display: block;">
|
<a href="/">home</a> |
<a href="/about/">about/links</a> |
<a href="/posts/">posts</a> |
<!--<a href="/publications/">publications</a> |-->
<!--<a href="/repo/">iOS repo</a> |-->
<a href="/feed.rss">RSS Feed</a> |
</nav>
</center>
    
<main>

	<h1>n-body solution generator</h1>

<p>This post requires JavaScript to be viewed properly :(</p>

<p>Adapted from the Numerics Tutorial - <a rel="noopener" target="_blank" href="https://github.com/kirklong/ThreeBodyBot/tree/master/NumericsTutorial">kirklong/ThreeBodyBot</a>. The Julia code has been rewritten in JavaScript.</p>

<p>Workflow:</p>

<ul>
<li>Understand the problem</li>
<li>Visualise a basic orbit</li>
<li>Solve and plot the classic figure-8 orbit</li>
<li>Random n-body solution generator</li>
</ul>

<p><strong>To workaround memory issues, the simulations are only run on-demand when the user clicks the respective button. Scroll down to the bottom of the page to see the results.</strong></p>

<h2>The n-body problem</h2>

<p>The n-body problem is a classic puzzle in physics (and thus astrophysics) and mathematics that deals with predicting the motion of multiple celestial objects that interact with each other through gravitational forces. </p>

<p>Imagine you are observing a <em>cosmic dance</em> between multiple celestial bodies, all tugging on one another as they move through space. The n-body problem aims to understand and predict the paths of these objects as they move through space.</p>

<p>When <code>n=2</code>, i.e we have only two objects, say the Earth and the Moon, we can easily apply Newtonian physics to predict their motion. However, when <code>n&gt;2</code>, the problem becomes much more difficult to solve analytically.[1] This is because each object feels the gravitational pull from all other objects, and thus the equations of motion become coupled and non-linear. </p>

<p>As the number of objects increases, finding an exact solution becomes impossible, and we rely on analytical approximations.</p>

<h2>Visualising a basic orbit</h2>

<p>If we want to create a n-body simulation in our browser, we need to figure out how we are going to visualise the motion of the objects. There are a few ways to do this, but the easiest is to use Plotly.js, a JavaScript library for creating interactive graphs. (An alternative is to use the HTML5 canvas element).</p>

<div class="codehilite">
<pre><span></span><code><span class="cm">/*</span>
<span class="cm"> * Earth - Sun Orbit Plot</span>
<span class="cm"> * Taken from Numerics tutorial</span>
<span class="cm"> */</span>

<span class="kd">const</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67e-11</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2e30</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">AU</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">v0</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">AU</span><span class="p">);</span><span class="w"> </span><span class="c1">// SI</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">,</span><span class="w"> </span><span class="nx">v</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">dv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">],</span><span class="w"> </span><span class="o">-</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Msun</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">]];</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">dr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[...</span><span class="nx">v</span><span class="p">];</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">dr</span><span class="p">,</span><span class="w"> </span><span class="nx">dv</span><span class="p">];</span>
<span class="p">}</span>

<span class="c1">// initialize system</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="nx">AU</span><span class="p">,</span><span class="w"> </span><span class="mf">0</span><span class="p">];</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">theta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">atan2</span><span class="p">(</span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">],</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">]);</span>
<span class="kd">let</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="nx">v0</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">theta</span><span class="p">),</span><span class="w"> </span><span class="nx">v0</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">theta</span><span class="p">)];</span>

<span class="kd">const</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="mf">1001</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">100</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">0.0</span><span class="p">);</span><span class="w"> </span><span class="c1">// years</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">yearSec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">365</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">24</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">3600</span><span class="p">;</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">dt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">t</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">t</span><span class="p">[</span><span class="mf">0</span><span class="p">])</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">yearSec</span><span class="p">;</span><span class="w"> </span><span class="c1">// s</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">x4Plot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">length</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="mf">0</span><span class="p">);</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">y4Plot</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">length</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="mf">0</span><span class="p">);</span>

<span class="c1">// integrate using RK4!</span>
<span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">t</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">k1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">,</span><span class="w"> </span><span class="nx">v</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">k2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k1</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">),</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k1</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">k3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">),</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">2</span><span class="p">)).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">k4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">dR</span><span class="p">(</span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]),</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">])).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">dt</span><span class="p">));</span>
<span class="w">  </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">ri</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">ri</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">k1</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k4</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">j</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">6</span><span class="p">);</span>
<span class="w">  </span><span class="nx">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">v</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">vi</span><span class="p">,</span><span class="w"> </span><span class="nx">j</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">vi</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">k1</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k2</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">k3</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">k4</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">j</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">6</span><span class="p">);</span>
<span class="w">  </span><span class="nx">x4Plot</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w">  </span><span class="nx">y4Plot</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span>
<span class="p">}</span>

<span class="c1">// make data for plot</span>
<span class="kd">var</span><span class="w"> </span><span class="nx">sun</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">};</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">earth</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">x4Plot</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">AU</span><span class="p">),</span><span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">y4Plot</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">AU</span><span class="p">)</span><span class="w"> </span><span class="p">};</span>
<span class="kd">const</span><span class="w"> </span><span class="nx">circle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="mf">1001</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">100</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span><span class="p">)),</span><span class="w"> </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="mf">1001</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">100</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span><span class="p">))</span><span class="w"> </span><span class="p">};</span>
</code></pre>
</div>

<p>This code simulates the orbit of Earth around the Sun, using a numerical method called the Runge-Kutta 4th order (RK4) method.</p>

<p>First, we define some constants:</p>

<p>G: the gravitational constant (6.67e-11 N m²/kg²)
Msun: the mass of the Sun (2e30 kg)
AU: an astronomical unit, the average distance between Earth and Sun (1.5e11 m)
v0: the initial velocity of Earth, calculated from its distance to the Sun
Next, the function dR takes the position r and velocity v of Earth as input and returns the rate of change in position (dr) and the rate of change in velocity (dv) using the gravitational force formula.</p>

<p>We then initialize the position r and velocity v of Earth, and create an array t that represents time in years, divided into 1001 steps. We also define yearSec as the number of seconds in a year and dt as the time step in seconds.</p>

<p>Now, we integrate the Earth's motion using the RK4 method. For each time step, we calculate the rates of change for position and velocity (k1, k2, k3, k4) and update Earth's position and velocity based on these. We store the updated position in x4Plot and y4Plot.</p>

<p>Finally, we normalize the position data by dividing it by the astronomical unit (AU) to make it more visually meaningful. We also create a circle for reference, which represents a perfect circular orbit. The code ends with the data for the Sun's position, Earth's orbit, and the reference circle ready to be plotted.</p>

<h3>Plotting the orbit</h3>

<p>Now that we have the data for the Sun's position, Earth's orbit, and the reference circle, we can plot them using Plotly.js.</p>

<div class="codehilite">
<pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">traceSun</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">sun</span><span class="p">.</span><span class="nx">x</span><span class="p">],</span>
<span class="w">      </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">sun</span><span class="p">.</span><span class="nx">y</span><span class="p">],</span>
<span class="w">      </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;markers&quot;</span><span class="p">,</span>
<span class="w">      </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;star&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">10</span><span class="p">,</span>
<span class="w">        </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;gold&quot;</span><span class="p">,</span>
<span class="w">      </span><span class="p">},</span>
<span class="w">      </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;Sun&quot;</span><span class="p">,</span>
<span class="w">    </span><span class="p">};</span>

<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceEarth</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">earth</span><span class="p">.</span><span class="nx">x</span><span class="p">,</span>
<span class="w">      </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">earth</span><span class="p">.</span><span class="nx">y</span><span class="p">,</span>
<span class="w">      </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;lines&quot;</span><span class="p">,</span>
<span class="w">      </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;white&quot;</span>
<span class="w">      </span><span class="p">},</span>
<span class="w">      </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;Earth&quot;</span><span class="p">,</span>
<span class="w">    </span><span class="p">};</span>

<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceOrbit</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">circle</span><span class="p">.</span><span class="nx">x</span><span class="p">,</span>
<span class="w">      </span><span class="nx">y</span><span class="o">:</span><span class="nx">circle</span><span class="p">.</span><span class="nx">y</span><span class="p">,</span>
<span class="w">      </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;lines&quot;</span><span class="p">,</span>
<span class="w">      </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;crimson&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">dash</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;dash&quot;</span>
<span class="w">      </span><span class="p">},</span>
<span class="w">      </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;1 AU Circle&quot;</span><span class="p">,</span>
<span class="w">    </span><span class="p">};</span>

<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">earthSunLayout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;Earth-Sun Orbit&quot;</span><span class="p">,</span>
<span class="w">      </span><span class="nx">xaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;x [AU]&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">],</span>
<span class="w">        </span><span class="nx">showgrid</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w">        </span><span class="nx">gridcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;rgba(255,255,255,0.5)&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">gridwidth</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span>
<span class="w">        </span><span class="nx">zeroline</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w">        </span><span class="nx">tickmode</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;auto&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">nticks</span><span class="o">:</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span>
<span class="w">      </span><span class="p">},</span>
<span class="w">      </span><span class="nx">yaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;y [AU]&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">],</span>
<span class="w">        </span><span class="nx">showgrid</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w">        </span><span class="nx">gridcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;rgba(255,255,255,0.5)&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">gridwidth</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span>
<span class="w">        </span><span class="nx">zeroline</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w">        </span><span class="nx">tickmode</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;auto&quot;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">nticks</span><span class="o">:</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span>
<span class="w">      </span><span class="p">},</span>
<span class="w">      </span><span class="nx">margin</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">l</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w">        </span><span class="nx">r</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w">        </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w">        </span><span class="nx">t</span><span class="o">:</span><span class="w"> </span><span class="mf">50</span><span class="p">,</span>
<span class="w">        </span><span class="nx">pad</span><span class="o">:</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span>
<span class="w">      </span><span class="p">},</span>
<span class="w">      </span><span class="nx">paper_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;black&quot;</span><span class="p">,</span>
<span class="w">      </span><span class="nx">plot_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;black&quot;</span><span class="p">,</span>
<span class="w">    </span><span class="p">};</span>
<span class="w">    </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">newPlot</span><span class="p">(</span><span class="s2">&quot;plot&quot;</span><span class="p">,[</span><span class="nx">traceSun</span><span class="p">,</span><span class="nx">traceEarth</span><span class="p">,</span><span class="nx">traceOrbit</span><span class="p">],</span><span class="w"> </span><span class="nx">earthSunLayout</span><span class="p">);</span>
</code></pre>
</div>

<h2>Figure of 8 orbit</h2>

<p>The figure of 8 solution[2] in the three-body problem refers to a unique and special trajectory where three celestial bodies (e.g., planets, stars) move in a figure of 8 shaped pattern around their mutual center of mass. This is special because it represents a stable and periodic solution to the three-body problem, which is known for its complexity and lack of general solutions.</p>

<p>In the figure of 8 solution, each of the three bodies follows the same looping path, but with a phase difference such that when one body is at one end of the loop, the other two are symmetrically positioned elsewhere along the path. The bodies maintain equal distances from each other throughout their motion, and their velocities and positions are perfectly balanced to maintain this periodic motion.</p>

<p>The figure of 8 is interesting because:</p>

<ul>
<li><p>It is a relatively stable solution, which means that the objects continue to follow the same looping path almost indefinitely.</p></li>
<li><p>It breaks down the notion that no simple periodic solutions exist for the three-body problem.</p></li>
<li><p>It looks cool!</p></li>
</ul>

<h3>Show me the code</h3>

<p>The code for this simulation is very similar to the Earth-Sun orbit simulation, except that we now have three bodies instead of two. We also use a different set of initial conditions to generate the figure of 8 orbit.</p>

<div class="codehilite">
<pre><span></span><code><span class="kd">function</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">vx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">vy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">];</span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">delta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">clone</span><span class="p">(</span><span class="nx">coords</span><span class="p">);</span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="nx">xn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="nx">yn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="nx">deltaVx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0</span><span class="p">;</span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="nx">deltaVy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0.0</span><span class="p">;</span>

<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">!==</span><span class="w"> </span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="nx">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">xn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">yn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="mf">2</span><span class="p">));</span><span class="w"> </span><span class="c1">// Euclidean distance</span>
<span class="w">                </span><span class="nx">deltaVx</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">xn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">sep</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span>
<span class="w">                </span><span class="nx">deltaVy</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">yn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="nx">sep</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">);</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="p">}</span>

<span class="w">        </span><span class="nx">delta</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">n</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">deltaVx</span><span class="p">;</span>
<span class="w">        </span><span class="nx">delta</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">n</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">deltaVy</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="nx">delta</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vx</span><span class="p">;</span>
<span class="w">    </span><span class="nx">delta</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vy</span><span class="p">;</span>

<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">delta</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k2</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>

<span class="w">    </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k2</span><span class="p">),</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">k4</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="o">/</span><span class="mf">6</span><span class="p">));</span>

<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">coords</span><span class="p">;</span>
<span class="p">}</span>

<span class="w">    </span><span class="c1">// Initial conditions setup</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">M</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">0.97000436</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="mf">0.97000436</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0.24208753</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">0.24208753</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">vx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0.4662036850</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">0.933240737</span><span class="p">,</span><span class="w"> </span><span class="mf">0.4662036850</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">vy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">0.4323657300</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">0.86473146</span><span class="p">,</span><span class="w"> </span><span class="mf">0.4323657300</span><span class="p">];</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">Ei</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="mf">1</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">0.97000436</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">0.24208753</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">))</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">0.97000436</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">pow</span><span class="p">(</span><span class="mf">0.24208753</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">0.5</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">sum</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">dotPow</span><span class="p">(</span><span class="nx">vx</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">),</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">dotPow</span><span class="p">(</span><span class="nx">vy</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">))));</span>

<span class="w">    </span><span class="kd">function</span><span class="w"> </span><span class="nx">linspace</span><span class="p">(</span><span class="nx">start</span><span class="p">,</span><span class="w"> </span><span class="nx">stop</span><span class="p">,</span><span class="w"> </span><span class="nx">num</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">step</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">stop</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">start</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">num</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">num</span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">start</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">step</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">i</span><span class="p">));</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">,</span><span class="w"> </span><span class="nx">vx</span><span class="p">,</span><span class="w"> </span><span class="nx">vy</span><span class="p">];</span>
<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">time</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">linspace</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">6.3259</span><span class="p">,</span><span class="w"> </span><span class="mf">1001</span><span class="p">);</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">deltaT</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>

<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">X</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">Y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">VX</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">VY</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">zeros</span><span class="p">(</span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">).</span><span class="nx">toArray</span><span class="p">();</span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">M</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">        </span><span class="nx">X</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w">        </span><span class="nx">Y</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w">        </span><span class="nx">VX</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">VX</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w">        </span><span class="nx">VY</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">idx</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">VY</span><span class="p">[</span><span class="nx">idx</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">idx</span><span class="p">]);</span>
<span class="w">    </span><span class="p">}</span>
</code></pre>
</div>

<p>The <code>deltaR</code> function computes the rate of change in position and velocity of the celestial bodies based on their current positions, velocities, and masses. It accounts for the gravitational forces between all bodies.</p>

<p>The <code>step</code> function performs a single RK4 integration step, updating the positions and velocities of the celestial bodies. It uses <code>deltaR</code> to compute the four increments (k1, k2, k3, and k4) and then updates the coordinates accordingly.</p>

<p>Next, the initial conditions for the figure-8 three-body problem are set. The masses (<code>M</code>), initial positions (<code>x</code>, <code>y</code>), and initial velocities (<code>vx</code>, <code>vy</code>) are provided. <code>Ei</code> calculates the initial total energy of the system.</p>

<p>The <code>linspace</code> function is defined to create a linearly spaced array of time points. <code>coords</code> is an array containing the positions and velocities for all bodies. The <code>time</code> array is created using <code>linspace</code>, and <code>deltaT</code> is set as the time step.</p>

<p><code>X</code>, <code>Y</code>, <code>VX</code>, and <code>VY</code> are 2D arrays that will store the positions and velocities of the celestial bodies over time. They are initialized with zeros and will be updated in the loop.</p>

<p>Finally, a loop iterates over each time step, updating the positions and velocities of the celestial bodies using the <code>step</code> function. The updated coordinates are stored in the <code>X</code>, <code>Y</code>, <code>VX</code>, and <code>VY</code> arrays.</p>

<h3>Animation?</h3>

<p>Now that we have time-series data, we need to animate it. We can use Plotly's animate function, as this does not force a full re-render, saving us some precious GPU and CPU cycles when we are trying to run this in the browser itself</p>

<div class="codehilite">
<pre><span></span><code><span class="w">    </span><span class="kd">function</span><span class="w"> </span><span class="nx">plotClassicFunc</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="kd">var</span><span class="w"> </span><span class="nx">tailLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span>
<span class="w">      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">tailLength</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">tailLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">tailLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">tailLength</span><span class="p">;</span>
<span class="w">      </span><span class="p">}</span>

<span class="w">      </span><span class="kd">var</span><span class="w"> </span><span class="nx">currentIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">plotIndex</span><span class="p">;</span>

<span class="w">     </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w">         </span><span class="nx">time</span><span class="p">[</span><span class="nx">currentIndex</span><span class="p">].</span><span class="nx">toFixed</span><span class="p">(</span><span class="mf">3</span><span class="p">);</span>
<span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">currentIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">      </span><span class="p">}</span>

<span class="w">       </span><span class="kd">const</span><span class="w"> </span><span class="nx">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span>
<span class="w">        </span><span class="p">{</span>
<span class="w">            </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w">            </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w">            </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;lines+markers&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;star&#39;</span><span class="p">,</span>
<span class="w">                </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span>
<span class="w">                </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">},</span>
<span class="w">            </span><span class="p">},</span>
<span class="w">            </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span>
<span class="w">            </span><span class="p">},</span>
<span class="w">            </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">,</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="p">{</span>
<span class="w">            </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w">            </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w">            </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;lines+markers&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;star&#39;</span><span class="p">,</span>
<span class="w">                </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span>
<span class="w">                </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">},</span>
<span class="w">            </span><span class="p">},</span>
<span class="w">            </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span>
<span class="w">            </span><span class="p">},</span>
<span class="w">            </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">,</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="p">{</span>
<span class="w">            </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">X</span><span class="p">[</span><span class="mf">2</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w">            </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">Y</span><span class="p">[</span><span class="mf">2</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="nx">tailLength</span><span class="p">,</span><span class="w"> </span><span class="nx">currentIndex</span><span class="p">),</span>
<span class="w">            </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;lines+markers&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;star&#39;</span><span class="p">,</span>
<span class="w">                </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">8</span><span class="p">,</span>
<span class="w">                </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="w"> </span><span class="p">},</span>
<span class="w">            </span><span class="p">},</span>
<span class="w">            </span><span class="nx">line</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">                </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span>
<span class="w">            </span><span class="p">},</span>
<span class="w">            </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">,</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">    </span><span class="p">];</span>

<span class="w">    </span><span class="c1">// width: 1000, height: 400</span>
<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">layout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;∞ Three-Body Problem: t = &#39;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="nx">currentIndex</span><span class="p">].</span><span class="nx">toFixed</span><span class="p">(</span><span class="mf">3</span><span class="p">),</span>
<span class="w">        </span><span class="nx">xaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;x&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">1.1</span><span class="p">,</span><span class="mf">1.1</span><span class="p">]</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="nx">yaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;y&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">scaleanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;x&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">scaleratio</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span>
<span class="w">            </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">0.5</span><span class="p">,</span><span class="mf">0.5</span><span class="p">]</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="nx">plot_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;black&#39;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">paper_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;black&#39;</span><span class="p">,</span>
<span class="w">        </span><span class="nx">font</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;white&#39;</span><span class="p">,</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">    </span><span class="p">};</span>

<span class="w">    </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">animate</span><span class="p">(</span><span class="s2">&quot;plot&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">data</span><span class="o">:</span><span class="w"> </span><span class="nx">data</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="o">:</span><span class="w"> </span><span class="nx">layout</span>
<span class="w">      </span><span class="p">},</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">staticPlot</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w">        </span><span class="nx">transition</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="nx">frame</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w">          </span><span class="nx">redraw</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">      </span><span class="p">});</span>
<span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">newPlot</span><span class="p">(</span><span class="s1">&#39;plot&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">data</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="p">);</span>
<span class="w">      </span><span class="p">}</span>


<span class="w">    </span><span class="nx">plotIndex</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="nx">delay</span><span class="p">;</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">plotClassic</span><span class="o">===</span><span class="kc">true</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">requestAnimationFrame</span><span class="p">(</span><span class="nx">plotClassicFunc</span><span class="p">);</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">      </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="w">      </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="p">}</span>
</code></pre>
</div>

<h2>"General" N-Body Solver</h2>

<h3>Show me the code!</h3>

<div class="codehilite">
<pre><span></span><code><span class="kd">function</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">3</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">k2</span><span class="p">,</span><span class="w"> </span><span class="mf">0.5</span><span class="p">)),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="nx">k4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaR</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">));</span>

<span class="w">    </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="nx">math</span><span class="p">.</span><span class="nx">add</span><span class="p">(</span><span class="nx">k1</span><span class="p">,</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k2</span><span class="p">),</span><span class="w"> </span><span class="nx">math</span><span class="p">.</span><span class="nx">multiply</span><span class="p">(</span><span class="mf">2.0</span><span class="p">,</span><span class="w"> </span><span class="nx">k3</span><span class="p">),</span><span class="w"> </span><span class="nx">k4</span><span class="p">),</span><span class="w"> </span><span class="mf">1</span><span class="o">/</span><span class="mf">6</span><span class="p">));</span>

<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="nx">coords</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">detectCollisionsEscape</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">x</span><span class="p">,</span><span class="w"> </span><span class="nx">y</span><span class="p">,</span><span class="w"> </span><span class="nx">vx</span><span class="p">,</span><span class="w"> </span><span class="nx">vy</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coords</span><span class="p">;</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">V</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vx</span><span class="p">.</span><span class="nx">map</span><span class="p">((</span><span class="nx">v</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nx">v</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">vy</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">));</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">R</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">V</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">v</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">);</span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="nx">collision</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>

<span class="w">  </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">n</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">rn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">R</span><span class="p">[</span><span class="nx">n</span><span class="p">],</span><span class="w"> </span><span class="nx">xn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">n</span><span class="p">],</span><span class="w"> </span><span class="nx">yn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">n</span><span class="p">];</span>
<span class="w">      </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">x</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">i</span><span class="w"> </span><span class="o">!==</span><span class="w"> </span><span class="nx">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">              </span><span class="kd">const</span><span class="w"> </span><span class="nx">minSep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">rn</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">R</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
<span class="w">              </span><span class="kd">const</span><span class="w"> </span><span class="nx">sep</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">((</span><span class="nx">xn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">x</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="nx">yn</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">y</span><span class="p">[</span><span class="nx">i</span><span class="p">])</span><span class="w"> </span><span class="o">**</span><span class="w"> </span><span class="mf">2</span><span class="p">);</span>
<span class="w">              </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">sep</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">minSep</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                  </span><span class="nx">collision</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="w">                  </span><span class="nx">collisionInds</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">n</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">];</span>
<span class="w">              </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">sep</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">                  </span><span class="nx">escape</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="w">                  </span><span class="nx">escapeInd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">n</span><span class="p">;</span>
<span class="w">                  </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">];</span>
<span class="w">              </span><span class="p">}</span>
<span class="w">          </span><span class="p">}</span>
<span class="w">      </span><span class="p">}</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">];</span>
<span class="p">}</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">nBodyStep</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Similar to our step function before, but keeping track of collisions</span>
<span class="w">  </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">step</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">);</span><span class="w"> </span><span class="c1">// Update the positions as we did before</span>
<span class="w">  </span><span class="c1">//console.log(detectCollisionsEscape(coords, deltaT, maxSep));</span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">detectCollisionsEscape</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="nx">maxSep</span><span class="p">);</span><span class="w"> </span><span class="c1">// Detect collisions/escapes</span>


<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">collision</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Do inelastic collision and delete extra body (2 -&gt; 1)</span>
<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">i1</span><span class="p">,</span><span class="w"> </span><span class="nx">i2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">;</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">x1</span><span class="p">,</span><span class="w"> </span><span class="nx">x2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">y1</span><span class="p">,</span><span class="w"> </span><span class="nx">y2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">vx1</span><span class="p">,</span><span class="w"> </span><span class="nx">vx2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">vy1</span><span class="p">,</span><span class="w"> </span><span class="nx">vy2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i1</span><span class="p">],</span><span class="w"> </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i2</span><span class="p">]];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">px1</span><span class="p">,</span><span class="w"> </span><span class="nx">px2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vx1</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vx2</span><span class="p">];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="p">[</span><span class="nx">py1</span><span class="p">,</span><span class="w"> </span><span class="nx">py2</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vy1</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">vy2</span><span class="p">];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">px</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">px2</span><span class="p">;</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">py</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">py1</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">py2</span><span class="p">;</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">newM</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">];</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">vfx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">px</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">newM</span><span class="p">;</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">vfy</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">py</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">newM</span><span class="p">;</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">x1</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">x2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]);</span><span class="w"> </span><span class="c1">// Center of mass</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="nx">y1</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">])</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i2</span><span class="p">]);</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vfx</span><span class="p">;</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">vfy</span><span class="p">;</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">].</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">      </span><span class="nx">masses</span><span class="p">[</span><span class="nx">i1</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">newM</span><span class="p">;</span>
<span class="w">      </span><span class="nx">masses</span><span class="p">.</span><span class="nx">splice</span><span class="p">(</span><span class="nx">i2</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span>
<span class="w">      </span><span class="nx">nBodies</span><span class="o">--</span><span class="p">;</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="c1">// Could also implement condition for escape where we stop calculating forces but I&#39;m too lazy for now</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">collision</span><span class="p">,</span><span class="w"> </span><span class="nx">collisionInds</span><span class="p">,</span><span class="w"> </span><span class="nx">escape</span><span class="p">,</span><span class="w"> </span><span class="nx">escapeInd</span><span class="p">];</span>
<span class="p">}</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">uniform</span><span class="p">(</span><span class="nx">min</span><span class="p">,</span><span class="w"> </span><span class="nx">max</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="nx">max</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">min</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">min</span><span class="p">;</span>
<span class="p">}</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">deepCopyCoordsArray</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="nx">arr</span><span class="p">.</span><span class="nx">map</span><span class="p">(</span><span class="nx">innerArr</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">innerArr</span><span class="p">.</span><span class="nx">slice</span><span class="p">());</span>
<span class="p">}</span>

<span class="kd">function</span><span class="w"> </span><span class="nx">genNBodyResults</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">tStop</span><span class="p">,</span><span class="w"> </span><span class="nx">nTPts</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodiesStop</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">10</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">6.67408313131313e-11</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>

<span class="w">  </span><span class="kd">var</span><span class="w"> </span><span class="nx">btn</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s2">&quot;startSim3&quot;</span><span class="p">);</span>
<span class="w">  </span><span class="c1">// Set button text to Solving</span>
<span class="w">  </span><span class="kd">var</span><span class="w"> </span><span class="nx">prevText</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">btn</span><span class="p">.</span><span class="nx">innerHTML</span><span class="p">;</span>
<span class="w">  </span><span class="nx">btn</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">&quot;Solving...&quot;</span><span class="p">;</span>

<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="nx">coords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="mf">0</span><span class="p">)];</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">Mstar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">2e30</span><span class="p">;</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">Mp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Mstar</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1e4</span><span class="p">;</span>

<span class="w">  </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Initialize coordinates on ~Keplerian orbits</span>
<span class="w">      </span><span class="kd">let</span><span class="w"> </span><span class="nx">accept</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span>
<span class="w">      </span><span class="kd">let</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<span class="w">      </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">accept</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Prevent a particle from spawning within 0.2 AU too close to &quot;star&quot;</span>
<span class="w">          </span><span class="nx">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">;</span><span class="w"> </span><span class="c1">// Say radius of 2 AU</span>
<span class="w">          </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">r</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mf">0.2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">              </span><span class="nx">accept</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">true</span><span class="p">;</span>
<span class="w">          </span><span class="p">}</span>
<span class="w">      </span><span class="p">}</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">theta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">uniform</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">PI</span><span class="p">);</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">theta</span><span class="p">);</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">r</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">theta</span><span class="p">);</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sqrt</span><span class="p">(</span><span class="nx">G</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">Mstar</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">r</span><span class="p">);</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">perturbedV</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">v</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1000</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">uniform</span><span class="p">(</span><span class="o">-</span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">);</span><span class="w"> </span><span class="c1">// Perturb the velocities ever so slightly</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">vTheta</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">atan2</span><span class="p">(</span><span class="nx">y</span><span class="p">,</span><span class="w"> </span><span class="nx">x</span><span class="p">);</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="p">;</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">-</span><span class="nx">perturbedV</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">sin</span><span class="p">(</span><span class="nx">vTheta</span><span class="p">);</span>
<span class="w">      </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">perturbedV</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">cos</span><span class="p">(</span><span class="nx">vTheta</span><span class="p">);</span>
<span class="w">  </span><span class="p">}</span>

<span class="w">  </span><span class="c1">//console.log(&#39;Initial coords:&#39;, coords);</span>


<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="nx">masses</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">(</span><span class="nx">nBodies</span><span class="p">).</span><span class="nx">fill</span><span class="p">(</span><span class="nx">Mp</span><span class="p">);</span><span class="w"> </span><span class="c1">// Initialize masses</span>
<span class="w">  </span><span class="nx">masses</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Mstar</span><span class="p">;</span><span class="w"> </span><span class="c1">// Make index one special as the central star</span>
<span class="w">  </span><span class="nx">coords</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">  </span><span class="nx">coords</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">  </span><span class="nx">coords</span><span class="p">[</span><span class="mf">2</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">  </span><span class="nx">coords</span><span class="p">[</span><span class="mf">3</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="c1">// Initialize central star at origin with no velocity</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">yearSec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">365</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">24</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">3600</span><span class="p">;</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">time</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Array</span><span class="p">.</span><span class="kr">from</span><span class="p">({</span><span class="w"> </span><span class="nx">length</span><span class="o">:</span><span class="w"> </span><span class="nx">nTPts</span><span class="w"> </span><span class="p">},</span><span class="w"> </span><span class="p">(</span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">tStop</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="p">(</span><span class="nx">nTPts</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">1</span><span class="p">)</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="nx">yearSec</span><span class="p">);</span><span class="w"> </span><span class="c1">// Years -&gt; s</span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">deltaT</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">1</span><span class="p">]</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="mf">0</span><span class="p">];</span>
<span class="w">  </span><span class="kd">let</span><span class="w"> </span><span class="nx">tInd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">coordsRecord</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">deepCopyCoordsArray</span><span class="p">(</span><span class="nx">coords</span><span class="p">)];</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">massRecord</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="nx">masses</span><span class="p">.</span><span class="nx">slice</span><span class="p">()];</span><span class="w"> </span><span class="c1">// Initialize records with initial conditions</span>


<span class="w">  </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="nx">tInd</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nTPts</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="nx">nBodies</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="nx">nBodiesStop</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="c1">//console.log(&#39;Initial coords:&#39;, coords);</span>
<span class="w">    </span><span class="p">[</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nBodyStep</span><span class="p">(</span><span class="nx">coords</span><span class="p">,</span><span class="w"> </span><span class="nx">masses</span><span class="p">,</span><span class="w"> </span><span class="nx">deltaT</span><span class="p">,</span><span class="w"> </span><span class="mf">10</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">,</span><span class="w"> </span><span class="nx">nBodies</span><span class="p">,</span><span class="w"> </span><span class="nx">G</span><span class="p">);</span><span class="w"> </span><span class="c1">// Update</span>
<span class="w">    </span><span class="nx">coordsRecord</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">deepCopyCoordsArray</span><span class="p">(</span><span class="nx">coords</span><span class="p">));</span>
<span class="w">    </span><span class="nx">massRecord</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">masses</span><span class="p">.</span><span class="nx">slice</span><span class="p">());</span><span class="w"> </span><span class="c1">// Add to records</span>
<span class="w">    </span><span class="nx">tInd</span><span class="o">++</span><span class="p">;</span>
<span class="w">    </span><span class="nx">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">time</span><span class="p">[</span><span class="nx">tInd</span><span class="p">];</span>
<span class="w">    </span><span class="c1">//console.log(`currently at t = ${(t / yearSec).toFixed(2)} years\r`);</span>
<span class="w">  </span><span class="p">}</span>
<span class="w">  </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="sb">`final time = </span><span class="si">${</span><span class="nx">time</span><span class="p">[</span><span class="nx">tInd</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">yearSec</span><span class="si">}</span><span class="sb"> years with </span><span class="si">${</span><span class="nx">nBodies</span><span class="si">}</span><span class="sb"> bodies remaining`</span><span class="p">);</span>

<span class="w">  </span><span class="c1">// Set button text to Start Simulation</span>
<span class="w">  </span><span class="nx">btn</span><span class="p">.</span><span class="nx">innerHTML</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">prevText</span><span class="p">;</span>

<span class="w">  </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsRecord</span><span class="p">,</span><span class="w"> </span><span class="nx">massRecord</span><span class="p">,</span><span class="w"> </span><span class="nx">time</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="nx">tInd</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">1</span><span class="p">)];</span>
<span class="p">}</span>


<span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsRecordR</span><span class="p">,</span><span class="w"> </span><span class="nx">_</span><span class="p">,</span><span class="w"> </span><span class="nx">tR</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">genNBodyResults</span><span class="p">(</span><span class="mf">256</span><span class="p">,</span><span class="mf">1</span><span class="p">,</span><span class="mf">1001</span><span class="p">);</span>
<span class="w">    </span><span class="c1">//console.log(coordsRecordR);</span>
<span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">yearSec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">365</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">24</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">3600</span><span class="p">;</span>

<span class="w">    </span><span class="kd">function</span><span class="w"> </span><span class="nx">createFrame</span><span class="p">(</span><span class="nx">coordsR</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="nx">coordsR</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="o">!</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="k">return</span><span class="w"> </span><span class="p">[];</span>
<span class="w">      </span><span class="p">}</span>

<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceCentralStar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">0</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">],</span>
<span class="w">          </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">1</span><span class="p">][</span><span class="mf">0</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">],</span>
<span class="w">          </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;markers&#39;</span><span class="p">,</span>
<span class="w">          </span><span class="nx">type</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;scatter&#39;</span><span class="p">,</span>
<span class="w">          </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Central star&#39;</span><span class="p">,</span>
<span class="w">          </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;gold&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;star&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">10</span><span class="w"> </span><span class="p">},</span>
<span class="w">      </span><span class="p">};</span>

<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">xCoords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">0</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">);</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">yCoords</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">coordsR</span><span class="p">[</span><span class="mf">1</span><span class="p">].</span><span class="nx">slice</span><span class="p">(</span><span class="mf">1</span><span class="p">).</span><span class="nx">map</span><span class="p">(</span><span class="nx">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">y</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1.5e11</span><span class="p">);</span>

<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">traceOtherBodies</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="nx">xCoords</span><span class="p">,</span>
<span class="w">          </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="nx">yCoords</span><span class="p">,</span>
<span class="w">          </span><span class="nx">mode</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;markers&#39;</span><span class="p">,</span>
<span class="w">          </span><span class="nx">type</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;scatter&#39;</span><span class="p">,</span>
<span class="w">          </span><span class="nx">name</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">,</span>
<span class="w">          </span><span class="nx">marker</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">color</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;dodgerblue&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">symbol</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;circle&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">2</span><span class="w"> </span><span class="p">},</span>
<span class="w">      </span><span class="p">};</span>

<span class="w">      </span><span class="k">return</span><span class="w"> </span><span class="p">[</span><span class="nx">traceCentralStar</span><span class="p">,</span><span class="w"> </span><span class="nx">traceOtherBodies</span><span class="p">];</span>
<span class="w">  </span><span class="p">}</span>


<span class="w">  </span><span class="kd">function</span><span class="w"> </span><span class="nx">createLayout</span><span class="p">(</span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">            </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="sb">`N-Body Problem: t = </span><span class="si">${</span><span class="nb">Number</span><span class="p">(</span><span class="nx">t</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="nx">yearSec</span><span class="p">).</span><span class="nx">toFixed</span><span class="p">(</span><span class="mf">3</span><span class="p">)</span><span class="si">}</span><span class="sb"> years`</span><span class="p">,</span>
<span class="w">            </span><span class="nx">x</span><span class="o">:</span><span class="w"> </span><span class="mf">0.03</span><span class="p">,</span>
<span class="w">            </span><span class="nx">y</span><span class="o">:</span><span class="w"> </span><span class="mf">0.97</span><span class="p">,</span>
<span class="w">            </span><span class="nx">xanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;left&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">yanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;top&#39;</span><span class="p">,</span>
<span class="w">            </span><span class="nx">font</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">size</span><span class="o">:</span><span class="w"> </span><span class="mf">14</span><span class="w"> </span><span class="p">},</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="nx">xaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;x [AU]&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">2.1</span><span class="p">,</span><span class="w"> </span><span class="mf">2.1</span><span class="p">]</span><span class="w"> </span><span class="p">},</span>
<span class="w">        </span><span class="nx">yaxis</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">title</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;y [AU]&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="o">-</span><span class="mf">2.1</span><span class="p">,</span><span class="w"> </span><span class="mf">2.1</span><span class="p">],</span><span class="w"> </span><span class="nx">scaleanchor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;x&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">scaleratio</span><span class="o">:</span><span class="w"> </span><span class="mf">1</span><span class="w"> </span><span class="p">},</span>
<span class="w">        </span><span class="nx">showlegend</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w">        </span><span class="nx">margin</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">l</span><span class="o">:</span><span class="w"> </span><span class="mf">60</span><span class="p">,</span><span class="w"> </span><span class="nx">r</span><span class="o">:</span><span class="w"> </span><span class="mf">40</span><span class="p">,</span><span class="w"> </span><span class="nx">t</span><span class="o">:</span><span class="w"> </span><span class="mf">40</span><span class="p">,</span><span class="w"> </span><span class="nx">b</span><span class="o">:</span><span class="w"> </span><span class="mf">40</span><span class="w"> </span><span class="p">},</span>
<span class="w">        </span><span class="nx">width</span><span class="o">:</span><span class="w"> </span><span class="mf">800</span><span class="p">,</span>
<span class="w">        </span><span class="nx">height</span><span class="o">:</span><span class="w"> </span><span class="mf">800</span><span class="p">,</span>
<span class="w">        </span><span class="nx">plot_bgcolor</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;black&#39;</span><span class="p">,</span>
<span class="w">    </span><span class="p">};</span>
<span class="p">}</span>

<span class="w">  </span><span class="kd">function</span><span class="w"> </span><span class="nx">animateNBodyProblem</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">nFrames</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">tR</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>

<span class="w">  </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="nx">nFrames</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">frameData</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">createFrame</span><span class="p">(</span><span class="nx">coordsRecordR</span><span class="p">[</span><span class="nx">i</span><span class="p">]);</span>
<span class="w">      </span><span class="kd">const</span><span class="w"> </span><span class="nx">layout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">createLayout</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span>
<span class="w">      </span><span class="c1">//Plotly.newPlot(plotDiv, frameData, layout);</span>
<span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">animate</span><span class="p">(</span><span class="s2">&quot;plot&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">data</span><span class="o">:</span><span class="w"> </span><span class="nx">frameData</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="o">:</span><span class="w"> </span><span class="nx">layout</span>
<span class="w">      </span><span class="p">},</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="nx">staticPlot</span><span class="o">:</span><span class="w"> </span><span class="kc">true</span><span class="p">,</span>
<span class="w">        </span><span class="nx">transition</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w">        </span><span class="p">},</span>
<span class="w">        </span><span class="nx">frame</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<span class="w">          </span><span class="nx">duration</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<span class="w">          </span><span class="nx">redraw</span><span class="o">:</span><span class="w"> </span><span class="kc">false</span><span class="p">,</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">      </span><span class="p">});</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="nx">err</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">      </span><span class="nx">Plotly</span><span class="p">.</span><span class="nx">newPlot</span><span class="p">(</span><span class="s1">&#39;plot&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">frameData</span><span class="p">,</span><span class="w"> </span><span class="nx">layout</span><span class="p">);</span>
<span class="w">    </span><span class="p">}</span>
<span class="w">  </span><span class="p">}</span>
<span class="p">}</span>

<span class="nx">animateNBodyProblem</span><span class="p">();</span>
</code></pre>
</div>

<h2>Playground</h2>

<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.8.0/math.js"></script>

<script src="/assets/n-body/script.js"></script>

<div id="main-plot-div">
<noscript>
The simulations require JavaScript to be viewed properly :(
</noscript>

<div id="plot"></div>

<label for="speedControl">Speed for 3-Body Visualisation</label>
<input type="range" min="1" max="20" value="10" id="speedControl">
<label for="nBodies">Number of Bodies for n-Body Simulation (5-500)</label>
<input type="range" min="5" max="500" value="256" id="nBodies">
<label for="distanceAU">Position Range for Random 3-Body Simulations (1-55)</label>
<input type="number" min="1" max="55" value="10" id="distanceAU">
<label for="vRange">Velocity Range for Random 3-Body Simulation (1-10e4)<label>
<input type="number" min="1" max="10000" value="7e3" id="vRange">

</div>

<p><button type="button" id="startSim1" onclick="plotEarthSun()">Sun-Earth Orbit</button>
<button type="button" id="startSim2" onclick="plotClassic3BodyProblem()">Classic 3-Body Problem</button>
<button type="button" id="startSim3" onclick="plotRandomNBodySimulation()">Random n-Body Simulation</button>
<button type="button" id="startSim4" onclick="plotRandom3BodySimulation()">Random 3-Body Simulation</button></p>

<script>
function clearAl() {
    plotOrbit = false;
    plotClassic = false;
    plotRandom = false;
    plotRandom3Body = false;
}

function plotEarthSun() {
    clearAl();
    plotOrbit = true;
    calculateAndPlot();
    setTimeout(function(){
        calculateAndPlot()
      }, 500);
    }

function plotClassic3BodyProblem() {
      clearAl();
      plotClassic = true;
      calculateAndPlot();
      setTimeout(function(){
        calculateAndPlot()
      }, 500);
    }

function plotRandomNBodySimulation() {
      clearAl();
      plotRandom = true;
      calculateAndPlot();
    }

  function plotRandom3BodySimulation() {
    clearAl();
    plotRandom3Body = true;
    calculateAndPlot();
  }



</script>

<script>
    document.getElementById("speedControl").addEventListener("input", (event) => {
    const speedPercentage = parseInt(event.target.value, 10);
    delay = speedPercentage; // Adjust the delay based on the trackbar value
});
    document.getElementById("nBodies").addEventListener("input", (event) => {
    nBodies = parseInt(event.target.value, 10);
    });

 </script>     

<h2>References</h2>

<ol>
<li>Barrow-Green, June (2008), "The Three-Body Problem", in Gowers, Timothy; Barrow-Green, June; Leader, Imre (eds.), <em>The Princeton Companion to Mathematics</em>, Princeton University Press, pp. 726–728</li>
<li>Moore, Cristopher (1993), "Braids in classical dynamics", <em>Physical Review Letters</em>, 70 (24): 3675–3679</li>
</ol>

	<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
	<script data-isso="https://comments.navan.dev/"
        src="https://comments.navan.dev/js/embed.min.js"></script>
	<section id="isso-thread">
	    <noscript>Javascript needs to be activated to view comments.</noscript>
	</section>
</main>

    <script src="assets/manup.min.js"></script>
    <script src="/pwabuilder-sw-register.js"></script>    
</body>
</html>