-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCG1.cpp
More file actions
856 lines (682 loc) · 25.3 KB
/
CG1.cpp
File metadata and controls
856 lines (682 loc) · 25.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
//ca sa nu se amestece codurile, le-am comentat pe toate si le-am delimitat prin + si decomentate pe rand pt a le rula.
/*
#include "glut.h"
#include <cmath>
#include <string>
// Constante pentru precizie maximă
const double PI = 3.14159265358979323846;
// Setăm nivelul inițial la 2 (pentru prima imagine din cerința ta)
// Apăsând tasta '+' vei ajunge la nivelul 5 (a doua imagine)
int nivelRecursivitate = 2;
const double razaCerc = 0.8;
class Turtle {
public:
double x, y, unghi;
Turtle(double startX, double startY) : x(startX), y(startY), unghi(0) {}
void move(double distanta, bool deseneaza) {
double xNou = x + distanta * cos(unghi * PI / 180.0);
double yNou = y + distanta * sin(unghi * PI / 180.0);
if (deseneaza) {
glBegin(GL_LINES);
glVertex2d(x, y);
glVertex2d(xNou, yNou);
glEnd();
}
x = xNou;
y = yNou;
}
void turn(double grade) {
unghi += grade;
}
};
void deseneazaLinieKoch(Turtle &t, double lungime, int nivel) {
if (nivel == 0) {
t.move(lungime, true);
} else {
double l = lungime / 3.0;
deseneazaLinieKoch(t, l, nivel - 1);
t.turn(60);
deseneazaLinieKoch(t, l, nivel - 1);
t.turn(-120);
deseneazaLinieKoch(t, l, nivel - 1);
t.turn(60);
deseneazaLinieKoch(t, l, nivel - 1);
}
}
void deseneazaCerc(double r) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 200; i++) { // 200 de segmente pentru un cerc și mai fin
double theta = 2.0 * PI * double(i) / 200.0;
glVertex2d(r * cos(theta), r * sin(theta));
}
glEnd();
}
void afiseazaText(float x, float y, std::string text) {
glRasterPos2f(x, y);
for (char c : text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, c);
}
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
// Curățăm matricea pentru a preveni deplasarea desenului
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
// 1. Desenăm cercul circumscris
deseneazaCerc(razaCerc);
// 2. Calculăm latura L a triunghiului echilateral înscris în cerc
double lungimeLatura = razaCerc * sqrt(3.0);
// 3. Poziționăm țestoasa în vârful de sus al cercului (0, R)
Turtle t(0.0, razaCerc);
// CORELARE GEOMETRICĂ: Pentru a desena în sensul acelor de ceasornic
// și a lăsa fractalul să crească spre EXTERIOR, trebuie să începem
// coborând spre dreapta-jos (unghi de -60 grade).
t.turn(-60);
// 4. Desenăm cele 3 laturi
for (int i = 0; i < 3; i++) {
deseneazaLinieKoch(t, lungimeLatura, nivelRecursivitate);
t.turn(-120); // La fiecare colț, ne întoarcem spre interiorul triunghiului
}
// Afișăm textul informativ
std::string info = "Recursion Level: " + std::to_string(nivelRecursivitate);
afiseazaText(-0.95, -0.95, info);
glutSwapBuffers();
}
void Reshape(int w, int h) {
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Păstrăm proporția corectă (cerc perfect, nu oval)
float aspect = (float)w / (float)h;
if (w >= h)
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
else
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
void Keyboard(unsigned char key, int x, int y) {
if (key == '+' || key == '=') {
if (nivelRecursivitate < 7) nivelRecursivitate++;
} else if (key == '-' || key == '_') {
if (nivelRecursivitate > 0) nivelRecursivitate--;
}
glutPostRedisplay(); // Cere desenarea noului cadru
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Koch Snowflake - Rezolvare Corecta");
glClearColor(1.0, 1.0, 1.0, 1.0);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMainLoop();
return 0;
}
*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
#include "glut.h"
#include <cmath>
#include <string>
const double PI = 3.14159265358979323846;
int nivelRecursivitateArbore = 2;
const double razaCercArbore = 0.6; // Păstrăm variabila doar pentru a calcula lungimea tulpinii
class TurtleArbore {
public:
double x, y, unghi;
TurtleArbore(double startX, double startY) : x(startX), y(startY), unghi(0) {}
void move(double distanta, bool deseneaza) {
double xNou = x + distanta * cos(unghi * PI / 180.0);
double yNou = y + distanta * sin(unghi * PI / 180.0);
if (deseneaza) {
glBegin(GL_LINES);
glVertex2d(x, y);
glVertex2d(xNou, yNou);
glEnd();
}
x = xNou;
y = yNou;
}
void turn(double grade) {
unghi += grade;
}
};
void deseneazaArboreFractal(TurtleArbore t, double lungime, int nivel, double unghiRamificare, double factorScalare) {
if (nivel == 0) {
t.move(lungime, true);
} else {
t.move(lungime, true);
TurtleArbore tStateStanga = t;
TurtleArbore tStateDreapta = t;
tStateStanga.turn(unghiRamificare);
deseneazaArboreFractal(tStateStanga, lungime * factorScalare, nivel - 1, unghiRamificare, factorScalare);
tStateDreapta.turn(-unghiRamificare);
deseneazaArboreFractal(tStateDreapta, lungime * factorScalare, nivel - 1, unghiRamificare, factorScalare);
}
}
void afiseazaTextArbore(float x, float y, std::string text) {
glRasterPos2f(x, y);
for (char c : text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, c);
}
}
void DisplayArbore() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
// --- CERCUL A FOST ELIMINAT COMPLET DE AICI ---
// 1. Tulpina pleacă de jos, orientată în sus
TurtleArbore t(0.0, -razaCercArbore);
t.turn(90);
// Parametrii care permit fractalului să se extindă
double lungimeTulpinainitiala = razaCercArbore;
double unghiRamificareArbore = 45.0;
double factorScalareArbore = 0.7;
// 2. Desenăm copacul
deseneazaArboreFractal(t, lungimeTulpinainitiala, nivelRecursivitateArbore, unghiRamificareArbore, factorScalareArbore);
std::string info = "Recursion Level: " + std::to_string(nivelRecursivitateArbore);
afiseazaTextArbore(-0.95, -0.95, info);
glutSwapBuffers();
}
void ReshapeArbore(int w, int h) {
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)w / (float)h;
if (w >= h)
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
else
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
void KeyboardArbore(unsigned char key, int x, int y) {
if (key == '+' || key == '=') {
if (nivelRecursivitateArbore < 12) nivelRecursivitateArbore++;
} else if (key == '-' || key == '_') {
if (nivelRecursivitateArbore > 0) nivelRecursivitateArbore--;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Arbore Fractal - Fara Cerc");
glClearColor(1.0, 1.0, 1.0, 1.0);
glutDisplayFunc(DisplayArbore);
glutReshapeFunc(ReshapeArbore);
glutKeyboardFunc(KeyboardArbore);
glutMainLoop();
return 0;
}
*/
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
#include "glut.h"
#include <cmath>
#include <string>
const double PI = 3.14159265358979323846;
// Setăm nivelul de pornire la 2 pentru a coincide exact cu prima ta imagine
int nivelRecursivitate = 2;
// --- Clasa Turtle Graphics (Singurul loc unde există glBegin) ---
class Turtle {
public:
double x, y, unghi;
Turtle(double startX, double startY) : x(startX), y(startY), unghi(0) {}
void move(double distanta, bool deseneaza) {
double xNou = x + distanta * cos(unghi * PI / 180.0);
double yNou = y + distanta * sin(unghi * PI / 180.0);
// Doar funcția move a țestoasei are voie să deseneze linii
if (deseneaza) {
glBegin(GL_LINES);
glVertex2d(x, y);
glVertex2d(xNou, yNou);
glEnd();
}
x = xNou;
y = yNou;
}
void turn(double grade) {
unghi += grade;
}
};
// --- Funcție Ajutătoare Respectând Restricția ---
// În loc să folosim glBegin(GL_QUADS), forțăm o țestoasă nouă să traseze un pătrat
void deseneazaPatratCuTestoasa(double cx, double cy, double latura) {
// Calculăm colțul din stânga-jos al pătratului
double startX = cx - latura / 2.0;
double startY = cy - latura / 2.0;
Turtle t(startX, startY);
t.unghi = 0; // Orientată spre dreapta
// Țestoasa trasează cele 4 laturi
for (int i = 0; i < 4; i++) {
t.move(latura, true);
t.turn(90);
}
}
// --- LOGICA RECURSIVĂ: Găurile din Covorul Sierpiński ---
void deseneazaCovorSierpinski(double cx, double cy, double dimensiune, int nivel) {
if (nivel == 0) return;
// 1. Pătratul central are întotdeauna o treime din dimensiunea regiunii curente
double dimensiuneGaura = dimensiune / 3.0;
// Desenăm "gaura" din centru folosind țestoasa
deseneazaPatratCuTestoasa(cx, cy, dimensiuneGaura);
// 2. Apelăm recursiv pentru cele 8 sub-regiuni din jurul centrului
if (nivel > 1) {
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
if (dx == 0 && dy == 0) continue; // Sărim peste centrul deja desenat
double centruNouX = cx + dx * dimensiuneGaura;
double centruNouY = cy + dy * dimensiuneGaura;
deseneazaCovorSierpinski(centruNouX, centruNouY, dimensiuneGaura, nivel - 1);
}
}
}
}
void afiseazaText(float x, float y, std::string text) {
glRasterPos2f(x, y);
for (char c : text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, c);
}
}
void Display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0); // Roșu
// Dimensiunea pătratului exterior maxim
double laturaMaxima = 1.8;
// 1. Desenăm marginea exterioară (Nivelul 0 de bază)
deseneazaPatratCuTestoasa(0.0, 0.0, laturaMaxima);
// 2. Pornim recursivitatea care decupează "găurile" în interior
deseneazaCovorSierpinski(0.0, 0.0, laturaMaxima, nivelRecursivitate);
// 3. Afișăm textul în colțul stânga-jos
std::string info = "Recursion Level: " + std::to_string(nivelRecursivitate);
afiseazaText(-0.95, -0.95, info);
glutSwapBuffers();
}
void Reshape(int w, int h) {
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)w / (float)h;
if (w >= h)
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
else
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
void Keyboard(unsigned char key, int x, int y) {
if (key == '+' || key == '=') {
// Limităm la nivelul 5. Dincolo de nivelul 5 (aprox. 32.000 de pătrate)
// programul poate îngheța în funcție de procesor.
if (nivelRecursivitate < 5) nivelRecursivitate++;
} else if (key == '-' || key == '_') {
if (nivelRecursivitate > 0) nivelRecursivitate--;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Sierpiński Carpet - Turtle Graphics");
glClearColor(1.0, 1.0, 1.0, 1.0); // Fundal Alb
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMainLoop();
return 0;
}
*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// mai bine de atat nu pot
/*
#include "glut.h"
#include <cmath>
#include <string>
// Constanta PI pentru calculul unghiurilor
const double PI = 3.14159265358979323846;
// Setăm startul la Nivelul 2
int nivelRecursivitateArrowhead = 2;
// --- Clasa Turtle Graphics ---
class TurtleArrowhead {
public:
double x, y, unghi;
TurtleArrowhead(double startX, double startY) : x(startX), y(startY), unghi(0) {}
void move(double distanta, bool deseneaza) {
double xNou = x + distanta * cos(unghi * PI / 180.0);
double yNou = y + distanta * sin(unghi * PI / 180.0);
if (deseneaza) {
glBegin(GL_LINES);
glVertex2d(x, y);
glVertex2d(xNou, yNou);
glEnd();
}
x = xNou;
y = yNou;
}
void turn(double grade) {
unghi += grade;
}
};
// --- LOGICA RECURSIVĂ: Curba Sierpinski Arrowhead ---
void deseneazaArrowheadCurve(TurtleArrowhead &t, double lungime, int nivel, double turn_angle) {
if (nivel == 0) {
t.move(lungime, true);
} else {
// CORECȚIA 1: Lungimea se împarte la 2, nu la 3!
double l = lungime / 2.0;
deseneazaArrowheadCurve(t, l, nivel - 1, -turn_angle);
t.turn(turn_angle);
deseneazaArrowheadCurve(t, l, nivel - 1, turn_angle);
t.turn(turn_angle);
deseneazaArrowheadCurve(t, l, nivel - 1, -turn_angle);
}
}
void afiseazaTextArrowhead(float x, float y, std::string text) {
glRasterPos2f(x, y);
for (char c : text) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, c);
}
}
void DisplayArrowhead() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
// CORECȚIA 2: Mutăm startul complet în stânga-jos (-0.9, -0.9)
// pentru a avea loc să deseneze de la stânga la dreapta
TurtleArrowhead t(-0.9, -0.9);
double lungimeTotala = 1.8;
// CORECȚIA 3: Regula de Paritate (din algoritmul original de pe Wikipedia)
// Dacă nivelul este impar, trebuie să rotim țestoasa inițial pentru ca
// triunghiul să crească în sus, nu în jos.
if (nivelRecursivitateArrowhead % 2 == 0) {
// Nivel par (ex: 0, 2, 4, 6)
deseneazaArrowheadCurve(t, lungimeTotala, nivelRecursivitateArrowhead, 60.0);
} else {
// Nivel impar (ex: 1, 3, 5)
t.turn(60.0);
deseneazaArrowheadCurve(t, lungimeTotala, nivelRecursivitateArrowhead, -60.0);
}
std::string info = "Recursion Level: " + std::to_string(nivelRecursivitateArrowhead);
afiseazaTextArrowhead(-0.95, -0.95, info);
glutSwapBuffers();
}
void ReshapeArrowhead(int w, int h) {
if (h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float)w / (float)h;
if (w >= h)
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
else
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
void KeyboardArrowhead(unsigned char key, int x, int y) {
if (key == '+' || key == '=') {
if (nivelRecursivitateArrowhead < 8) nivelRecursivitateArrowhead++;
} else if (key == '-' || key == '_') {
if (nivelRecursivitateArrowhead > 0) nivelRecursivitateArrowhead--;
}
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Sierpinski Arrowhead Curve Perfect");
glClearColor(1.0, 1.0, 1.0, 1.0);
glutDisplayFunc(DisplayArrowhead);
glutReshapeFunc(ReshapeArrowhead);
glutKeyboardFunc(KeyboardArrowhead);
glutMainLoop();
return 0;
}
*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// julia-fatou a iesit prea...straveziu
/*
#include "glut.h"
#include <cmath>
#include <iostream>
// Rezoluția ferestrei - păstrăm 800x800 pentru aspect ratio 1:1
const int WIDTH = 800;
const int HEIGHT = 800;
// Constantele exacte cerute (a = -0.81, b = -0.2)
const double a = -0.81;
const double b = -0.2;
// Parametrii de iterație
const int MAX_ITER = 256;
const double RAZA_EVADARE_SQ = 4.0; // r=2.0 -> r^2=4.0
// --- FUNCȚIA DE COLORARE CALIBRATĂ ---
// Aceasta generează gradientul: Albastru -> Verde -> Galben -> Roșu
void set_color(int iteratii) {
if (iteratii == MAX_ITER) {
// Interiorul fractalului este NEGRU
glColor3f(0.0f, 0.0f, 0.0f);
} else {
// Factor de scalare pentru densitatea culorilor
// Ajustăm 't' pentru a trece prin tot spectrul vizibil
float t = (float)iteratii / 15.0f;
// Algoritm de colorare sinusoidal defazat pentru paleta "Rainbow"
// Red: maxim la iterații mari (lângă fractal)
// Green: maxim la iterații medii
// Blue: maxim la iterații mici (fundal)
float r = 0.5f + 0.5f * std::sin(t + 3.1415f); // Defazaj 180 grade
float g = 0.5f + 0.5f * std::sin(t + 1.5707f); // Defazaj 90 grade
float bl = 0.5f + 0.5f * std::sin(t); // Fără defazaj
glColor3f(r, g, bl);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
// Zoom-ul imaginii: [-1.3, 1.3] oferă o încadrare similară cu poza ta
// Folosim o scară simetrică pentru a evita deformarea
double view_scale = 1.3;
for (int px = 0; px < WIDTH; px++) {
for (int py = 0; py < HEIGHT; py++) {
// 1. Mapăm pixelul (px, py) în planul complex
double x = -view_scale + (2.0 * view_scale * px) / WIDTH;
double y = -view_scale + (2.0 * view_scale * py) / HEIGHT;
// Julia: z0 este coordonata pixelului
double zx = x;
double zy = y;
int iter = 0;
// 2. Bucla fractalului: z = z^2 + c
// Optimizat: calculăm pătratele o singură dată pe iterație
double zx2 = zx * zx;
double zy2 = zy * zy;
while (zx2 + zy2 < RAZA_EVADARE_SQ && iter < MAX_ITER) {
zy = 2.0 * zx * zy + b;
zx = zx2 - zy2 + a;
zx2 = zx * zx;
zy2 = zy * zy;
iter++;
}
// 3. Aplicăm culoarea și desenăm punctul
set_color(iter);
glVertex2d(x, y);
}
}
glEnd();
glutSwapBuffers();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Fundalul ferestrei
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Coordonatele vederii (match cu view_scale)
gluOrtho2D(-1.3, 1.3, -1.3, 1.3);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("Julia-Fatou - Full Gradient Palette");
init();
glutDisplayFunc(display);
std::cout << "Se genereaza fractalul... ai rabdare oleaca." << std::endl;
glutMainLoop();
return 0;
}
*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* mandelbrot stil dexter
#include "glut.h"
#include <cmath>
#include <iostream>
// Rezoluția ferestrei
const int WIDTH = 800;
const int HEIGHT = 800;
// Parametrii de iterație pentru detalii fine
const int MAX_ITER = 500;
const double ESCAPE_RADIUS_SQ = 4.0;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
// Mapăm vizualizarea pentru a cuprinde întreg setul Mandelbrot
// Planul complex: x intre [-2.0, 1.0], y intre [-1.5, 1.5]
double x_min = -2.1, x_max = 0.6;
double y_min = -1.35, y_max = 1.35;
for (int px = 0; px < WIDTH; px++) {
for (int py = 0; py < HEIGHT; py++) {
// 1. Mapăm pixelul (px, py) la coordonata complexă c = x0 + y0 * i
double x0 = x_min + (x_max - x_min) * px / WIDTH;
double y0 = y_min + (y_max - y_min) * py / HEIGHT;
// Mandelbrot: z0 = 0
double zx = 0.0;
double zy = 0.0;
int iter = 0;
// 2. Aplicăm regula recursivă: z_{n+1} = z_n^2 + c
while (zx * zx + zy * zy < ESCAPE_RADIUS_SQ && iter < MAX_ITER) {
double xtemp = zx * zx - zy * zy + x0;
zy = 2.0 * zx * zy + y0;
zx = xtemp;
iter++;
}
// 3. Logica de colorare (Cerința: Fractal Roșu, Fundal Alb)
if (iter == MAX_ITER) {
// Punctul aparține setului (nu a evadat) -> ROȘU
glColor3f(1.0f, 0.0f, 0.0f);
} else {
// Punctul a evadat -> ALB
glColor3f(1.0f, 1.0f, 1.0f);
}
glVertex2d(x0, y0);
}
}
glEnd();
glutSwapBuffers();
}
void init() {
// Fundal alb implicit
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Setăm proiecția să se potrivească cu intervalul mapat mai sus
gluOrtho2D(-2.1, 0.6, -1.35, 1.35);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mandelbrot Set - Red on White");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// alt fractal straveziu
/*
#include "glut.h"
#include <cmath>
#include <iostream>
// Rezoluția ferestrei
const int WIDTH = 800;
const int HEIGHT = 800;
// Parametrii de iterație pentru detalii fine
const int MAX_ITER = 256;
const double ESCAPE_RADIUS_SQ = 4.0;
// --- FUNCȚIA DE COLORARE CALIBRATĂ ---
// Generează gradientul: Albastru (exterior) -> Cyan -> Verde -> Galben -> Roșu (margine)
void set_mandelbrot_color(int iter) {
if (iter == MAX_ITER) {
// Interiorul setului este NEGRU
glColor3f(0.0f, 0.0f, 0.0f);
} else {
// Factor de scalare pentru densitatea culorilor (frecvența benzilor)
float t = (float)iter / 15.0f;
// Algoritm sinusoidal pentru paleta de culori "Rainbow"
// Calibrat pentru a începe cu Albastru la iterații mici
float r = 0.5f + 0.5f * std::sin(t + 3.1415f); // Roșu (maxim la marginea setului)
float g = 0.5f + 0.5f * std::sin(t + 1.5707f); // Verde
float b = 0.5f + 0.5f * std::sin(t + 0.0000f); // Albastru (maxim în exterior)
glColor3f(r, g, b);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
// Mapăm vizualizarea pentru a cuprinde întreg setul Mandelbrot fără deformare
// Interval standard: x în [-2.1, 0.6], y în [-1.35, 1.35]
double x_min = -2.1, x_max = 0.6;
double y_min = -1.35, y_max = 1.35;
for (int px = 0; px < WIDTH; px++) {
for (int py = 0; py < HEIGHT; py++) {
// 1. Mapăm pixelul (px, py) la coordonata complexă c = x0 + y0 * i
double x0 = x_min + (x_max - x_min) * px / WIDTH;
double y0 = y_min + (y_max - y_min) * py / HEIGHT;
// Mandelbrot: punctul de start z0 este mereu (0,0)
double zx = 0.0;
double zy = 0.0;
int iter = 0;
// Optimizare: calculăm pătratele o singură dată pe iterație
double zx2 = 0.0;
double zy2 = 0.0;
// 2. Aplicăm regula recursivă: z_{n+1} = z_n^2 + c
while (zx2 + zy2 < ESCAPE_RADIUS_SQ && iter < MAX_ITER) {
zy = 2.0 * zx * zy + y0;
zx = zx2 - zy2 + x0;
zx2 = zx * zx;
zy2 = zy * zy;
iter++;
}
// 3. Aplicăm culoarea bazată pe numărul de iterații
set_mandelbrot_color(iter);
glVertex2d(x0, y0);
}
}
glEnd();
glutSwapBuffers();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Fundal negru până la randare
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Setăm proiecția ortografică pentru a se potrivi cu planul complex mapat
gluOrtho2D(-2.1, 0.6, -1.35, 1.35);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitWindowPosition(100, 100);
glutCreateWindow("Mandelbrot Set - Full Color Gradient");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
*/