Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.52 KB | None | 0 0
  1. #include <windows.h>
  2. #pragma warning(disable : 4996)
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #include <assert.h>
  7. #include <float.h>
  8. #include <GL/glut.h>
  9.  
  10. // dimensiunea ferestrei in pixeli
  11. #define dim 300
  12. #define NRITER_MB 5000
  13. #define MODMAX_MB 10000000
  14. #define RX_MB 0.01
  15. #define RY_MB 0.01
  16.  
  17. unsigned char prevKey, lastOption = 0;
  18. int nivel = 0;
  19.  
  20. class C2coord
  21. {
  22. public:
  23. C2coord()
  24. {
  25. m.x = m.y = 0;
  26. }
  27.  
  28. C2coord(double x, double y)
  29. {
  30. m.x = x;
  31. m.y = y;
  32. }
  33.  
  34. C2coord(const C2coord &p)
  35. {
  36. m.x = p.m.x;
  37. m.y = p.m.y;
  38. }
  39.  
  40. C2coord &operator=(C2coord &p)
  41. {
  42. m.x = p.m.x;
  43. m.y = p.m.y;
  44. return *this;
  45. }
  46.  
  47. int operator==(C2coord &p)
  48. {
  49. return ((m.x == p.m.x) && (m.y == p.m.y));
  50. }
  51.  
  52. protected:
  53. struct SDate
  54. {
  55. double x, y;
  56. } m;
  57. };
  58.  
  59. class CPunct : public C2coord
  60. {
  61. public:
  62. CPunct() : C2coord(0.0, 0.0)
  63. {
  64. }
  65.  
  66. CPunct(double x, double y) : C2coord(x, y)
  67. {
  68. }
  69.  
  70. CPunct &operator=(const CPunct &p)
  71. {
  72. m.x = p.m.x;
  73. m.y = p.m.y;
  74. return *this;
  75. }
  76.  
  77. void getxy(double &x, double &y)
  78. {
  79. x = m.x;
  80. y = m.y;
  81. }
  82.  
  83. int operator==(CPunct &p)
  84. {
  85. return ((m.x == p.m.x) && (m.y == p.m.y));
  86. }
  87.  
  88. void marcheaza()
  89. {
  90. glBegin(GL_POINTS);
  91. glVertex2d(m.x, m.y);
  92. glEnd();
  93. }
  94.  
  95. void print(FILE *fis)
  96. {
  97. fprintf(fis, "(%+f,%+f)", m.x, m.y);
  98. }
  99. };
  100.  
  101. class CVector : public C2coord
  102. {
  103. public:
  104. CVector() : C2coord(0.0, 0.0)
  105. {
  106. normalizare();
  107. }
  108.  
  109. CVector(double x, double y) : C2coord(x, y)
  110. {
  111. normalizare();
  112. }
  113.  
  114. CVector &operator=(CVector &p)
  115. {
  116. m.x = p.m.x;
  117. m.y = p.m.y;
  118. return *this;
  119. }
  120.  
  121. int operator==(CVector &p)
  122. {
  123. return ((m.x == p.m.x) && (m.y == p.m.y));
  124. }
  125.  
  126. CPunct getDest(CPunct &orig, double lungime)
  127. {
  128. double x, y;
  129. orig.getxy(x, y);
  130. CPunct p(x + m.x * lungime, y + m.y * lungime);
  131. return p;
  132. }
  133.  
  134. void rotatie(double grade)
  135. {
  136. double x = m.x;
  137. double y = m.y;
  138. double t = 2 * (4.0 * atan(1)) * grade / 360.0;
  139. m.x = x * cos(t) - y * sin(t);
  140. m.y = x * sin(t) + y * cos(t);
  141. normalizare();
  142. }
  143.  
  144. void deseneaza(CPunct p, double lungime)
  145. {
  146. double x, y;
  147. p.getxy(x, y);
  148. glColor3f(1.0, 0.1, 0.1);
  149. glBegin(GL_LINE_STRIP);
  150. glVertex2d(x, y);
  151. glVertex2d(x + m.x * lungime, y + m.y * lungime);
  152. glEnd();
  153. }
  154.  
  155. void print(FILE *fis)
  156. {
  157. fprintf(fis, "%+fi %+fj", C2coord::m.x, C2coord::m.y);
  158. }
  159.  
  160. private:
  161. void normalizare()
  162. {
  163. double d = sqrt(C2coord::m.x * C2coord::m.x + C2coord::m.y * C2coord::m.y);
  164. if (d != 0.0)
  165. {
  166. C2coord::m.x = C2coord::m.x * 1.0 / d;
  167. C2coord::m.y = C2coord::m.y * 1.0 / d;
  168. }
  169. }
  170. };
  171.  
  172. class CComplex
  173. {
  174. public:
  175. CComplex() : re(0.0), im(0.0) {}
  176. CComplex(double re1, double im1) : re(re1 * 1.0), im(im1 * 1.0) {}
  177. CComplex(const CComplex &c) : re(c.re), im(c.im) {}
  178. ~CComplex() {}
  179.  
  180. CComplex &operator=(const CComplex &c)
  181. {
  182. re = c.re;
  183. im = c.im;
  184. return *this;
  185. }
  186.  
  187. double getRe() { return re; }
  188. void setRe(double re1) { re = re1; }
  189.  
  190. double getIm() { return im; }
  191. void setIm(double im1) { im = im1; }
  192.  
  193. double getModul() { return sqrt(re * re + im * im); }
  194.  
  195. int operator==(CComplex &c1)
  196. {
  197. return ((re == c1.re) && (im == c1.im));
  198. }
  199.  
  200. CComplex pow2()
  201. {
  202. CComplex rez;
  203. rez.re = powl(re * 1.0, 2) - powl(im * 1.0, 2);
  204. rez.im = 2.0 * re * im;
  205. return rez;
  206. }
  207.  
  208. friend CComplex operator+(const CComplex &c1, const CComplex &c2);
  209. friend CComplex operator*(CComplex &c1, CComplex &c2);
  210.  
  211. void print(FILE *f)
  212. {
  213. fprintf(f, "%.20f%+.20f i", re, im);
  214. }
  215.  
  216. private:
  217. double re, im;
  218. };
  219.  
  220. CComplex operator+(const CComplex &c1, const CComplex &c2)
  221. {
  222. CComplex rez(c1.re + c2.re, c1.im + c2.im);
  223. return rez;
  224. }
  225.  
  226. CComplex operator*(CComplex &c1, CComplex &c2)
  227. {
  228. CComplex rez(c1.re * c2.re - c1.im * c2.im,
  229. c1.re * c2.im + c1.im * c2.re);
  230. return rez;
  231. }
  232.  
  233. class CMandelbrot
  234. {
  235. public:
  236. CMandelbrot()
  237. {
  238. m1.nriter = NRITER_MB;
  239. m1.modmax = MODMAX_MB;
  240. }
  241.  
  242. CMandelbrot(CComplex &c)
  243. {
  244. m1.c = c;
  245. m1.nriter = NRITER_MB;
  246. m1.modmax = MODMAX_MB;
  247. m1.contor = 0;
  248. }
  249.  
  250. ~CMandelbrot() {}
  251.  
  252. void setmodmax(double v)
  253. {
  254. assert(v <= MODMAX_MB);
  255. m1.modmax = v;
  256. }
  257. double getmodmax() { return m1.modmax; }
  258.  
  259. void setnriter(int v)
  260. {
  261. assert(v <= NRITER_MB);
  262. m1.nriter = v;
  263. }
  264. int getnriter() { return m1.nriter; }
  265.  
  266. int isInMandelbrot(CComplex &x)
  267. {
  268. int rez = -1;
  269. CComplex z0(0.0, 0.0);
  270. int i = 0;
  271. CComplex z1;
  272. while (i < 10 && z1.getModul() <= 2.0)
  273. {
  274. z1 = z0 * z0 + x;
  275. z0 = z1;
  276. i++;
  277. }
  278. if (z1.getModul() <= 2.0)
  279. rez = 0;
  280. else
  281. rez = i;
  282. return rez;
  283. }
  284.  
  285. void displayMandelbrout(double xmin, double ymin, double xmax, double ymax)
  286. {
  287. glPushMatrix();
  288. glLoadIdentity();
  289.  
  290. glBegin(GL_POINTS);
  291. for (double x = xmin; x <= xmax; x += RX_MB)
  292. for (double y = ymin; y <= ymax; y += RY_MB)
  293. {
  294. CComplex c(x, y);
  295. int r = isInMandelbrot(c);
  296. if (r == 0)
  297. {
  298. glColor3f(1, 0.1, 0.1);
  299. glVertex3d(x / 2.0, y / 2.0, 0);
  300. }
  301. else
  302. {
  303. glColor3f(0.1 * r, 0, 0.1 * r);
  304. glVertex3d(x / 2.0, y / 2.0, 0);
  305. }
  306. }
  307. fprintf(stdout, "STOP\n");
  308. glEnd();
  309.  
  310. glPopMatrix();
  311. }
  312.  
  313. private:
  314. struct SDate
  315. {
  316. CComplex c;
  317. int nriter;
  318. double modmax;
  319. int contor;
  320. } m1;
  321. };
  322.  
  323. class CImagine1
  324. {
  325. public:
  326. void imagine1(double l, int nivel, CPunct p)
  327. {
  328. if (nivel == 0)
  329. {
  330. CVector v(-1, 1);
  331. l /= 3;
  332. p = v.getDest(p, l * sqrt((double)2) / 2.0);
  333. v.rotatie(-135);
  334. for (int i = 0; i < 4; ++i)
  335. {
  336. v.deseneaza(p, l);
  337. p = v.getDest(p, l);
  338. v.rotatie(-90);
  339. }
  340. return;
  341. }
  342.  
  343. imagine1(l, 0, p);
  344. CVector v(-1, -1);
  345. for (int i = 0; i < 8; ++i)
  346. {
  347. CPunct p1 = v.getDest(p, (i % 2 ? l : l * sqrt((double)2)) / 3.0);
  348. imagine1(l / 3, nivel - 1, p1);
  349. v.rotatie(45);
  350. }
  351. }
  352.  
  353. void afisare(double lungime, int nivel)
  354. {
  355. CPunct p(0.0, 0.0);
  356. if (nivel > 0)
  357. imagine1(lungime * 3, 0, p);
  358. imagine1(lungime, nivel, p);
  359. }
  360. };
  361.  
  362. class CImagine2
  363. {
  364. public:
  365. void imagine2(double lungime,
  366. int nivel,
  367. double factordiviziune,
  368. CPunct p,
  369. CVector v)
  370. {
  371. if (factordiviziune != 0)
  372. {
  373. CPunct p1, p2;
  374. if (nivel != 0)
  375. {
  376. v.rotatie(-45);
  377. v.deseneaza(p, lungime);
  378. p1 = v.getDest(p, lungime);
  379. imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
  380.  
  381. v.rotatie(90);
  382. v.deseneaza(p, lungime);
  383. p1 = v.getDest(p, lungime);
  384. p2 = p1;
  385.  
  386. v.rotatie(15);
  387. v.deseneaza(p1, lungime);
  388. p1 = v.getDest(p1, lungime);
  389. imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
  390.  
  391. p1 = p2;
  392. v.rotatie(-60);
  393. v.deseneaza(p1, lungime);
  394. p1 = v.getDest(p1, lungime);
  395. p2 = p1;
  396.  
  397. v.rotatie(-90);
  398. v.deseneaza(p1, lungime / 2);
  399. p1 = v.getDest(p1, lungime / 2);
  400. imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
  401.  
  402. p1 = p2;
  403. v.rotatie(120);
  404. v.deseneaza(p1, lungime / 2);
  405. p1 = v.getDest(p1, lungime / 2);
  406. imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
  407. }
  408. }
  409. }
  410.  
  411. void afisare(double lungime, int nivel)
  412. {
  413. CVector v(0.0, -1.0);
  414. CPunct p(0.0, 1.0);
  415.  
  416. v.deseneaza(p, 0.25);
  417. p = v.getDest(p, 0.25);
  418. imagine2(lungime, nivel, 0.4, p, v);
  419. }
  420. };
  421.  
  422. class CImagine3
  423. {
  424. public:
  425. void imagine3(double lungime, int nivel, int unghi, CPunct &p, CVector &v)
  426. {
  427. if (nivel == 0)
  428. {
  429. v.deseneaza(p, lungime);
  430. p = v.getDest(p, lungime);
  431. }
  432. else
  433. {
  434. for (int i = 0; i < 3; i++)
  435. {
  436. imagine3(lungime / 2, nivel - 1, (i % 2) ? unghi : -unghi, p, v);
  437. if (i != 2)
  438. v.rotatie(unghi);
  439. }
  440. }
  441. }
  442.  
  443. void afisare(double lungime, int nivel)
  444. {
  445. CVector v(0.0, 1.0);
  446. CPunct p(-0.5, -0.5);
  447.  
  448. imagine3(lungime, nivel, (nivel % 2) ? -60 : 60, p, v);
  449. }
  450. };
  451.  
  452. void Display1()
  453. {
  454. CComplex c;
  455. CMandelbrot cm(c);
  456.  
  457. glColor3f(1.0, 0.1, 0.1);
  458. cm.setnriter(30);
  459. cm.displayMandelbrout(-2, -2, 2, 2);
  460. }
  461.  
  462. void Display2()
  463. {
  464. CImagine1 cap;
  465.  
  466. char c[3];
  467. sprintf(c, "%2d", nivel);
  468. glRasterPos2d(-0.98, -0.98);
  469. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');
  470. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  471. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'v');
  472. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  473. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'l');
  474. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '=');
  475. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[0]);
  476. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[1]);
  477.  
  478. glRasterPos2d(-1.0, 0.93);
  479. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  480. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'm');
  481. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
  482. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'g');
  483. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  484. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'n');
  485. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  486. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
  487. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ' ');
  488. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '1');
  489.  
  490. cap.afisare(1.0, nivel);
  491. nivel++;
  492. }
  493.  
  494. void Display3()
  495. {
  496. CImagine2 cap;
  497.  
  498. char c[3];
  499. sprintf(c, "%2d", nivel);
  500. glRasterPos2d(-0.98, -0.98);
  501. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');
  502. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  503. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'v');
  504. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  505. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'l');
  506. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '=');
  507. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[0]);
  508. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[1]);
  509.  
  510. glRasterPos2d(-1.0, 0.93);
  511. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  512. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'm');
  513. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
  514. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'g');
  515. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  516. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'n');
  517. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  518. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
  519. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ' ');
  520. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '2');
  521.  
  522. glPushMatrix();
  523. glLoadIdentity();
  524. glScaled(0.4, 0.4, 1);
  525. glTranslated(-0.5, -0.5, 0.0);
  526. cap.afisare(1, nivel);
  527. glPopMatrix();
  528. nivel++;
  529. }
  530.  
  531. void Display4()
  532. {
  533. CImagine3 cap;
  534.  
  535. char c[3];
  536. sprintf(c, "%2d", nivel);
  537. glRasterPos2d(-0.98, -0.98);
  538. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');
  539. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  540. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'v');
  541. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  542. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'l');
  543. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '=');
  544. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[0]);
  545. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[1]);
  546.  
  547. glRasterPos2d(-1.0, 0.93);
  548. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  549. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'm');
  550. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
  551. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'g');
  552. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
  553. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'n');
  554. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  555. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
  556. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ' ');
  557. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '3');
  558.  
  559. cap.afisare(1.0, nivel);
  560. nivel++;
  561. }
  562.  
  563. void Init(void)
  564. {
  565.  
  566. glClearColor(1.0, 1.0, 1.0, 1.0);
  567.  
  568. glLineWidth(1);
  569.  
  570. glPolygonMode(GL_FRONT, GL_LINE);
  571. }
  572.  
  573. void Display(void)
  574. {
  575. if (prevKey != lastOption)
  576. nivel = 0;
  577. switch (prevKey)
  578. {
  579. case '0':
  580. glClear(GL_COLOR_BUFFER_BIT);
  581. prevKey = lastOption;
  582. nivel = 0;
  583. fprintf(stderr, "nivel = %d\n", nivel);
  584. Display();
  585. break;
  586. case '1':
  587. glClear(GL_COLOR_BUFFER_BIT);
  588. Display1();
  589. break;
  590. case '2':
  591. glClear(GL_COLOR_BUFFER_BIT);
  592. Display2();
  593. break;
  594. case '3':
  595. glClear(GL_COLOR_BUFFER_BIT);
  596. Display3();
  597. break;
  598. case '4':
  599. glClear(GL_COLOR_BUFFER_BIT);
  600. Display4();
  601. break;
  602. default:
  603. break;
  604. }
  605. lastOption = prevKey;
  606. glFlush();
  607. }
  608.  
  609. void Reshape(int w, int h)
  610. {
  611. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  612. }
  613.  
  614. void KeyboardFunc(unsigned char key, int x, int y)
  615. {
  616. prevKey = key;
  617. if (key == 27) // escape
  618. exit(0);
  619. glutPostRedisplay();
  620. }
  621.  
  622. void MouseFunc(int button, int state, int x, int y)
  623. {
  624. }
  625.  
  626. int main(int argc, char **argv)
  627. {
  628.  
  629. glutInit(&argc, argv);
  630.  
  631. glutInitWindowSize(dim, dim);
  632.  
  633. glutInitWindowPosition(100, 100);
  634.  
  635. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  636.  
  637. glutCreateWindow(argv[0]);
  638.  
  639. Init();
  640.  
  641. glutReshapeFunc(Reshape);
  642.  
  643. glutKeyboardFunc(KeyboardFunc);
  644.  
  645. glutMouseFunc(MouseFunc);
  646.  
  647. glutDisplayFunc(Display);
  648.  
  649. glutMainLoop();
  650.  
  651. return 0;
  652. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement