-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1133 lines (989 loc) · 49.3 KB
/
main.c
File metadata and controls
1133 lines (989 loc) · 49.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "raylib.h"
#include "rlgl.h"
#include <math.h>
#include <stdio.h>
#ifndef PI
#define PI 3.14159265358979323846f
#endif
#define MAX_DEGREE 7
#define PRESET_COUNT 8
#define MAX_U_STEPS 210
#define MAX_V_STEPS 120
typedef struct Complex {
float re;
float im;
} Complex;
typedef struct Preset {
const char *label;
int degree;
Complex roots[MAX_DEGREE];
} Preset;
typedef struct RootMapping {
int n;
int shiftA;
int shiftB;
Complex orderedA[MAX_DEGREE];
Complex orderedB[MAX_DEGREE];
} RootMapping;
static float Clamp01(float x) {
if (x < 0.0f) return 0.0f;
if (x > 1.0f) return 1.0f;
return x;
}
static float SmootherStep01(float x) {
float t = Clamp01(x);
return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
}
static float ComplexAbs(Complex z) {
return sqrtf(z.re * z.re + z.im * z.im);
}
static float ComplexArg(Complex z) {
return atan2f(z.im, z.re);
}
static Complex MixComplex(Complex a, Complex b, float t) {
Complex out = {
a.re + (b.re - a.re) * t,
a.im + (b.im - a.im) * t
};
return out;
}
static void SortRootsByArg(const Preset *p, Complex out[MAX_DEGREE]) {
for (int i = 0; i < p->degree; i++) out[i] = p->roots[i];
for (int i = 0; i < p->degree - 1; i++) {
for (int j = i + 1; j < p->degree; j++) {
float ai = ComplexArg(out[i]);
float aj = ComplexArg(out[j]);
if (aj < ai) {
Complex tmp = out[i];
out[i] = out[j];
out[j] = tmp;
}
}
}
}
static Complex RootFromMapping(const Complex roots[MAX_DEGREE], int degree, int shift, int idx, int n) {
int k = (idx * degree) / n;
if (k < 0) k = 0;
if (k >= degree) k = degree - 1;
int id = (k + shift) % degree;
return roots[id];
}
static int ComputeBestShift(const Complex a[MAX_DEGREE], int da, const Complex b[MAX_DEGREE], int db, int n, int shiftA) {
int bestShift = 0;
float bestCost = 1e30f;
for (int sb = 0; sb < db; sb++) {
float cost = 0.0f;
for (int i = 0; i < n; i++) {
Complex ra = RootFromMapping(a, da, shiftA, i, n);
Complex rb = RootFromMapping(b, db, sb, i, n);
float dx = ra.re - rb.re;
float dy = ra.im - rb.im;
cost += dx * dx + dy * dy;
}
if (cost < bestCost) {
bestCost = cost;
bestShift = sb;
}
}
return bestShift;
}
static RootMapping BuildRootMapping(const Preset *a, const Preset *b) {
RootMapping m = { 0 };
m.n = (a->degree > b->degree) ? a->degree : b->degree;
if (m.n < 1) m.n = 1;
SortRootsByArg(a, m.orderedA);
SortRootsByArg(b, m.orderedB);
m.shiftA = 0;
m.shiftB = ComputeBestShift(m.orderedA, a->degree, m.orderedB, b->degree, m.n, m.shiftA);
return m;
}
static Complex MixedRootMapped(const RootMapping *m, const Preset *a, const Preset *b, float mix, int idx) {
Complex ra = RootFromMapping(m->orderedA, a->degree, m->shiftA, idx, m->n);
Complex rb = RootFromMapping(m->orderedB, b->degree, m->shiftB, idx, m->n);
return MixComplex(ra, rb, mix);
}
static Complex ComplexSub(Complex a, Complex b) {
Complex out = { a.re - b.re, a.im - b.im };
return out;
}
static Complex ComplexMul(Complex a, Complex b) {
Complex out = { a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re };
return out;
}
static float MixedMaxRootMagnitude(const RootMapping *map, const Preset *a, const Preset *b, float mix) {
float maxMag = 1.0f;
for (int i = 0; i < map->n; i++) {
float mag = ComplexAbs(MixedRootMapped(map, a, b, mix, i));
if (mag > maxMag) maxMag = mag;
}
return maxMag;
}
static Complex EvalPolynomialPreset(Complex z, const Preset *p) {
Complex out = { 1.0f, 0.0f };
for (int i = 0; i < p->degree; i++) {
out = ComplexMul(out, ComplexSub(z, p->roots[i]));
}
return out;
}
static Complex EvalPolynomialHomotopy(Complex z, const Preset *a, const Preset *b, float mix) {
Complex pa = EvalPolynomialPreset(z, a);
Complex pb = EvalPolynomialPreset(z, b);
Complex out = {
(1.0f - mix) * pa.re + mix * pb.re,
(1.0f - mix) * pa.im + mix * pb.im
};
return out;
}
static float SurfaceHeightFromValue(Complex p) {
float logMag = logf(ComplexAbs(p) + 1e-6f);
return -2.25f * tanhf(logMag * 0.58f);
}
static Vector3 V3Sub(Vector3 a, Vector3 b) {
Vector3 out = { a.x - b.x, a.y - b.y, a.z - b.z };
return out;
}
static Vector3 V3Cross(Vector3 a, Vector3 b) {
Vector3 out = {
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
return out;
}
static float V3Dot(Vector3 a, Vector3 b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static Vector3 V3Normalize(Vector3 v) {
float len = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
if (len < 1e-6f) return (Vector3){ 0.0f, 1.0f, 0.0f };
return (Vector3){ v.x / len, v.y / len, v.z / len };
}
static Vector3 V3Lerp(Vector3 a, Vector3 b, float t) {
Vector3 out = {
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t
};
return out;
}
static Color ShadeColor(Color c, float k) {
float s = Clamp01(k);
Color out = {
(unsigned char)fminf(255.0f, c.r * s),
(unsigned char)fminf(255.0f, c.g * s),
(unsigned char)fminf(255.0f, c.b * s),
c.a
};
return out;
}
static Color MixColor(Color a, Color b, float t) {
float u = Clamp01(t);
Color c = {
(unsigned char)((1.0f - u) * a.r + u * b.r),
(unsigned char)((1.0f - u) * a.g + u * b.g),
(unsigned char)((1.0f - u) * a.b + u * b.b),
(unsigned char)((1.0f - u) * a.a + u * b.a)
};
return c;
}
static Color SpectralColorFromComplex(Complex pVal, float yNorm) {
float arg = ComplexArg(pVal);
float lm = logf(ComplexAbs(pVal) + 1e-6f);
float w = arg + 0.95f * lm;
float r = 0.5f + 0.5f * cosf(w + 0.0f);
float g = 0.5f + 0.5f * cosf(w + 2.0943951f);
float b = 0.5f + 0.5f * cosf(w + 4.1887902f);
float phaseBands = 0.5f + 0.5f * cosf(8.0f * arg + 2.6f * lm);
float boost = 0.78f + 0.38f * phaseBands + 0.26f * Clamp01(yNorm);
Color c = {
(unsigned char)fminf(255.0f, 255.0f * powf(r, 0.82f) * boost),
(unsigned char)fminf(255.0f, 255.0f * powf(g, 0.82f) * boost),
(unsigned char)fminf(255.0f, 255.0f * powf(b, 0.82f) * boost),
255
};
return c;
}
static void DrawDottedLine3D(Vector3 a, Vector3 b, int dots, float phase, float radius, Color c) {
float head = phase - floorf(phase);
for (int i = 0; i < dots; i++) {
float u = (float)i / (float)(dots - 1);
float d = fabsf(u - head);
if (d > 0.5f) d = 1.0f - d;
float packet = expf(-(d * d) / 0.02f);
float twinkle = 0.75f + 0.25f * sinf(2.0f * PI * (u * 2.0f + phase * 0.35f));
float alpha = Clamp01(0.10f + 0.45f * packet) * twinkle;
float rr = radius * (0.75f + 0.55f * packet);
Vector3 p = V3Lerp(a, b, u);
Color cc = c;
cc.a = (unsigned char)(c.a * alpha);
DrawSphere(p, rr, cc);
}
}
static void DrawDottedLine2D(Vector2 a, Vector2 b, int dots, float phase, float radius, Color c) {
float head = phase - floorf(phase);
for (int i = 0; i < dots; i++) {
float u = (float)i / (float)(dots - 1);
float d = fabsf(u - head);
if (d > 0.5f) d = 1.0f - d;
float packet = expf(-(d * d) / 0.03f);
float twinkle = 0.78f + 0.22f * sinf(2.0f * PI * (u * 1.8f + phase * 0.30f));
float alpha = Clamp01(0.08f + 0.40f * packet) * twinkle;
float rr = radius * (0.78f + 0.45f * packet);
Vector2 p = { a.x + (b.x - a.x) * u, a.y + (b.y - a.y) * u };
Color cc = c;
cc.a = (unsigned char)(c.a * alpha);
DrawCircleV(p, rr, cc);
}
}
static void DrawOrbitDots2D(Vector2 center, float radius, int dots, float phase, float dotRadius, Color c) {
for (int i = 0; i < dots; i++) {
float u = ((float)i / (float)dots) + phase;
u -= floorf(u);
float ang = 2.0f * PI * u;
Vector2 p = { center.x + radius * cosf(ang), center.y + radius * sinf(ang) };
float pulse = 0.65f + 0.35f * sinf(2.0f * PI * u + phase * 6.0f);
Color cc = c;
cc.a = (unsigned char)(c.a * pulse);
DrawCircleV(p, dotRadius * (0.75f + 0.45f * pulse), cc);
}
}
static void DrawOrbitDots3D(Vector3 center, float radius, int dots, float phase, float dotRadius, Color c) {
for (int i = 0; i < dots; i++) {
float u = ((float)i / (float)dots) + phase;
u -= floorf(u);
float ang = 2.0f * PI * u;
Vector3 p = { center.x + radius * cosf(ang), center.y, center.z + radius * sinf(ang) };
float pulse = 0.65f + 0.35f * sinf(2.0f * PI * u + phase * 6.0f);
Color cc = c;
cc.a = (unsigned char)(c.a * pulse);
DrawSphere(p, dotRadius * (0.75f + 0.45f * pulse), cc);
}
}
static void DrawZeroContourLines(
int uSteps,
int vSteps,
Vector3 mesh[MAX_U_STEPS][MAX_V_STEPS],
float vals[MAX_U_STEPS][MAX_V_STEPS],
float yPlane,
Color col,
float thickness
) {
for (int u = 0; u < uSteps - 1; u++) {
for (int v = 0; v < vSteps - 1; v++) {
Vector3 p00 = mesh[u][v];
Vector3 p10 = mesh[u + 1][v];
Vector3 p11 = mesh[u + 1][v + 1];
Vector3 p01 = mesh[u][v + 1];
float f00 = vals[u][v];
float f10 = vals[u + 1][v];
float f11 = vals[u + 1][v + 1];
float f01 = vals[u][v + 1];
Vector3 pts[4];
int pc = 0;
if ((f00 > 0.0f) != (f10 > 0.0f)) {
float t = fabsf(f00) / (fabsf(f00) + fabsf(f10) + 1e-12f);
pts[pc++] = (Vector3){ p00.x + (p10.x - p00.x) * t, yPlane, p00.z + (p10.z - p00.z) * t };
}
if ((f10 > 0.0f) != (f11 > 0.0f)) {
float t = fabsf(f10) / (fabsf(f10) + fabsf(f11) + 1e-12f);
pts[pc++] = (Vector3){ p10.x + (p11.x - p10.x) * t, yPlane, p10.z + (p11.z - p10.z) * t };
}
if ((f11 > 0.0f) != (f01 > 0.0f)) {
float t = fabsf(f11) / (fabsf(f11) + fabsf(f01) + 1e-12f);
pts[pc++] = (Vector3){ p11.x + (p01.x - p11.x) * t, yPlane, p11.z + (p01.z - p11.z) * t };
}
if ((f01 > 0.0f) != (f00 > 0.0f)) {
float t = fabsf(f01) / (fabsf(f01) + fabsf(f00) + 1e-12f);
pts[pc++] = (Vector3){ p01.x + (p00.x - p01.x) * t, yPlane, p01.z + (p00.z - p01.z) * t };
}
if (pc == 2) {
DrawLine3D((Vector3){ pts[0].x, pts[0].y - thickness, pts[0].z }, (Vector3){ pts[1].x, pts[1].y - thickness, pts[1].z }, (Color){ col.r, col.g, col.b, (unsigned char)(col.a * 0.55f) });
DrawLine3D(pts[0], pts[1], col);
DrawLine3D((Vector3){ pts[0].x, pts[0].y + thickness, pts[0].z }, (Vector3){ pts[1].x, pts[1].y + thickness, pts[1].z }, (Color){ col.r, col.g, col.b, (unsigned char)(col.a * 0.55f) });
} else if (pc == 4) {
DrawLine3D((Vector3){ pts[0].x, pts[0].y - thickness, pts[0].z }, (Vector3){ pts[1].x, pts[1].y - thickness, pts[1].z }, (Color){ col.r, col.g, col.b, (unsigned char)(col.a * 0.55f) });
DrawLine3D(pts[0], pts[1], col);
DrawLine3D((Vector3){ pts[0].x, pts[0].y + thickness, pts[0].z }, (Vector3){ pts[1].x, pts[1].y + thickness, pts[1].z }, (Color){ col.r, col.g, col.b, (unsigned char)(col.a * 0.55f) });
DrawLine3D((Vector3){ pts[2].x, pts[2].y - thickness, pts[2].z }, (Vector3){ pts[3].x, pts[3].y - thickness, pts[3].z }, (Color){ col.r, col.g, col.b, (unsigned char)(col.a * 0.55f) });
DrawLine3D(pts[2], pts[3], col);
DrawLine3D((Vector3){ pts[2].x, pts[2].y + thickness, pts[2].z }, (Vector3){ pts[3].x, pts[3].y + thickness, pts[3].z }, (Color){ col.r, col.g, col.b, (unsigned char)(col.a * 0.55f) });
}
}
}
}
static Camera3D BuildSurfaceCamera(float cameraDistance, float camYaw, float camPitch, float camZoom) {
Camera3D cam = { 0 };
float camY = camPitch;
if (camY < -1.2f) camY = -1.2f;
if (camY > 1.2f) camY = 1.2f;
float dist = cameraDistance * camZoom;
cam.position = (Vector3){
dist * cosf(camY) * cosf(camYaw),
dist * sinf(camY),
dist * cosf(camY) * sinf(camYaw)
};
cam.target = (Vector3){ 0.0f, 0.0f, 0.0f };
cam.up = (Vector3){ 0.0f, 1.0f, 0.0f };
cam.fovy = 45.0f;
cam.projection = CAMERA_PERSPECTIVE;
return cam;
}
static void DrawHeightExplanationPanel(Rectangle panel, float mix, Complex z, Complex pA, Complex pB, Complex pT, float yHeight, int morphing) {
DrawRectangleRounded(panel, 0.05f, 10, (Color){ 7, 13, 30, 208 });
DrawRectangleRoundedLines(panel, 0.05f, 10, (Color){ 120, 182, 255, 120 });
Rectangle left = { panel.x + 12, panel.y + 34, panel.width * 0.47f, panel.height - 46 };
Rectangle right = { panel.x + panel.width * 0.50f, panel.y + 34, panel.width * 0.48f - 10, panel.height - 46 };
DrawRectangleRounded(left, 0.06f, 8, (Color){ 8, 18, 38, 175 });
DrawRectangleRounded(right, 0.06f, 8, (Color){ 8, 18, 38, 175 });
Vector2 c = { left.x + left.width * 0.5f, left.y + left.height * 0.5f };
float maxMag = fmaxf(1.0f, fmaxf(ComplexAbs(pA), fmaxf(ComplexAbs(pB), ComplexAbs(pT))));
float scale = fminf(left.width, left.height) * 0.40f / maxMag;
DrawLine((int)left.x + 8, (int)c.y, (int)(left.x + left.width - 8), (int)c.y, (Color){ 125, 172, 230, 95 });
DrawLine((int)c.x, (int)left.y + 8, (int)c.x, (int)(left.y + left.height - 8), (Color){ 125, 172, 230, 95 });
DrawText("Value plane", (int)left.x + 8, (int)left.y + 8, 14, (Color){ 180, 218, 255, 230 });
Vector2 vA = { c.x + pA.re * scale, c.y - pA.im * scale };
Vector2 vB = { c.x + pB.re * scale, c.y - pB.im * scale };
Vector2 vT = { c.x + pT.re * scale, c.y - pT.im * scale };
if (morphing) {
DrawLineEx(c, vA, 2.0f, (Color){ 255, 168, 148, 190 });
DrawLineEx(c, vB, 2.0f, (Color){ 140, 222, 255, 190 });
}
DrawLineEx(c, vT, 3.0f, (Color){ 255, 245, 210, 235 });
if (morphing) {
DrawCircleV(vA, 3.8f, (Color){ 255, 168, 148, 220 });
DrawCircleV(vB, 3.8f, (Color){ 140, 222, 255, 220 });
}
DrawCircleV(vT, 5.0f, (Color){ 255, 245, 210, 245 });
if (morphing) {
DrawText("Pa", (int)vA.x + 6, (int)vA.y - 2, 13, (Color){ 255, 172, 156, 220 });
DrawText("Pb", (int)vB.x + 6, (int)vB.y - 2, 13, (Color){ 145, 228, 255, 220 });
DrawText("Pt", (int)vT.x + 6, (int)vT.y - 2, 13, (Color){ 255, 245, 210, 240 });
} else {
DrawText("P", (int)vT.x + 6, (int)vT.y - 2, 13, (Color){ 255, 245, 210, 240 });
}
float absPt = ComplexAbs(pT);
float logMag = logf(absPt + 1e-6f);
float nLog = Clamp01((logMag + 5.0f) / 10.0f);
float nY = Clamp01((yHeight + 2.25f) / 4.5f);
if (morphing) {
DrawText("Pt = (1-t)Pa + tPb", (int)right.x + 8, (int)right.y + 8, 15, (Color){ 202, 229, 255, 235 });
DrawText("height = -2.25*tanh(0.58*ln(|Pt|+1e-6))", (int)right.x + 8, (int)right.y + 27, 14, (Color){ 186, 210, 245, 225 });
} else {
DrawText("P(z) at hovered point", (int)right.x + 8, (int)right.y + 8, 15, (Color){ 202, 229, 255, 235 });
DrawText("height = -2.25*tanh(0.58*ln(|P(z)|+1e-6))", (int)right.x + 8, (int)right.y + 27, 14, (Color){ 186, 210, 245, 225 });
}
DrawRectangle((int)right.x + 8, (int)right.y + 52, (int)right.width - 16, 11, (Color){ 28, 48, 80, 220 });
DrawRectangle((int)right.x + 8, (int)right.y + 52, (int)((right.width - 16) * nLog), 11, (Color){ 130, 210, 255, 225 });
DrawText(morphing ? "ln(|Pt|)" : "ln(|P(z)|)", (int)right.x + 8, (int)right.y + 66, 13, (Color){ 175, 215, 255, 220 });
DrawRectangle((int)right.x + 8, (int)right.y + 87, (int)right.width - 16, 11, (Color){ 28, 48, 80, 220 });
DrawRectangle((int)right.x + 8, (int)right.y + 87, (int)((right.width - 16) * nY), 11, (Color){ 255, 210, 140, 225 });
DrawText("surface height", (int)right.x + 8, (int)right.y + 101, 13, (Color){ 255, 222, 166, 220 });
char info1[128];
char info2[160];
if (morphing) {
snprintf(info1, sizeof(info1), "z = %.3f %+.3fi t = %.3f", z.re, z.im, mix);
snprintf(info2, sizeof(info2), "|Pt|=%.5f ln|Pt|=%.5f y=%.5f", absPt, logMag, yHeight);
} else {
snprintf(info1, sizeof(info1), "z = %.3f %+.3fi", z.re, z.im);
snprintf(info2, sizeof(info2), "|P(z)|=%.5f ln|P(z)|=%.5f y=%.5f", absPt, logMag, yHeight);
}
DrawText(info1, (int)right.x + 8, (int)right.y + 124, 14, (Color){ 234, 240, 250, 230 });
DrawText(info2, (int)right.x + 8, (int)right.y + 142, 14, (Color){ 234, 240, 250, 230 });
}
static Color RootColor(Complex r, float t, float extraHue) {
float mag = ComplexAbs(r);
float arg = ComplexArg(r);
float hue = (arg + PI) * (180.0f / PI) + extraHue + t * 8.0f;
float sat = 0.58f + 0.30f * Clamp01(mag / 2.6f);
float val = 0.72f + 0.24f * (0.5f + 0.5f * sinf(t * 0.9f + arg));
return ColorFromHSV(hue, sat, val);
}
static void DrawBackdrop(int w, int h, int topBarH, float t) {
DrawRectangleGradientV(0, topBarH, w, h - topBarH, (Color){ 4, 9, 24, 255 }, (Color){ 1, 3, 12, 255 });
for (int i = 0; i < 12; i++) {
float phase = t * (0.08f + 0.015f * i) + 0.6f * i;
float x = (float)w * (0.07f + 0.07f * i) + 55.0f * sinf(phase);
float y = topBarH + (float)(h - topBarH) * (0.12f + 0.06f * i) + 35.0f * cosf(phase * 1.1f);
float rad = 70.0f + 35.0f * (0.5f + 0.5f * sinf(phase * 0.7f));
Color c = { (unsigned char)(18 + i * 5), (unsigned char)(28 + i * 6), (unsigned char)(46 + i * 7), 24 };
DrawCircleGradient((int)x, (int)y, rad, c, BLANK);
}
}
static void DrawTopBar(int w, int page, int paused) {
Rectangle bar = { 0, 0, (float)w, 62.0f };
DrawRectangleRec(bar, (Color){ 3, 8, 18, 238 });
DrawLine(0, 61, w, 61, (Color){ 68, 140, 230, 120 });
Rectangle tab1 = { 16, 14, 110, 34 };
Rectangle tab2 = { 138, 14, 110, 34 };
Color active = (Color){ 44, 132, 232, 230 };
Color idle = (Color){ 22, 41, 70, 220 };
DrawRectangleRounded(tab1, 0.25f, 8, page == 0 ? active : idle);
DrawRectangleRounded(tab2, 0.25f, 8, page == 1 ? active : idle);
DrawText("[1] Roots", 30, 23, 16, RAYWHITE);
DrawText("[2] Empty", 150, 23, 16, RAYWHITE);
DrawText("P: preset Space: pause/resume Q: quit", w - 420, 23, 16, (Color){ 180, 223, 255, 220 });
if (paused) DrawText("PAUSED", w / 2 - 34, 23, 16, (Color){ 255, 220, 140, 235 });
}
static int DrawMiniComplexPlane(Rectangle panel, const Preset *a, const Preset *b, float mix, float t, int existingHover, int probeActive, Complex probeZ) {
(void)t;
RootMapping map = BuildRootMapping(a, b);
int n = map.n;
Vector2 mouse = GetMousePosition();
DrawRectangleRounded(panel, 0.06f, 10, (Color){ 6, 13, 30, 205 });
DrawRectangleRoundedLines(panel, 0.06f, 10, (Color){ 120, 185, 255, 120 });
Rectangle inner = { panel.x + 14, panel.y + 26, panel.width - 28, panel.height - 40 };
Vector2 c = { inner.x + inner.width * 0.5f, inner.y + inner.height * 0.5f };
float maxMag = 1.5f;
for (int i = 0; i < n; i++) {
Complex r = MixedRootMapped(&map, a, b, mix, i);
float m = ComplexAbs(r);
if (m > maxMag) maxMag = m;
}
float scale = fminf(inner.width, inner.height) * 0.42f / maxMag;
Color grid = (Color){ 88, 130, 198, 80 };
for (int k = -4; k <= 4; k++) {
DrawLine((int)(c.x + k * scale * 0.5f), (int)inner.y, (int)(c.x + k * scale * 0.5f), (int)(inner.y + inner.height), grid);
DrawLine((int)inner.x, (int)(c.y + k * scale * 0.5f), (int)(inner.x + inner.width), (int)(c.y + k * scale * 0.5f), grid);
}
DrawLineEx((Vector2){ inner.x, c.y }, (Vector2){ inner.x + inner.width, c.y }, 2.0f, (Color){ 170, 214, 255, 180 });
DrawLineEx((Vector2){ c.x, inner.y }, (Vector2){ c.x, inner.y + inner.height }, 2.0f, (Color){ 170, 214, 255, 180 });
if (probeActive) {
Vector2 q = { c.x + probeZ.re * scale, c.y - probeZ.im * scale };
DrawDottedLine2D((Vector2){ inner.x, q.y }, (Vector2){ inner.x + inner.width, q.y }, 34, 0.11f * t, 1.9f, (Color){ 255, 232, 176, 170 });
DrawDottedLine2D((Vector2){ q.x, inner.y }, (Vector2){ q.x, inner.y + inner.height }, 34, 0.15f * t + 0.23f, 1.9f, (Color){ 172, 218, 255, 170 });
DrawCircleV(q, 4.8f, (Color){ 255, 245, 210, 165 });
}
int hovered = -1;
float hoveredDist = 1e9f;
for (int i = 0; i < n; i++) {
Complex r = MixedRootMapped(&map, a, b, mix, i);
Vector2 p = { c.x + r.re * scale, c.y - r.im * scale };
float dx = p.x - mouse.x;
float dy = p.y - mouse.y;
float d = sqrtf(dx * dx + dy * dy);
if (CheckCollisionPointRec(mouse, panel) && d < 11.0f && d < hoveredDist) {
hovered = i;
hoveredDist = d;
}
}
if (hovered < 0 && CheckCollisionPointRec(mouse, panel)) hovered = existingHover;
for (int i = 0; i < n; i++) {
Complex r = MixedRootMapped(&map, a, b, mix, i);
Vector2 p = { c.x + r.re * scale, c.y - r.im * scale };
Color col = RootColor(r, t, i * 34.0f);
float rad = (i == hovered) ? 8.5f : 5.5f;
DrawCircleV(p, rad + 4.0f, (Color){ col.r, col.g, col.b, (unsigned char)(i == hovered ? 120 : 55) });
DrawCircleV(p, rad, col);
DrawCircleLines((int)p.x, (int)p.y, rad + 1.0f, (Color){ 255, 255, 255, 200 });
if (i == hovered) {
float ph = 0.12f * t;
DrawOrbitDots2D(p, rad + 5.8f, 20, ph, 1.7f, (Color){ 255, 240, 190, 210 });
DrawOrbitDots2D(p, rad + 9.2f, 26, -0.8f * ph + 0.13f, 1.4f, (Color){ 165, 228, 255, 180 });
for (int k = 0; k < 8; k++) {
float ang = 2.0f * PI * ((float)k / 8.0f) + 0.5f * t;
Vector2 q = { p.x + (rad + 12.0f) * cosf(ang), p.y + (rad + 12.0f) * sinf(ang) };
DrawLineEx(p, q, 1.2f, (Color){ 255, 235, 190, 85 });
}
}
}
DrawText("Complex Plane", (int)panel.x + 14, (int)panel.y + 7, 18, (Color){ 190, 220, 255, 235 });
if (hovered >= 0) {
Complex r = MixedRootMapped(&map, a, b, mix, hovered);
char txt[96];
snprintf(txt, sizeof(txt), "r%d = %.3f %+.3fi", hovered + 1, r.re, r.im);
DrawText(txt, (int)panel.x + 14, (int)(panel.y + panel.height - 20), 16, (Color){ 245, 226, 170, 240 });
}
return hovered;
}
static int MiniPanelHoverToComplex(Rectangle panel, Vector2 mouse, const Preset *a, const Preset *b, float mix, Complex *outZ) {
RootMapping map = BuildRootMapping(a, b);
int n = map.n;
Rectangle inner = { panel.x + 14, panel.y + 26, panel.width - 28, panel.height - 40 };
if (!CheckCollisionPointRec(mouse, inner)) return 0;
float maxMag = 1.5f;
for (int i = 0; i < n; i++) {
Complex r = MixedRootMapped(&map, a, b, mix, i);
float m = ComplexAbs(r);
if (m > maxMag) maxMag = m;
}
float scale = fminf(inner.width, inner.height) * 0.42f / maxMag;
Vector2 c = { inner.x + inner.width * 0.5f, inner.y + inner.height * 0.5f };
outZ->re = (mouse.x - c.x) / scale;
outZ->im = -(mouse.y - c.y) / scale;
return 1;
}
static int MiniPanelHoveredRootIndex(Rectangle panel, Vector2 mouse, const Preset *a, const Preset *b, float mix) {
RootMapping map = BuildRootMapping(a, b);
int n = map.n;
Rectangle inner = { panel.x + 14, panel.y + 26, panel.width - 28, panel.height - 40 };
Vector2 c = { inner.x + inner.width * 0.5f, inner.y + inner.height * 0.5f };
float maxMag = 1.5f;
for (int i = 0; i < n; i++) {
Complex r = MixedRootMapped(&map, a, b, mix, i);
float m = ComplexAbs(r);
if (m > maxMag) maxMag = m;
}
float scale = fminf(inner.width, inner.height) * 0.42f / maxMag;
int hovered = -1;
float hoveredDist = 1e9f;
for (int i = 0; i < n; i++) {
Complex r = MixedRootMapped(&map, a, b, mix, i);
Vector2 p = { c.x + r.re * scale, c.y - r.im * scale };
float dx = p.x - mouse.x;
float dy = p.y - mouse.y;
float d = sqrtf(dx * dx + dy * dy);
if (CheckCollisionPointRec(mouse, panel) && d < 11.0f && d < hoveredDist) {
hovered = i;
hoveredDist = d;
}
}
return hovered;
}
static void DrawRootSurface(Rectangle area, const Preset *a, const Preset *b, float mix, float t, int highlightedRoot, float camYaw, float camPitch, float camZoom, int probeActive, Complex probeZ) {
RootMapping map = BuildRootMapping(a, b);
int n = map.n;
const int uSteps = 190;
const int vSteps = 108;
float maxRootMag = MixedMaxRootMagnitude(&map, a, b, mix);
float domain = 1.25f + 1.75f * maxRootMag;
float cameraDistance = 9.4f + 0.85f * maxRootMag;
float mirrorY = -2.45f;
Vector3 mesh[MAX_U_STEPS][MAX_V_STEPS] = { 0 };
Color colors[MAX_U_STEPS][MAX_V_STEPS] = { 0 };
float reVals[MAX_U_STEPS][MAX_V_STEPS] = { 0 };
float imVals[MAX_U_STEPS][MAX_V_STEPS] = { 0 };
BeginScissorMode((int)area.x, (int)area.y, (int)area.width, (int)area.height);
Camera3D cam = BuildSurfaceCamera(cameraDistance, camYaw, camPitch, camZoom);
BeginMode3D(cam);
for (int u = 0; u < uSteps; u++) {
float fu = (float)u / (float)(uSteps - 1);
float xr = -domain + 2.0f * domain * fu;
for (int v = 0; v < vSteps; v++) {
float fv = (float)v / (float)(vSteps - 1);
float yi = -domain + 2.0f * domain * fv;
Complex z = { xr, yi };
Complex pVal = EvalPolynomialHomotopy(z, a, b, mix);
float y = SurfaceHeightFromValue(pVal);
mesh[u][v] = (Vector3){ xr, y, yi };
reVals[u][v] = pVal.re;
imVals[u][v] = pVal.im;
float yNorm = (y + 2.25f) / 4.5f;
Color cMath = SpectralColorFromComplex(pVal, yNorm);
if (highlightedRoot >= 0) {
Complex hr = MixedRootMapped(&map, a, b, mix, highlightedRoot);
float dx = xr - hr.re;
float dz = yi - hr.im;
float d2 = dx * dx + dz * dz;
float glow = expf(-d2 / (0.075f * domain * domain));
cMath = MixColor(cMath, (Color){ 255, 246, 214, 255 }, 0.45f * glow);
}
colors[u][v] = cMath;
}
}
rlDisableBackfaceCulling();
DrawPlane((Vector3){ 0.0f, mirrorY, 0.0f }, (Vector2){ domain * 2.5f, domain * 2.5f }, (Color){ 10, 20, 36, 120 });
rlSetTexture(0);
rlBegin(RL_TRIANGLES);
Vector3 lightA = V3Normalize((Vector3){ 0.35f, 0.9f, 0.2f });
Vector3 lightB = V3Normalize((Vector3){ -0.28f, 0.95f, -0.10f });
Color skyTint = (Color){ 185, 230, 255, 255 };
Color rimTint = (Color){ 255, 182, 228, 255 };
for (int u = 0; u < uSteps - 1; u++) {
int u2 = u + 1;
for (int v = 0; v < vSteps - 1; v++) {
int v2 = v + 1;
Vector3 a0 = mesh[u][v];
Vector3 b0 = mesh[u2][v];
Vector3 c0 = mesh[u2][v2];
Vector3 d0 = mesh[u][v2];
Color ca = colors[u][v];
Color cb = colors[u2][v];
Color cc = colors[u2][v2];
Color cd = colors[u][v2];
Vector3 n1 = V3Normalize(V3Cross(V3Sub(b0, a0), V3Sub(c0, a0)));
Vector3 n2 = V3Normalize(V3Cross(V3Sub(c0, a0), V3Sub(d0, a0)));
Vector3 ctr1 = { (a0.x + b0.x + c0.x) / 3.0f, (a0.y + b0.y + c0.y) / 3.0f, (a0.z + b0.z + c0.z) / 3.0f };
Vector3 ctr2 = { (a0.x + c0.x + d0.x) / 3.0f, (a0.y + c0.y + d0.y) / 3.0f, (a0.z + c0.z + d0.z) / 3.0f };
Vector3 vdir1 = V3Normalize((Vector3){ cam.position.x - ctr1.x, cam.position.y - ctr1.y, cam.position.z - ctr1.z });
Vector3 vdir2 = V3Normalize((Vector3){ cam.position.x - ctr2.x, cam.position.y - ctr2.y, cam.position.z - ctr2.z });
Vector3 hA1 = V3Normalize((Vector3){ lightA.x + vdir1.x, lightA.y + vdir1.y, lightA.z + vdir1.z });
Vector3 hA2 = V3Normalize((Vector3){ lightA.x + vdir2.x, lightA.y + vdir2.y, lightA.z + vdir2.z });
Vector3 hB1 = V3Normalize((Vector3){ lightB.x + vdir1.x, lightB.y + vdir1.y, lightB.z + vdir1.z });
Vector3 hB2 = V3Normalize((Vector3){ lightB.x + vdir2.x, lightB.y + vdir2.y, lightB.z + vdir2.z });
float dA1 = fmaxf(0.0f, V3Dot(n1, lightA));
float dA2 = fmaxf(0.0f, V3Dot(n2, lightA));
float dB1 = fmaxf(0.0f, V3Dot(n1, lightB));
float dB2 = fmaxf(0.0f, V3Dot(n2, lightB));
float sA1 = powf(fmaxf(0.0f, V3Dot(n1, hA1)), 38.0f);
float sA2 = powf(fmaxf(0.0f, V3Dot(n2, hA2)), 38.0f);
float sB1 = powf(fmaxf(0.0f, V3Dot(n1, hB1)), 26.0f);
float sB2 = powf(fmaxf(0.0f, V3Dot(n2, hB2)), 26.0f);
float view1 = fmaxf(0.0f, V3Dot(n1, vdir1));
float view2 = fmaxf(0.0f, V3Dot(n2, vdir2));
float fres1 = powf(1.0f - view1, 2.3f);
float fres2 = powf(1.0f - view2, 2.3f);
float sky1 = powf(fmaxf(0.0f, n1.y), 1.1f);
float sky2 = powf(fmaxf(0.0f, n2.y), 1.1f);
float l1 = 0.66f + 0.45f * dA1 + 0.28f * dB1 + 0.78f * sA1 + 0.36f * sB1;
float l2 = 0.66f + 0.45f * dA2 + 0.28f * dB2 + 0.78f * sA2 + 0.36f * sB2;
Color c1 = MixColor(ShadeColor(ca, l1), skyTint, 0.26f * sky1 + 0.25f * fres1);
Color c2 = MixColor(ShadeColor(cb, l1), skyTint, 0.26f * sky1 + 0.25f * fres1);
Color c3 = MixColor(ShadeColor(cc, l1), rimTint, 0.18f * fres1);
Color c4 = MixColor(ShadeColor(cd, l2), skyTint, 0.26f * sky2 + 0.25f * fres2);
Color c5 = MixColor(ShadeColor(cc, l2), rimTint, 0.18f * fres2);
Color c6 = MixColor(ShadeColor(ca, l2), skyTint, 0.26f * sky2 + 0.25f * fres2);
rlColor4ub(c1.r, c1.g, c1.b, 255); rlVertex3f(a0.x, a0.y, a0.z);
rlColor4ub(c2.r, c2.g, c2.b, 255); rlVertex3f(b0.x, b0.y, b0.z);
rlColor4ub(c3.r, c3.g, c3.b, 255); rlVertex3f(c0.x, c0.y, c0.z);
rlColor4ub(c6.r, c6.g, c6.b, 255); rlVertex3f(a0.x, a0.y, a0.z);
rlColor4ub(c5.r, c5.g, c5.b, 255); rlVertex3f(c0.x, c0.y, c0.z);
rlColor4ub(c4.r, c4.g, c4.b, 255); rlVertex3f(d0.x, d0.y, d0.z);
}
}
rlEnd();
rlBegin(RL_TRIANGLES);
for (int u = 0; u < uSteps - 1; u++) {
int u2 = u + 1;
for (int v = 0; v < vSteps - 1; v++) {
int v2 = v + 1;
Vector3 a0 = mesh[u][v];
Vector3 b0 = mesh[u2][v];
Vector3 c0 = mesh[u2][v2];
Vector3 d0 = mesh[u][v2];
a0.y = 2.0f * mirrorY - a0.y;
b0.y = 2.0f * mirrorY - b0.y;
c0.y = 2.0f * mirrorY - c0.y;
d0.y = 2.0f * mirrorY - d0.y;
Color baseA = MixColor(colors[u][v], (Color){ 60, 120, 220, 255 }, 0.35f);
Color baseB = MixColor(colors[u2][v], (Color){ 60, 120, 220, 255 }, 0.35f);
Color baseC = MixColor(colors[u2][v2], (Color){ 60, 120, 220, 255 }, 0.35f);
Color baseD = MixColor(colors[u][v2], (Color){ 60, 120, 220, 255 }, 0.35f);
Vector3 n1 = V3Normalize(V3Cross(V3Sub(b0, a0), V3Sub(c0, a0)));
Vector3 n2 = V3Normalize(V3Cross(V3Sub(c0, a0), V3Sub(d0, a0)));
float l1 = 0.30f + 0.34f * fmaxf(0.0f, V3Dot(n1, lightA));
float l2 = 0.30f + 0.34f * fmaxf(0.0f, V3Dot(n2, lightA));
Color c1 = ShadeColor(baseA, l1); c1.a = 78;
Color c2 = ShadeColor(baseB, l1); c2.a = 78;
Color c3 = ShadeColor(baseC, l1); c3.a = 78;
Color c4 = ShadeColor(baseD, l2); c4.a = 78;
Color c5 = ShadeColor(baseC, l2); c5.a = 78;
Color c6 = ShadeColor(baseA, l2); c6.a = 78;
rlColor4ub(c1.r, c1.g, c1.b, c1.a); rlVertex3f(a0.x, a0.y, a0.z);
rlColor4ub(c2.r, c2.g, c2.b, c2.a); rlVertex3f(b0.x, b0.y, b0.z);
rlColor4ub(c3.r, c3.g, c3.b, c3.a); rlVertex3f(c0.x, c0.y, c0.z);
rlColor4ub(c6.r, c6.g, c6.b, c6.a); rlVertex3f(a0.x, a0.y, a0.z);
rlColor4ub(c5.r, c5.g, c5.b, c5.a); rlVertex3f(c0.x, c0.y, c0.z);
rlColor4ub(c4.r, c4.g, c4.b, c4.a); rlVertex3f(d0.x, d0.y, d0.z);
}
}
rlEnd();
rlEnableBackfaceCulling();
BeginBlendMode(BLEND_ADDITIVE);
DrawZeroContourLines(uSteps, vSteps, mesh, reVals, mirrorY + 0.03f, (Color){ 145, 232, 255, 220 }, 0.008f);
DrawZeroContourLines(uSteps, vSteps, mesh, imVals, mirrorY + 0.03f, (Color){ 255, 220, 150, 220 }, 0.008f);
DrawZeroContourLines(uSteps, vSteps, mesh, reVals, mirrorY + 0.03f, (Color){ 185, 242, 255, 110 }, 0.016f);
DrawZeroContourLines(uSteps, vSteps, mesh, imVals, mirrorY + 0.03f, (Color){ 255, 236, 185, 110 }, 0.016f);
EndBlendMode();
BeginBlendMode(BLEND_ADDITIVE);
for (int u = 0; u < uSteps; u += 7) {
float up = 0.5f + 0.5f * sinf(t * 2.4f + 0.08f * u);
for (int v = 0; v < vSteps - 1; v++) {
Vector3 p0 = mesh[u][v];
Vector3 p1 = mesh[u][v + 1];
float vp = 0.5f + 0.5f * sinf(t * 1.8f + 0.13f * v + 0.02f * u);
Color base = colors[u][v];
Color lc = MixColor(base, (Color){ 255, 240, 212, 255 }, 0.28f + 0.22f * up);
lc = MixColor(lc, (Color){ 140, 235, 255, 255 }, 0.18f * vp);
lc.a = (unsigned char)(95 + 110 * up * vp);
DrawLine3D(p0, p1, lc);
}
}
for (int v = 0; v < vSteps; v += 9) {
float vp = 0.5f + 0.5f * sinf(t * 2.1f + 0.10f * v);
for (int u = 0; u < uSteps - 1; u++) {
Vector3 p0 = mesh[u][v];
Vector3 p1 = mesh[u + 1][v];
float up = 0.5f + 0.5f * sinf(t * 1.5f + 0.09f * u + 0.03f * v);
Color base = colors[u][v];
Color lc = MixColor(base, (Color){ 255, 170, 230, 255 }, 0.22f + 0.20f * vp);
lc = MixColor(lc, (Color){ 190, 255, 236, 255 }, 0.12f * up);
lc.a = (unsigned char)(75 + 95 * up * vp);
DrawLine3D(p0, p1, lc);
}
}
EndBlendMode();
BeginBlendMode(BLEND_ADDITIVE);
for (int r = 0; r < n; r++) {
Complex rr = MixedRootMapped(&map, a, b, mix, r);
Color rc = RootColor(rr, t, r * 20.0f);
if (r == highlightedRoot) rc = (Color){ 255, 244, 210, 255 };
Color beam = (Color){ rc.r, rc.g, rc.b, (unsigned char)(r == highlightedRoot ? 245 : 135) };
DrawLine3D((Vector3){ rr.re, -2.3f, rr.im }, (Vector3){ rr.re, mirrorY + 0.03f, rr.im }, beam);
if (r == highlightedRoot) {
float offs = 0.010f;
DrawLine3D((Vector3){ rr.re + offs, -2.3f, rr.im }, (Vector3){ rr.re + offs, mirrorY + 0.03f, rr.im }, (Color){ beam.r, beam.g, beam.b, 140 });
DrawLine3D((Vector3){ rr.re - offs, -2.3f, rr.im }, (Vector3){ rr.re - offs, mirrorY + 0.03f, rr.im }, (Color){ beam.r, beam.g, beam.b, 140 });
DrawLine3D((Vector3){ rr.re, -2.3f, rr.im + offs }, (Vector3){ rr.re, mirrorY + 0.03f, rr.im + offs }, (Color){ beam.r, beam.g, beam.b, 140 });
DrawLine3D((Vector3){ rr.re, -2.3f, rr.im - offs }, (Vector3){ rr.re, mirrorY + 0.03f, rr.im - offs }, (Color){ beam.r, beam.g, beam.b, 140 });
}
DrawSphere((Vector3){ rr.re, mirrorY + 0.03f, rr.im }, (r == highlightedRoot) ? 0.10f : 0.055f, (Color){ rc.r, rc.g, rc.b, 180 });
}
EndBlendMode();
if (highlightedRoot >= 0) {
Complex hr = MixedRootMapped(&map, a, b, mix, highlightedRoot);
float pulse = 0.5f + 0.5f * sinf(2.8f * t);
Color warm = (Color){ 255, 245, 210, (unsigned char)(170 + 70 * pulse) };
Color cool = (Color){ 165, 228, 255, (unsigned char)(145 + 65 * (1.0f - pulse)) };
for (int k = 0; k < 64; k++) {
float a0 = 2.0f * PI * ((float)k / 64.0f);
float a1 = 2.0f * PI * ((float)(k + 1) / 64.0f);
float rad = 0.12f * domain;
Vector3 p0 = { hr.re + rad * cosf(a0), -2.22f, hr.im + rad * sinf(a0) };
Vector3 p1 = { hr.re + rad * cosf(a1), -2.22f, hr.im + rad * sinf(a1) };
DrawLine3D(p0, p1, warm);
}
DrawOrbitDots3D((Vector3){ hr.re, mirrorY + 0.03f, hr.im }, 0.12f * domain, 34, 0.11f * t, 0.016f, warm);
DrawOrbitDots3D((Vector3){ hr.re, mirrorY + 0.40f, hr.im }, 0.07f * domain, 24, -0.14f * t + 0.2f, 0.014f, cool);
DrawOrbitDots3D((Vector3){ hr.re, mirrorY + 0.85f, hr.im }, 0.05f * domain, 20, 0.09f * t + 0.45f, 0.012f, warm);
DrawDottedLine3D((Vector3){ hr.re, -2.22f, hr.im }, (Vector3){ hr.re, mirrorY + 0.03f, hr.im }, 30, 0.1f * t, 0.014f, cool);
{
float topY = mirrorY + 0.03f;
float tr = 0.16f + 0.05f * pulse;
Vector3 top = { hr.re, topY, hr.im };
BeginBlendMode(BLEND_ADDITIVE);
DrawSphere(top, 0.055f + 0.02f * pulse, (Color){ 255, 247, 220, 205 });
DrawOrbitDots3D(top, tr, 28, 0.16f * t, 0.013f, (Color){ 255, 240, 198, 220 });
DrawOrbitDots3D(top, tr * 1.35f, 34, -0.11f * t + 0.2f, 0.011f, (Color){ 160, 225, 255, 190 });
for (int k = 0; k < 56; k++) {
float a0 = 2.0f * PI * ((float)k / 56.0f);
float a1 = 2.0f * PI * ((float)(k + 1) / 56.0f);
Vector3 p0 = { hr.re + tr * cosf(a0), topY, hr.im + tr * sinf(a0) };
Vector3 p1 = { hr.re + tr * cosf(a1), topY, hr.im + tr * sinf(a1) };
DrawLine3D(p0, p1, (Color){ 255, 244, 214, (unsigned char)(140 + 70 * pulse) });
Vector3 q0 = { hr.re + tr * 0.85f * cosf(a0), topY + tr * 0.40f * sinf(a0), hr.im };
Vector3 q1 = { hr.re + tr * 0.85f * cosf(a1), topY + tr * 0.40f * sinf(a1), hr.im };
DrawLine3D(q0, q1, (Color){ 170, 226, 255, (unsigned char)(100 + 60 * (1.0f - pulse)) });
}
DrawLine3D((Vector3){ hr.re - tr * 1.55f, topY, hr.im }, (Vector3){ hr.re - tr * 0.80f, topY, hr.im }, (Color){ 255, 238, 196, 180 });
DrawLine3D((Vector3){ hr.re + tr * 0.80f, topY, hr.im }, (Vector3){ hr.re + tr * 1.55f, topY, hr.im }, (Color){ 255, 238, 196, 180 });
DrawLine3D((Vector3){ hr.re, topY, hr.im - tr * 1.55f }, (Vector3){ hr.re, topY, hr.im - tr * 0.80f }, (Color){ 170, 226, 255, 180 });
DrawLine3D((Vector3){ hr.re, topY, hr.im + tr * 0.80f }, (Vector3){ hr.re, topY, hr.im + tr * 1.55f }, (Color){ 170, 226, 255, 180 });
EndBlendMode();
}
}
if (probeActive) {
Complex pProbe = EvalPolynomialHomotopy(probeZ, a, b, mix);
float yProbe = SurfaceHeightFromValue(pProbe);
float ph = 0.16f * t;
BeginBlendMode(BLEND_ADDITIVE);
DrawDottedLine3D((Vector3){ -domain, mirrorY + 0.02f, probeZ.im }, (Vector3){ domain, mirrorY + 0.02f, probeZ.im }, 52, ph, 0.024f, (Color){ 122, 206, 255, 165 });
DrawDottedLine3D((Vector3){ probeZ.re, mirrorY + 0.02f, -domain }, (Vector3){ probeZ.re, mirrorY + 0.02f, domain }, 52, ph + 0.33f, 0.024f, (Color){ 255, 208, 142, 165 });
DrawDottedLine3D((Vector3){ probeZ.re, mirrorY + 0.02f, probeZ.im }, (Vector3){ probeZ.re, yProbe, probeZ.im }, 32, ph + 0.61f, 0.027f, (Color){ 255, 244, 214, 190 });
DrawSphere((Vector3){ probeZ.re, yProbe, probeZ.im }, 0.075f, (Color){ 255, 248, 220, 185 });
EndBlendMode();
}
EndMode3D();
EndScissorMode();
}
int main(void) {
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(1280, 780, "Complex Polynomial Roots - Raylib");
SetTargetFPS(60);
Preset presets[PRESET_COUNT] = {
{ "z^2 + 1 = 0", 2, { {0, 1}, {0, -1} } },
{ "z^2 - 5z + 6 = 0", 2, { {2, 0}, {3, 0} } },
{ "z^4 - 1 = 0", 4, { {1, 0}, {-1, 0}, {0, 1}, {0, -1} } },
{ "z^5 - 1 = 0", 5, { {1, 0}, {0.309f, 0.951f}, {-0.809f, 0.588f}, {-0.809f, -0.588f}, {0.309f, -0.951f} } },
{ "z^3 - 8 = 0", 3, { {2, 0}, {-1, 1.732f}, {-1, -1.732f} } },
{ "z^6 + 1 = 0", 6, {
{0.866f, 0.5f}, {0.0f, 1.0f}, {-0.866f, 0.5f},
{-0.866f, -0.5f}, {0.0f, -1.0f}, {0.866f, -0.5f}
} },
{ "(z-1)(z+2i)(z+1-i)=0", 3, { {1, 0}, {0, -2}, {-1, 1} } },
{ "z^7 - 1 = 0", 7, {
{1, 0}, {0.623f, 0.782f}, {-0.223f, 0.975f}, {-0.901f, 0.434f},
{-0.901f, -0.434f}, {-0.223f, -0.975f}, {0.623f, -0.782f}
} }
};
int page = 0;
int presetIndex = 0;
int targetPreset = 0;
int hoveredRoot = -1;
int paused = 0;
float transition = 1.0f;
float simTime = 0.0f;
float camYaw = 0.6f;
float camPitch = 0.22f;
float camZoom = 1.0f;
int draggingCamera = 0;
Vector2 lastMouse = { 0 };
int probeActive = 0;
Complex probeZ = { 0 };
Complex probeP = { 0 };
Complex probePA = { 0 };
Complex probePB = { 0 };
float probeY = 0.0f;
while (!WindowShouldClose()) {
if (IsKeyPressed(KEY_Q)) break;
if (IsKeyPressed(KEY_SPACE)) paused = !paused;
if (IsKeyPressed(KEY_ONE)) page = 0;
if (IsKeyPressed(KEY_TWO)) page = 1;
Vector2 mouse = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && mouse.y <= 62.0f) {
if (mouse.x >= 16 && mouse.x <= 126 && mouse.y >= 14 && mouse.y <= 48) page = 0;
if (mouse.x >= 138 && mouse.x <= 248 && mouse.y >= 14 && mouse.y <= 48) page = 1;
}
if (page == 0 && IsKeyPressed(KEY_P)) {
targetPreset = (presetIndex + 1) % PRESET_COUNT;
transition = 0.0f;
}
if (page == 0) {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && mouse.y > 62.0f) {
draggingCamera = 1;
lastMouse = mouse;
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
draggingCamera = 0;
}
if (draggingCamera && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
Vector2 d = { mouse.x - lastMouse.x, mouse.y - lastMouse.y };
camYaw -= d.x * 0.0075f;
camPitch -= d.y * 0.0070f;
if (camPitch < -1.2f) camPitch = -1.2f;
if (camPitch > 1.2f) camPitch = 1.2f;
lastMouse = mouse;
}
float wheel = GetMouseWheelMove();
if (wheel != 0.0f) {
camZoom -= wheel * 0.08f;
if (camZoom < 0.55f) camZoom = 0.55f;
if (camZoom > 2.2f) camZoom = 2.2f;
}
} else {
draggingCamera = 0;
}
float dt = GetFrameTime();
if (!paused) {
simTime += dt;
if (transition < 1.0f) {
transition += dt * 0.42f;
if (transition >= 1.0f) {