dotmax 0.1.8

High-performance terminal braille rendering for images, animations, and graphics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! Chaos / strange-attractor progress bars.
//!
//! Each style implements a real dynamical system from the literature, iterating
//! from a fixed seed each frame (stateless) so the bar is a pure function of
//! `(progress, time)`. Attractor orbits are mapped into dot-space coordinates
//! and revealed progressively via `ctx.eased`. Parameter animation uses
//! `ctx.time` to drive slow rotations, phase-shifts, and bifurcations.
//!
//! Systems implemented:
//! - **Lorenz** — the canonical butterfly attractor (σ=10, ρ=28, β=8/3).
//! - **Rössler** — a single spiral band (a=0.2, b=0.2, c=5.7).
//! - **Clifford** — algebraic iterated-function system with animated params.
//! - **De Jong** — sinusoidal IFS, slowly morphing.
//! - **Hénon map** — folded parabola (a=1.4, b=0.3).
//! - **Logistic bifurcation** — bifurcation diagram swept across the bar.
//! - **Double pendulum** — Lagrangian chaotic trace.
//! - **Standard map** — area-preserving twist map on the torus.
//! - **Gingerbreadman map** — triangular symmetry cellular automaton-like.
//! - **Duffing oscillator** — phase portrait of the forced anharmonic well.
//! - **Tinkerbell map** — complex-parameter fractal boundary.

use super::super::draw;
use super::super::{BarContext, ProgressStyle};
use crate::{BrailleGrid, DotmaxError};
use std::f32::consts::PI;

// ---------------------------------------------------------------------------
// Deterministic hash helper for seeding
// ---------------------------------------------------------------------------

#[inline(always)]
fn hash(n: u32) -> u32 {
    let mut x = n.wrapping_mul(2_654_435_761);
    x ^= x >> 15;
    x.wrapping_mul(2_246_822_519)
}

// ---------------------------------------------------------------------------
// Registry
// ---------------------------------------------------------------------------

/// All styles in the `chaos` theme.
///
/// Returns 11 dynamical-system progress bars. Every style is stateless and
/// renders from a fixed seed each frame; no mutable state is held.
pub fn styles() -> Vec<Box<dyn ProgressStyle>> {
    vec![
        Box::new(LorenzAttractor),
        Box::new(RosslerAttractor),
        Box::new(CliffordAttractor),
        Box::new(DeJongAttractor),
        Box::new(HenonMap),
        Box::new(LogisticBifurcation),
        Box::new(DoublePendulum),
        Box::new(StandardMap),
        Box::new(Gingerbreadman),
        Box::new(DuffingOscillator),
        Box::new(TinkerbellMap),
    ]
}

// ---------------------------------------------------------------------------
// 1. Lorenz Attractor
//    dx/dt = σ(y − x)        σ = 10
//    dy/dt = x(ρ − z) − y    ρ = 28
//    dz/dt = xy − βz         β = 8/3
//
//    Progress reveals orbit length. Time rotates the (x,z) projection.
// ---------------------------------------------------------------------------

struct LorenzAttractor;

impl ProgressStyle for LorenzAttractor {
    fn name(&self) -> &str {
        "lorenz"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Lorenz butterfly attractor: σ=10 ρ=28 β=8/3 — orbit revealed by progress, time rotates projection"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let sigma: f32 = 10.0;
        let rho: f32 = 28.0;
        let beta: f32 = 8.0 / 3.0;
        let dt: f32 = 0.01;

        // Total steps budget; eased controls how many we draw.
        let n_total: usize = 2800;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // Fixed seed — attractor min/max known analytically.
        let x_range = (-20.0_f32, 20.0_f32);
        let z_range = (0.0_f32, 50.0_f32);

        // Slow rotation of the projection plane with time.
        let angle = ctx.time * 0.3;
        let cos_a = angle.cos();
        let sin_a = angle.sin();

        let mut x: f32 = 0.1;
        let mut y: f32 = 0.0;
        let mut z: f32 = 0.0;

        // Burn-in to get onto the attractor.
        for _ in 0..300 {
            let dx = sigma * (y - x);
            let dy = x * (rho - z) - y;
            let dz = x * y - beta * z;
            x += dx * dt;
            y += dy * dt;
            z += dz * dt;
        }

        for _ in 0..n_draw.min(n_total) {
            let dx = sigma * (y - x);
            let dy = x * (rho - z) - y;
            let dz = x * y - beta * z;
            x += dx * dt;
            y += dy * dt;
            z += dz * dt;

            // Project: rotate in (x,y) plane then use (proj_x, z) for screen.
            let proj_x = x * cos_a - y * sin_a;
            let norm_x = (proj_x - x_range.0) / (x_range.1 - x_range.0);
            let norm_z = (z - z_range.0) / (z_range.1 - z_range.0);

            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_z.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        // Progress-bar underline: fill the bottom dot row to show eased.
        let filled = (ctx.eased * dw as f32) as usize;
        draw::hline(
            grid,
            0,
            filled.saturating_sub(1).min(dw.saturating_sub(1)),
            dh - 1,
        );

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 2. Rössler Attractor
//    dx/dt = −y − z
//    dy/dt = x + ay          a = 0.2
//    dz/dt = b + z(x − c)   b = 0.2, c = 5.7
//
//    Progress reveals orbit length. Time phase-shifts the (x,y) projection.
// ---------------------------------------------------------------------------

struct RosslerAttractor;

impl ProgressStyle for RosslerAttractor {
    fn name(&self) -> &str {
        "rossler"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Rössler spiral band: a=0.2 b=0.2 c=5.7 — single spiralling lobe revealed by progress"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let a: f32 = 0.2;
        let b: f32 = 0.2;
        let c: f32 = 5.7;
        let dt: f32 = 0.025;

        let n_total = 2400usize;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // Known attractor x,y range ≈ [-11, 11] × [-11, 11].
        let xy_min = -13.0_f32;
        let xy_max = 13.0_f32;

        let angle = ctx.time * 0.2;
        let cos_a = angle.cos();
        let sin_a = angle.sin();

        let mut x: f32 = 1.0;
        let mut y: f32 = 0.0;
        let mut z: f32 = 0.0;

        // Burn-in.
        for _ in 0..200 {
            let dx = -y - z;
            let dy = x + a * y;
            let dz = b + z * (x - c);
            x += dx * dt;
            y += dy * dt;
            z += dz * dt;
        }

        for _ in 0..n_draw.min(n_total) {
            let dx = -y - z;
            let dy = x + a * y;
            let dz = b + z * (x - c);
            x += dx * dt;
            y += dy * dt;
            z += dz * dt;

            // Rotate in (x,y).
            let rx = x * cos_a - y * sin_a;
            let ry = x * sin_a + y * cos_a;

            let norm_x = (rx - xy_min) / (xy_max - xy_min);
            let norm_y = (ry - xy_min) / (xy_max - xy_min);

            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_y.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 3. Clifford Attractor (IFS)
//    x_{n+1} = sin(a·y) + c·cos(a·x)
//    y_{n+1} = sin(b·x) + d·cos(b·y)
//
//    a,b animated with time; c,d fixed. 3000 iterations, eased fraction shown.
// ---------------------------------------------------------------------------

struct CliffordAttractor;

impl ProgressStyle for CliffordAttractor {
    fn name(&self) -> &str {
        "clifford"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Clifford IFS: sin/cos parameter orbit — params a,b slowly drift with time"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        // Animate a and b in ranges that keep the attractor bounded and chaotic.
        let a = -1.7 + 0.4 * (ctx.time * 0.17).sin();
        let b = 1.8 + 0.3 * (ctx.time * 0.13).cos();
        let c = -1.9_f32;
        let d = -0.4_f32;

        let n_total = 3000usize;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // Clifford attractor bounded by approx. ±2.
        let xy_min = -2.5_f32;
        let xy_max = 2.5_f32;
        let range = xy_max - xy_min;

        let mut x: f32 = 0.1;
        let mut y: f32 = 0.0;

        for _ in 0..n_draw.min(n_total) {
            let xn = (a * y).sin() + c * (a * x).cos();
            let yn = (b * x).sin() + d * (b * y).cos();
            x = xn;
            y = yn;

            let norm_x = (x - xy_min) / range;
            let norm_y = (y - xy_min) / range;
            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_y.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 4. De Jong Attractor (IFS)
//    x_{n+1} = sin(a·y) − cos(b·x)
//    y_{n+1} = sin(c·x) − cos(d·y)
//
//    All four params animated slowly with time.
// ---------------------------------------------------------------------------

struct DeJongAttractor;

impl ProgressStyle for DeJongAttractor {
    fn name(&self) -> &str {
        "de-jong"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "De Jong IFS: sin−cos iterated map — all four params drift with time for morphing forms"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let t = ctx.time;
        let a = -2.0 + 0.5 * (t * 0.11).sin();
        let b = -2.0 + 0.4 * (t * 0.07).cos();
        let c = 1.2 + 0.5 * (t * 0.09).sin();
        let d = 2.0 + 0.3 * (t * 0.13).cos();

        let n_total = 3000usize;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        let xy_min = -2.5_f32;
        let xy_max = 2.5_f32;
        let range = xy_max - xy_min;

        let mut x: f32 = 0.1;
        let mut y: f32 = 0.1;

        for _ in 0..n_draw.min(n_total) {
            let xn = (a * y).sin() - (b * x).cos();
            let yn = (c * x).sin() - (d * y).cos();
            x = xn;
            y = yn;

            let norm_x = (x - xy_min) / range;
            let norm_y = (y - xy_min) / range;
            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_y.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 5. Hénon Map
//    x_{n+1} = 1 − a·x² + y    a = 1.4
//    y_{n+1} = b·x              b = 0.3
//
//    Classic banana-shaped strange attractor. Progress = steps drawn.
//    Time shifts the plotting window to reveal hidden structure.
// ---------------------------------------------------------------------------

struct HenonMap;

impl ProgressStyle for HenonMap {
    fn name(&self) -> &str {
        "henon"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Hénon map: a=1.4 b=0.3 — banana strange attractor self-similar at every scale"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let a: f32 = 1.4;
        let b: f32 = 0.3;

        let n_total = 2800usize;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // Known attractor bounds: x ∈ [−1.33, 1.33], y ∈ [−0.43, 0.43].
        // Add margin: ±1.5, ±0.5.
        let x_min = -1.5_f32;
        let x_max = 1.5_f32;
        let y_min = -0.5_f32;
        let y_max = 0.5_f32;

        let mut x: f32 = 0.1;
        let mut y: f32 = 0.1;

        // Small burn-in.
        for _ in 0..50 {
            let xn = 1.0 - a * x * x + y;
            let yn = b * x;
            x = xn;
            y = yn;
        }

        for _ in 0..n_draw.min(n_total) {
            let xn = 1.0 - a * x * x + y;
            let yn = b * x;
            x = xn;
            y = yn;

            // Bail out if the orbit escaped.
            if x.abs() > 10.0 || y.abs() > 10.0 {
                break;
            }

            let norm_x = (x - x_min) / (x_max - x_min);
            let norm_y = (y - y_min) / (y_max - y_min);
            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_y.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 6. Logistic Map Bifurcation Diagram
//    x_{n+1} = r · x · (1 − x),   x₀ = 0.5
//
//    The x-axis is the bar: r swept from 2.8 to 4.0 left→right.
//    ctx.eased gates how far right r has been revealed.
//    At each r column we iterate 300 steps (discard) + 80 (plot).
//    The y-axis is x (attractor values). Time shifts x₀ slightly.
//
//    This is the most gorgeous bar — the entire period-doubling cascade
//    is drawn column by column as progress advances.
// ---------------------------------------------------------------------------

struct LogisticBifurcation;

impl ProgressStyle for LogisticBifurcation {
    fn name(&self) -> &str {
        "logistic-bifurcation"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Logistic map bifurcation diagram r∈[2.8,4.0] — period-doubling route to chaos column by column"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let r_min: f32 = 2.8;
        let r_max: f32 = 4.0;

        // How many dot-columns to reveal (controlled by eased progress).
        let reveal_cols = (ctx.eased * dw as f32).round() as usize;

        // x₀ drifts very slowly with time (barely visible — keeps it alive).
        let x0 = 0.5 + 0.01 * (ctx.time * 0.05).sin();

        for col in 0..reveal_cols.min(dw) {
            let r = r_min + (col as f32 / dw.saturating_sub(1).max(1) as f32) * (r_max - r_min);

            let mut x = x0.clamp(0.01, 0.99);

            // Discard transient.
            for _ in 0..300 {
                x = r * x * (1.0 - x);
            }

            // Plot attractor.
            for _ in 0..80 {
                x = r * x * (1.0 - x);
                // x is in [0,1]; map to dot row.
                let row = ((1.0 - x) * (dh - 1) as f32).round() as usize;
                if row < dh {
                    draw::dot(grid, col, row);
                }
            }
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 7. Double Pendulum Trace
//    Lagrangian equations (equal masses m=1, equal lengths l=1, g=9.81):
//
//    θ̈₁ = [−g(2m)sinθ₁ − mg·sin(θ₁−2θ₂) − 2sin(θ₁−θ₂)·m(θ̇₂²l+θ̇₁²l·cos(θ₁−θ₂))]
//           / [l(2m − m·cos(2θ₁−2θ₂))]
//
//    θ̈₂ = [2sin(θ₁−θ₂)(θ̇₁²l·2m + g·2m·cosθ₁ + θ̇₂²l·m·cos(θ₁−θ₂))]
//           / [l(2m − m·cos(2θ₁−2θ₂))]
//
//    Integrated with RK4 for accuracy. Progress reveals the chaotic tip trace.
// ---------------------------------------------------------------------------

struct DoublePendulum;

impl ProgressStyle for DoublePendulum {
    fn name(&self) -> &str {
        "double-pendulum"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Double pendulum chaotic trace — RK4 Lagrangian integration, tip path revealed by progress"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let g: f32 = 9.81;
        let dt: f32 = 0.02;

        let n_total: usize = 2000;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // State: [θ₁, ω₁, θ₂, ω₂]
        let mut th1: f32 = PI / 2.0 + 0.05 * (ctx.time * 0.001).sin();
        let mut om1: f32 = 0.0;
        let mut th2: f32 = PI + 0.03;
        let mut om2: f32 = 0.0;

        // The tip of the second rod (length 1+1=2 from pivot) traces a
        // path in Cartesian space. Scale so it fits in ±2 units → dot grid.
        let scale = 2.0_f32; // max extent (full length of both rods)

        let cx = dw as f32 / 2.0;
        let cy = dh as f32 / 2.0;

        let deriv = |th1: f32, om1: f32, th2: f32, om2: f32| -> (f32, f32, f32, f32) {
            let delta = th2 - th1;
            let denom = 2.0 - (2.0 * delta).cos(); // (2m - m·cos(2θ₁−2θ₂)) / m
            let denom = if denom.abs() < 1e-6 {
                1e-6_f32.copysign(denom)
            } else {
                denom
            };

            let dom1 = (-g * 2.0 * th1.sin()
                - g * delta.cos() * 2.0 * th2.sin() // approx — simplified form
                - 2.0 * delta.sin() * (om2 * om2 + om1 * om1 * delta.cos()))
                / denom;

            let dom2 = (2.0
                * delta.sin()
                * (om1 * om1 * 2.0 + g * 2.0 * th1.cos() + om2 * om2 * delta.cos()))
                / denom;

            (om1, dom1, om2, dom2)
        };

        for i in 0..n_draw.min(n_total) {
            // RK4 step.
            let (k1a, k1b, k1c, k1d) = deriv(th1, om1, th2, om2);
            let (k2a, k2b, k2c, k2d) = deriv(
                th1 + k1a * dt / 2.0,
                om1 + k1b * dt / 2.0,
                th2 + k1c * dt / 2.0,
                om2 + k1d * dt / 2.0,
            );
            let (k3a, k3b, k3c, k3d) = deriv(
                th1 + k2a * dt / 2.0,
                om1 + k2b * dt / 2.0,
                th2 + k2c * dt / 2.0,
                om2 + k2d * dt / 2.0,
            );
            let (k4a, k4b, k4c, k4d) = deriv(
                th1 + k3a * dt,
                om1 + k3b * dt,
                th2 + k3c * dt,
                om2 + k3d * dt,
            );

            th1 += dt / 6.0 * (k1a + 2.0 * k2a + 2.0 * k3a + k4a);
            om1 += dt / 6.0 * (k1b + 2.0 * k2b + 2.0 * k3b + k4b);
            th2 += dt / 6.0 * (k1c + 2.0 * k2c + 2.0 * k3c + k4c);
            om2 += dt / 6.0 * (k1d + 2.0 * k2d + 2.0 * k3d + k4d);

            // Tip position (second bob).
            let tip_x = th1.sin() + th2.sin();
            let tip_y = -(th1.cos() + th2.cos()); // y positive downward

            // Plot every step.
            let _ = i; // suppress unused warning
            let px = (cx + tip_x / scale * cx) as i32;
            let py = (cy + tip_y / scale * cy) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 8. Standard Map (Chirikov–Taylor map)
//    p_{n+1} = p_n + K·sin(θ_n)   (mod 2π)
//    θ_{n+1} = θ_n + p_{n+1}      (mod 2π)
//
//    K = stochasticity parameter. Progress ramps K from 0 → 4π.
//    Multiple orbits launched from a grid of initial conditions.
//    Time slowly shifts the phase-space viewport.
// ---------------------------------------------------------------------------

struct StandardMap;

impl ProgressStyle for StandardMap {
    fn name(&self) -> &str {
        "standard-map"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Chirikov–Taylor standard map — K grows with progress, KAM tori shatter into chaos"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        // K = stochasticity. K=0 → all circles. K≥4 → fully chaotic.
        let k = ctx.eased * 4.0 * PI;

        // Launch orbits from a small grid of initial conditions.
        let n_seeds = 8usize;
        let n_iter = 200usize;

        for si in 0..n_seeds {
            for sj in 0..n_seeds {
                let mut theta = 2.0 * PI * si as f32 / n_seeds as f32;
                let mut p = 2.0 * PI * sj as f32 / n_seeds as f32;

                // Small time-based phase shift.
                theta += ctx.time * 0.04;

                for _ in 0..n_iter {
                    p = (p + k * theta.sin()).rem_euclid(2.0 * PI);
                    theta = (theta + p).rem_euclid(2.0 * PI);

                    let norm_x = theta / (2.0 * PI);
                    let norm_y = p / (2.0 * PI);
                    let px = (norm_x * (dw - 1) as f32) as i32;
                    let py = ((1.0 - norm_y) * (dh - 1) as f32) as i32;
                    draw::dot_i(grid, px, py);
                }
            }
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 9. Gingerbreadman Map
//    x_{n+1} = 1 − y + |x|
//    y_{n+1} = x
//
//    Triangular symmetric strange attractor. Progress seeds more points;
//    time slowly rotates the view to reveal self-similar structure.
// ---------------------------------------------------------------------------

struct Gingerbreadman;

impl ProgressStyle for Gingerbreadman {
    fn name(&self) -> &str {
        "gingerbreadman"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Gingerbreadman map: x←1−y+|x|, y←x — fractal triangular symmetry, orbits seeded by progress"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let n_seeds_max = 12usize;
        let n_seeds = (ctx.eased * n_seeds_max as f32).max(1.0) as usize;
        let n_iter = 200usize;

        // Typical orbit extent ≈ ±100 for various seeds; normalize.
        let extent = 120.0_f32;

        let angle = ctx.time * 0.1;
        let cos_a = angle.cos();
        let sin_a = angle.sin();

        let cx = dw as f32 / 2.0;
        let cy = dh as f32 / 2.0;

        for si in 0..n_seeds.min(n_seeds_max) {
            // Deterministic seed positions.
            let h1 = hash(si as u32 * 17 + 3);
            let h2 = hash(si as u32 * 31 + 7);
            let mut x = (h1 % 40) as f32 - 20.0;
            let mut y = (h2 % 40) as f32 - 20.0;

            for _ in 0..n_iter {
                let xn = 1.0 - y + x.abs();
                let yn = x;
                x = xn;
                y = yn;

                // Rotate.
                let rx = x * cos_a - y * sin_a;
                let ry = x * sin_a + y * cos_a;

                let px = (cx + rx / extent * cx) as i32;
                let py = (cy + ry / extent * cy) as i32;
                draw::dot_i(grid, px, py);
            }
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 10. Duffing Oscillator Phase Portrait
//     ẋ = y
//     ẏ = −δy + αx − βx³ + γcos(ωt_sim)
//
//     δ=0.2, α=1.0, β=1.0, γ=0.3, ω=1.2 (periodic driving).
//     Plot (x, y) = (position, velocity) phase portrait.
//     Progress controls how many trajectory steps are drawn.
//     ctx.time offsets the driving phase, ctx.eased gates steps.
// ---------------------------------------------------------------------------

struct DuffingOscillator;

impl ProgressStyle for DuffingOscillator {
    fn name(&self) -> &str {
        "duffing"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Duffing oscillator phase portrait: ẏ=−δy+αx−βx³+γcos(ωt) — chaotic fractal basin boundary"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let delta: f32 = 0.2;
        let alpha: f32 = 1.0;
        let beta: f32 = 1.0;
        let gamma: f32 = 0.3;
        let omega: f32 = 1.2;
        let dt: f32 = 0.02;

        let n_total: usize = 2500;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // Phase portrait fits in roughly x∈[−1.5,1.5], y∈[−1.5,1.5].
        let x_range = 2.0_f32;
        let y_range = 2.0_f32;

        let mut x: f32 = 1.0;
        let mut y: f32 = 0.0;
        // Driving phase starts offset by ctx.time.
        let mut t_sim: f32 = ctx.time * 0.5;

        for _ in 0..n_draw.min(n_total) {
            let force = gamma * (omega * t_sim).cos();
            let ax = y;
            let ay = -delta * y + alpha * x - beta * x * x * x + force;

            x += ax * dt;
            y += ay * dt;
            t_sim += dt;

            let norm_x = (x + x_range) / (2.0 * x_range);
            let norm_y = (y + y_range) / (2.0 * y_range);
            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_y.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}

// ---------------------------------------------------------------------------
// 11. Tinkerbell Map
//     x_{n+1} = x² − y² + a·x + b·y
//     y_{n+1} = 2xy + c·x + d·y
//
//     a=0.9, b=−0.6013, c=2.0, d=0.5
//     Named for the fractal boundary of its basin of attraction.
//     Progress controls orbit length; time animates parameter a slightly.
// ---------------------------------------------------------------------------

struct TinkerbellMap;

impl ProgressStyle for TinkerbellMap {
    fn name(&self) -> &str {
        "tinkerbell"
    }
    fn theme(&self) -> &str {
        "chaos"
    }
    fn describe(&self) -> &str {
        "Tinkerbell map: a=0.9 b=−0.6013 c=2.0 d=0.5 — fractal basin boundary fairy-dust scatter"
    }
    fn render(&self, grid: &mut BrailleGrid, ctx: &BarContext) -> Result<(), DotmaxError> {
        let (dw, dh) = draw::dot_dims(grid);
        if dw == 0 || dh == 0 {
            return Ok(());
        }

        let a: f32 = 0.9 + 0.05 * (ctx.time * 0.08).sin();
        let b: f32 = -0.6013;
        let c: f32 = 2.0;
        let d: f32 = 0.5;

        let n_total = 3000usize;
        let n_draw = (ctx.eased * n_total as f32).max(1.0) as usize;

        // Known attractor extent: x ∈ [−0.7, 0.6], y ∈ [−0.7, 0.4].
        // Use ±1.5 margin to be safe.
        let x_min = -1.5_f32;
        let x_max = 1.5_f32;
        let y_min = -1.5_f32;
        let y_max = 1.5_f32;

        let mut x: f32 = -0.72;
        let mut y: f32 = -0.64;

        for _ in 0..n_draw.min(n_total) {
            let xn = x * x - y * y + a * x + b * y;
            let yn = 2.0 * x * y + c * x + d * y;
            x = xn;
            y = yn;

            // Escape detection.
            if x.abs() > 5.0 || y.abs() > 5.0 {
                break;
            }

            let norm_x = (x - x_min) / (x_max - x_min);
            let norm_y = (y - y_min) / (y_max - y_min);
            let px = (norm_x.clamp(0.0, 1.0) * (dw - 1) as f32) as i32;
            let py = ((1.0 - norm_y.clamp(0.0, 1.0)) * (dh - 1) as f32) as i32;
            draw::dot_i(grid, px, py);
        }

        Ok(())
    }
}