Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.16 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #pragma warning(disable: 4996)
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include <float.h>
  10. #include <iostream>
  11.  
  12.  
  13. #include "glut.h"
  14.  
  15. // dimensiunea ferestrei in pixeli
  16. #define dim 300
  17.  
  18. #define NRITER_MAN 3000
  19. #define MODMAX_MAN 10000000
  20.  
  21. #define RX_MAN 0.01
  22. #define RY_MAN 0.01
  23.  
  24.  
  25. unsigned char prevKey;
  26. int nivel = 0;
  27.  
  28. class CComplex {
  29. public:
  30. CComplex() : re(0.0), im(0.0) {}
  31. CComplex(double re1, double im1) : re(re1 * 1.0), im(im1 * 1.0) {}
  32. CComplex(const CComplex &c) : re(c.re), im(c.im) {}
  33. ~CComplex() {}
  34.  
  35. CComplex &operator=(const CComplex &c)
  36. {
  37. re = c.re;
  38. im = c.im;
  39. return *this;
  40. }
  41.  
  42. double getRe() { return re; }
  43. void setRe(double re1) { re = re1; }
  44.  
  45. double getIm() { return im; }
  46. void setIm(double im1) { im = im1; }
  47.  
  48. double getModul() { return sqrt(re * re + im * im); }
  49.  
  50. int operator==(CComplex &c1)
  51. {
  52. return ((re == c1.re) && (im == c1.im));
  53. }
  54.  
  55. CComplex pow2()
  56. {
  57. CComplex rezultat;
  58. rezultat.re = powl(re * 1.0, 2) - powl(im * 1.0, 2);
  59. rezultat.im = 2.0 * re * im;
  60. return rezultat;
  61. }
  62.  
  63. friend CComplex operator+(const CComplex &c1, const CComplex &c2);
  64. friend CComplex operator*(CComplex &c1, CComplex &c2);
  65.  
  66. void print(FILE *f)
  67. {
  68. fprintf(f, "%.20f%+.20f i", re, im);
  69. }
  70.  
  71. private:
  72. double re, im;
  73. };
  74.  
  75. CComplex operator+(const CComplex &c1, const CComplex &c2)
  76. {
  77. CComplex rezultat(c1.re + c2.re, c1.im + c2.im);
  78. return rezultat;
  79. }
  80.  
  81. CComplex operator*(CComplex &c1, CComplex &c2)
  82. {
  83. CComplex rezultat(c1.re * c2.re - c1.im * c2.im,
  84. c1.re * c2.im + c1.im * c2.re);
  85. return rezultat;
  86. }
  87.  
  88. class C2coord
  89. {
  90. public:
  91. C2coord()
  92. {
  93. m.x = m.y = 0;
  94. }
  95.  
  96. C2coord(double x, double y)
  97. {
  98. m.x = x;
  99. m.y = y;
  100. }
  101.  
  102. C2coord(const C2coord &p)
  103. {
  104. m.x = p.m.x;
  105. m.y = p.m.y;
  106. }
  107.  
  108. C2coord &operator=(C2coord &p)
  109. {
  110. m.x = p.m.x;
  111. m.y = p.m.y;
  112. return *this;
  113. }
  114.  
  115. int operator==(C2coord &p)
  116. {
  117. return ((m.x == p.m.x) && (m.y == p.m.y));
  118. }
  119.  
  120. protected:
  121. struct SDate
  122. {
  123. double x, y;
  124. } m;
  125. };
  126.  
  127. class CPunct : public C2coord
  128. {
  129. public:
  130. CPunct() : C2coord(0.0, 0.0)
  131. {}
  132.  
  133. CPunct(double x, double y) : C2coord(x, y)
  134. {}
  135.  
  136. CPunct &operator=(const CPunct &p)
  137. {
  138. m.x = p.m.x;
  139. m.y = p.m.y;
  140. return *this;
  141. }
  142.  
  143. void getxy(double &x, double &y)
  144. {
  145. x = m.x;
  146. y = m.y;
  147. }
  148.  
  149. int operator==(CPunct &p)
  150. {
  151. return ((m.x == p.m.x) && (m.y == p.m.y));
  152. }
  153.  
  154. void marcheaza()
  155. {
  156. glColor3f(1.0, 0.1, 0.1);
  157. glBegin(GL_POINTS);
  158. glVertex2d(m.x, m.y);
  159. glEnd();
  160. }
  161.  
  162. void print(FILE *fis)
  163. {
  164. fprintf(fis, "(%+f,%+f)", m.x, m.y);
  165. }
  166. };
  167.  
  168. class CVector : public C2coord
  169. {
  170. public:
  171. CVector() : C2coord(0.0, 0.0)
  172. {
  173. normalizare();
  174. }
  175.  
  176. CVector(double x, double y) : C2coord(x, y)
  177. {
  178. normalizare();
  179. }
  180.  
  181. CVector &operator=(CVector &p)
  182. {
  183. m.x = p.m.x;
  184. m.y = p.m.y;
  185. return *this;
  186. }
  187.  
  188. int operator==(CVector &p)
  189. {
  190. return ((m.x == p.m.x) && (m.y == p.m.y));
  191. }
  192.  
  193. CPunct getDest(CPunct &orig, double lungime)
  194. {
  195. double x, y;
  196. orig.getxy(x, y);
  197. CPunct p(x + m.x * lungime, y + m.y * lungime);
  198. return p;
  199. }
  200.  
  201. void rotatie(double grade)
  202. {
  203. double x = m.x;
  204. double y = m.y;
  205. double t = 2 * (4.0 * atan(1.0)) * grade / 360.0;
  206. m.x = x * cos(t) - y * sin(t);
  207. m.y = x * sin(t) + y * cos(t);
  208. normalizare();
  209. }
  210.  
  211. void deseneaza(CPunct p, double lungime)
  212. {
  213. double x, y;
  214. p.getxy(x, y);
  215. glColor3f(1.0, 0.1, 0.1);
  216. glBegin(GL_LINE_STRIP);
  217. glVertex2d(x, y);
  218. glVertex2d(x + m.x * lungime, y + m.y * lungime);
  219. glEnd();
  220. }
  221.  
  222. void print(FILE *fis)
  223. {
  224. fprintf(fis, "%+fi %+fj", C2coord::m.x, C2coord::m.y);
  225. }
  226.  
  227. private:
  228. void normalizare()
  229. {
  230. double d = sqrt(C2coord::m.x * C2coord::m.x + C2coord::m.y * C2coord::m.y);
  231. if (d != 0.0)
  232. {
  233. C2coord::m.x = C2coord::m.x * 1.0 / d;
  234. C2coord::m.y = C2coord::m.y * 1.0 / d;
  235. }
  236. }
  237. };
  238.  
  239. class CCurbaKoch
  240. {
  241. public:
  242. void segmentKoch(double lungime, int nivel, CPunct &p, CVector v)
  243. {
  244. CPunct punct;
  245. if (nivel == 0)
  246. {
  247. v.deseneaza(p, lungime);
  248. }
  249. else
  250. {
  251. // v.print(stderr);
  252. // fprintf(stderr, "\n");
  253. segmentKoch(lungime / 3.0, nivel - 1, p, v);
  254. punct = v.getDest(p, lungime / 3.0);
  255. v.rotatie(60);
  256. // v.print(stderr);
  257. // fprintf(stderr, "\n");
  258. segmentKoch(lungime / 3.0, nivel - 1, punct, v);
  259. punct = v.getDest(punct, lungime / 3.0);
  260. v.rotatie(-120);
  261. // v.print(stderr);
  262. // fprintf(stderr, "\n");
  263. segmentKoch(lungime / 3.0, nivel - 1, punct, v);
  264. punct = v.getDest(punct, lungime / 3.0);
  265. v.rotatie(60);
  266. // v.print(stderr);
  267. // fprintf(stderr, "\n");
  268. segmentKoch(lungime / 3.0, nivel - 1, punct, v);
  269. }
  270. }
  271.  
  272. void afisare(double lungime, int nivel)
  273. {
  274. CVector v1(sqrt(3.0) / 2.0, 0.5);
  275. CPunct punct(-1.0, 0.0);
  276.  
  277. CVector v2(0.0, -1.0);
  278. CPunct p2(0.5, sqrt(3.0) / 2.0);
  279.  
  280. CVector v3(-sqrt(3.0) / 2.0, 0.5);
  281. CPunct p3(0.5, -sqrt(3.0) / 2.0);
  282.  
  283. segmentKoch(lungime, nivel, punct, v1);
  284. segmentKoch(lungime, nivel, p2, v2);
  285. segmentKoch(lungime, nivel, p3, v3);
  286. }
  287. };
  288.  
  289. class CArboreBinar
  290. {
  291. public:
  292. void arboreBinar(double lungime, int nivel, CPunct &p, CVector v)
  293. {
  294. CPunct punct;
  295. if (nivel == 0)
  296. {
  297. v.deseneaza(p, lungime);
  298. }
  299. else
  300. {
  301. arboreBinar(lungime, nivel - 1, p, v);
  302. punct = v.getDest(p, lungime);
  303.  
  304. v.rotatie(-45);
  305. arboreBinar(lungime / 2.0, nivel - 1, punct, v);
  306.  
  307. v.rotatie(90);
  308. arboreBinar(lungime / 2.0, nivel - 1, punct, v);
  309. }
  310. }
  311.  
  312. void afisare(double lungime, int nivel)
  313. {
  314. CVector v(0.0, -1.0);
  315. CPunct p(0.0, 1.0);
  316.  
  317. arboreBinar(lungime, nivel, p, v);
  318. }
  319. };
  320.  
  321. class CArborePerron
  322. {
  323. public:
  324. void arborePerron(double lungime,
  325. int nivel,
  326. double factordiviziune,
  327. CPunct p,
  328. CVector v)
  329. {
  330. assert(factordiviziune != 0);
  331. CPunct punct, p2;
  332. if (nivel == 0)
  333. {
  334. }
  335. else
  336. {
  337. v.rotatie(30);
  338. v.deseneaza(p, lungime);
  339. punct = v.getDest(p, lungime);
  340. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  341.  
  342. v.rotatie(-90);
  343. v.deseneaza(p, lungime);
  344. punct = v.getDest(p, lungime);
  345. p2 = punct;
  346.  
  347. v.rotatie(-30);
  348. v.deseneaza(punct, lungime);
  349. punct = v.getDest(punct, lungime);
  350. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  351.  
  352. punct = p2;
  353. v.rotatie(90);
  354. v.deseneaza(punct, lungime);
  355. punct = v.getDest(punct, lungime);
  356. p2 = punct;
  357.  
  358. v.rotatie(30);
  359. v.deseneaza(punct, lungime);
  360. punct = v.getDest(punct, lungime);
  361. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  362.  
  363. punct = p2;
  364. v.rotatie(-90);
  365. v.deseneaza(punct, lungime);
  366. punct = v.getDest(punct, lungime);
  367. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  368. }
  369. }
  370.  
  371. void afisare(double lungime, int nivel)
  372. {
  373. CVector v(0.0, 1.0);
  374. CPunct p(0.0, -1.0);
  375.  
  376. v.deseneaza(p, 0.25);
  377. p = v.getDest(p, 0.25);
  378. arborePerron(lungime, nivel, 0.4, p, v);
  379. }
  380. };
  381.  
  382.  
  383. class CCurbaHilbert
  384. {
  385. public:
  386. void curbaHilbert(double lungime, int nivel, CPunct &p, CVector &v, int d)
  387. {
  388. if (nivel == 0)
  389. {
  390. }
  391. else
  392. {
  393. v.rotatie(d * 90);
  394. curbaHilbert(lungime, nivel - 1, p, v, -d);
  395.  
  396. v.deseneaza(p, lungime);
  397. p = v.getDest(p, lungime);
  398.  
  399. v.rotatie(-d * 90);
  400. curbaHilbert(lungime, nivel - 1, p, v, d);
  401.  
  402. v.deseneaza(p, lungime);
  403. p = v.getDest(p, lungime);
  404.  
  405. curbaHilbert(lungime, nivel - 1, p, v, d);
  406.  
  407. v.rotatie(-d * 90);
  408. v.deseneaza(p, lungime);
  409. p = v.getDest(p, lungime);
  410.  
  411. curbaHilbert(lungime, nivel - 1, p, v, -d);
  412.  
  413. v.rotatie(d * 90);
  414. }
  415. }
  416.  
  417. void afisare(double lungime, int nivel)
  418. {
  419. CVector v(0.0, 1.0);
  420. CPunct p(0.0, 0.0);
  421.  
  422. curbaHilbert(lungime, nivel, p, v, 1);
  423. }
  424. };
  425.  
  426. class CImagine1
  427. {
  428. public:
  429. void deseneaza_patrat(CPunct& p, double segment)
  430. {
  431. glColor3f(1, 0.1, 0.1);
  432. glBegin(GL_LINE_STRIP);
  433.  
  434. double x, y;
  435. p.getxy(x, y);
  436.  
  437. glVertex2f(x - segment / 2, y + segment / 2);
  438. glVertex2f(x + segment / 2, y + segment / 2);
  439. glVertex2f(x + segment / 2, y - segment / 2);
  440. glVertex2f(x - segment / 2, y - segment / 2);
  441. glVertex2f(x - segment / 2, y + segment / 2);
  442. glEnd();
  443. }
  444.  
  445. void deseneaza_recursiv(double lungime, int nivel, CPunct &punct)
  446. {
  447. deseneaza_patrat(punct, lungime);
  448.  
  449. if (nivel == 0)
  450. {
  451. }
  452. else
  453. {
  454. double _lungime = lungime / 3;
  455. double _distanta = lungime;
  456.  
  457. double x, y;
  458. punct.getxy(x, y);
  459.  
  460. punct = CPunct(x + _distanta, y);
  461. deseneaza_recursiv(_lungime, nivel - 1, punct);
  462.  
  463. punct = CPunct(x + _distanta, y + _distanta);
  464. deseneaza_recursiv(_lungime, nivel - 1, punct);
  465.  
  466. punct = CPunct(x - _distanta, y);
  467. deseneaza_recursiv(_lungime, nivel - 1, punct);
  468.  
  469. punct = CPunct(x - _distanta, y - _distanta);
  470. deseneaza_recursiv(_lungime, nivel - 1, punct);
  471.  
  472. punct = CPunct(x, y - _distanta);
  473. deseneaza_recursiv(_lungime, nivel - 1, punct);
  474.  
  475. punct = CPunct(x + _distanta, y - _distanta);
  476. deseneaza_recursiv(_lungime, nivel - 1, punct);
  477.  
  478. CPunct punct = CPunct(x, y + _distanta);
  479. deseneaza_recursiv(_lungime, nivel - 1, punct);
  480.  
  481. punct = CPunct(x - _distanta, y + _distanta);
  482. deseneaza_recursiv(_lungime, nivel - 1, punct);
  483. }
  484. }
  485.  
  486. void afisare(double lungime, int nivel)
  487. {
  488. CPunct punct(0.0, 0.0);
  489. deseneaza_recursiv(lungime, nivel, punct);
  490. }
  491. };
  492.  
  493. class CImagine2
  494. {
  495. public:
  496. void afisare(double lungime, int nivel)
  497. {
  498. CVector v(0.0, -1.0);
  499. CPunct p(0.0, 2.0);
  500.  
  501. v.deseneaza(p, 0.25);
  502. p = v.getDest(p, 0.25);
  503. arborePerron(lungime, nivel, 0.4, p, v);
  504. }
  505.  
  506. void arborePerron(double lungime,
  507. int nivel,
  508. double factordiviziune,
  509. CPunct p,
  510. CVector v)
  511. {
  512. if (nivel == 0)
  513. {
  514. return;
  515. }
  516.  
  517. assert(factordiviziune != 0);
  518. CPunct punct, p2;
  519.  
  520.  
  521. v.rotatie(-45);
  522. v.deseneaza(p, lungime);
  523. punct = v.getDest(p, lungime);
  524. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  525.  
  526. v.rotatie(90);
  527. v.deseneaza(p, lungime);
  528. punct = v.getDest(p, lungime);
  529. p2 = punct;
  530.  
  531. v.rotatie(15);
  532. v.deseneaza(punct, lungime);
  533. punct = v.getDest(punct, lungime);
  534. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  535.  
  536. punct = p2;
  537. v.rotatie(-60);
  538. v.deseneaza(punct, lungime);
  539. punct = v.getDest(punct, lungime);
  540. p2 = punct;
  541.  
  542. lungime *= (1 - factordiviziune);
  543.  
  544. v.rotatie(30);
  545. v.deseneaza(punct, lungime);
  546. punct = v.getDest(punct, lungime);
  547. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  548.  
  549. punct = p2;
  550. v.rotatie(-120);
  551. v.deseneaza(punct, lungime);
  552. punct = v.getDest(punct, lungime);
  553. arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
  554. }
  555. };
  556.  
  557. class CImagine3
  558. {
  559. public:
  560. void first_koch(double lungime, int nivel, CPunct &p, CVector v)
  561. {
  562. CPunct punct;
  563. if (nivel == 0)
  564. {
  565. v.deseneaza(p, lungime);
  566. }
  567. else
  568. {
  569. double scale = 2;
  570. punct = p;
  571. v.rotatie(-60);
  572. second_koch(lungime / scale, nivel - 1, punct, v);
  573.  
  574. punct = v.getDest(punct, lungime / scale);
  575. v.rotatie(60);
  576. first_koch(lungime / scale, nivel - 1, punct, v);
  577.  
  578. punct = v.getDest(punct, lungime / scale);
  579. v.rotatie(60);
  580. second_koch(lungime / scale, nivel - 1, punct, v);
  581. }
  582. }
  583.  
  584. void second_koch(double lungime, int nivel, CPunct &p, CVector v)
  585. {
  586. CPunct punct;
  587. if (nivel == 0)
  588. {
  589. v.deseneaza(p, lungime);
  590. }
  591. else
  592. {
  593. double scale = 2;
  594. punct = p;
  595. v.rotatie(60);
  596. first_koch(lungime / scale, nivel - 1, punct, v);
  597.  
  598. punct = v.getDest(punct, lungime / scale);
  599. v.rotatie(-60);
  600. second_koch(lungime / scale, nivel - 1, punct, v);
  601.  
  602. punct = v.getDest(punct, lungime / scale);
  603. v.rotatie(-60);
  604. first_koch(lungime / scale, nivel - 1, punct, v);
  605. }
  606. }
  607.  
  608. void afisare(double lungime, int nivel)
  609. {
  610. CVector v1(0, -2);
  611. CPunct punct(-0.9, 1.0);
  612. second_koch(2 * lungime, nivel, punct, v1);
  613. }
  614. };
  615.  
  616. class CMandelbrot {
  617. public:
  618. CMandelbrot()
  619. {
  620. m.max_mod = MODMAX_MAN;
  621. m.nr_iterations = NRITER_MAN;
  622. }
  623.  
  624. CMandelbrot(CComplex &c)
  625. {
  626. m.c = c;
  627. m.nr_iterations = NRITER_MAN;
  628. m.max_mod = MODMAX_MAN;
  629. }
  630.  
  631. void set_nr_iterations(int v)
  632. {
  633. m.nr_iterations = v;
  634. }
  635.  
  636. int is_inside(CComplex &c, int &iter, int nr_iter)
  637. {
  638. iter = -1;
  639.  
  640. int rezultat = 0;
  641. CComplex complex_first;
  642. CComplex complex_second;
  643.  
  644. for (int i = 1; i < nr_iter; i++)
  645. {
  646. complex_second = complex_first * complex_first + c;
  647.  
  648. if (complex_second.getModul() > m.max_mod)
  649. {
  650. rezultat = 1;
  651. break;
  652. }
  653. else if (complex_second.getModul() > 2)
  654. {
  655. rezultat = -1;
  656. break;
  657. }
  658.  
  659. iter = i;
  660. complex_first = complex_second;
  661. }
  662. return rezultat;
  663. }
  664.  
  665. double dr[10] = { 0.1, 0.3, 0.5, 0.7, 0.9, 1};
  666. double dg[10] = { 0.1, 0.3, 0.5, 0.7, 0.9, 1 };
  667. double db[10] = { 0.1, 0.3, 0.5, 0.7, 0.9, 1 };
  668.  
  669. void afisare(double xmin, double ymin, double xmax, double ymax)
  670. {
  671. glPushMatrix();
  672. glLoadIdentity();
  673.  
  674. int k = 0;
  675. for (int i = 0; i < m.nr_iterations; i++)
  676. {
  677. glBegin(GL_POINTS);
  678. for (double x = xmin; x <= xmax; x += RX_MAN)
  679. {
  680. for (double y = ymin; y <= ymax; y += RY_MAN)
  681. {
  682. int iter = -1;
  683. CComplex complex(x, y);
  684.  
  685. k++;
  686.  
  687. int rezultat = is_inside(complex, iter, i);
  688. if (rezultat == 0)
  689. {
  690. double scale_x = x / 2 + 0.25;
  691. double scale_y = y / 2;
  692. glColor3f(dr[iter % 6], db[(4 + 2 * iter) % 6], db[(2 + 3 * iter) % 6]);
  693. glVertex3d(scale_x, scale_y, 0);
  694. }
  695. }
  696. }
  697. glEnd();
  698. }
  699. glPopMatrix();
  700. }
  701.  
  702. private:
  703. struct SDate {
  704. CComplex c;
  705. // nr. de iteratii
  706. int nr_iterations;
  707. // modulul maxim
  708. double max_mod;
  709. } m;
  710. };
  711.  
  712.  
  713. // helper for info display
  714. void display_text(const char *name)
  715. {
  716. char c[3];
  717. sprintf(c, "%2d", nivel);
  718. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'v');
  719. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
  720. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'l');
  721. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '=');
  722. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[0]);
  723. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[1]);
  724.  
  725. glRasterPos2d(-1.0,-0.9);
  726. for (int i = 0; i < strlen(name); i++)
  727. {
  728. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, name[i]);
  729. }
  730. nivel++;
  731. }
  732.  
  733. // afisare curba lui Koch "fulg de zapada"
  734. void Display1() {
  735. CCurbaKoch cck;
  736. cck.afisare(sqrt(3.0), nivel);
  737. display_text("curba lui Koch");
  738. }
  739.  
  740. // afisare arbore binar
  741. void Display2() {
  742. CArboreBinar cab;
  743. cab.afisare(1, nivel);
  744. display_text("arbore binar");
  745. }
  746.  
  747. // afisare arborele lui Perron
  748. void Display3() {
  749. CArborePerron cap;
  750.  
  751. display_text("arbore Perron");
  752.  
  753. glPushMatrix();
  754. glLoadIdentity();
  755. glScaled(0.4, 0.4, 1);
  756. glTranslated(-0.5, -0.5, 0.0);
  757. cap.afisare(1, nivel);
  758. glPopMatrix();
  759. }
  760.  
  761. // afisare curba lui Hilbert
  762. void Display4() {
  763. CCurbaHilbert cch;
  764. cch.afisare(0.05, nivel);
  765. display_text("curba Hilbert");
  766. }
  767.  
  768. // afisare mandelbrot
  769. void Display5() {
  770. display_text("mandelbrot");
  771. glColor3f(1.0, 0.1, 0.1);
  772.  
  773. CComplex complex(0.0, 0.0);
  774. CMandelbrot mandelbrot(complex);
  775. mandelbrot.set_nr_iterations(nivel);
  776. mandelbrot.afisare(-2, -2, 2, 2);
  777. }
  778.  
  779. void Display6()
  780. {
  781. display_text("imagine 1 patrat");
  782. CImagine1 imagine1;
  783. imagine1.afisare(0.66, nivel);
  784. }
  785.  
  786. void Display7()
  787. {
  788. display_text("imagine 2 arbore");
  789.  
  790. glPushMatrix();
  791. glLoadIdentity();
  792. glScaled(0.4, 0.4, 1);
  793. glTranslated(-0.5, -0.5, 0.0);
  794.  
  795. CImagine2 imagine2;
  796. imagine2.afisare(1, nivel);
  797.  
  798. glPopMatrix();
  799. }
  800.  
  801. void Display8()
  802. {
  803. display_text("imagine 3 koch");
  804. CImagine3 imagine3;
  805. imagine3.afisare(1, nivel);
  806. }
  807.  
  808. void Init(void) {
  809.  
  810. glClearColor(1.0, 1.0, 1.0, 1.0);
  811.  
  812. glLineWidth(1);
  813.  
  814. glPointSize(1);
  815.  
  816. glPolygonMode(GL_FRONT, GL_LINE);
  817. }
  818.  
  819. void Display(void)
  820. {
  821. switch (prevKey)
  822. {
  823. case '0':
  824. glClear(GL_COLOR_BUFFER_BIT);
  825. nivel = 0;
  826. fprintf(stderr, "nivel = %d\n", nivel);
  827. break;
  828. case '1':
  829. glClear(GL_COLOR_BUFFER_BIT);
  830. Display1();
  831. break;
  832. case '2':
  833. glClear(GL_COLOR_BUFFER_BIT);
  834. Display2();
  835. break;
  836. case '3':
  837. glClear(GL_COLOR_BUFFER_BIT);
  838. Display3();
  839. break;
  840. case '4':
  841. glClear(GL_COLOR_BUFFER_BIT);
  842. Display4();
  843. break;
  844. case '5':
  845. glClear(GL_COLOR_BUFFER_BIT);
  846. Display5();
  847. break;
  848. case '6':
  849. glClear(GL_COLOR_BUFFER_BIT);
  850. Display6();
  851. break;
  852. case '7':
  853. glClear(GL_COLOR_BUFFER_BIT);
  854. Display7();
  855. break;
  856. case '8':
  857. glClear(GL_COLOR_BUFFER_BIT);
  858. Display8();
  859. break;
  860. default:
  861. break;
  862. }
  863.  
  864. glFlush();
  865. }
  866.  
  867. void Reshape(int w, int h)
  868. {
  869. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  870. }
  871.  
  872. void KeyboardFunc(unsigned char key, int x, int y)
  873. {
  874. prevKey = key;
  875. if (key == 27) // escape
  876. exit(0);
  877. glutPostRedisplay();
  878. }
  879.  
  880. void MouseFunc(int button, int state, int x, int y)
  881. {
  882. }
  883.  
  884. int main(int argc, char** argv)
  885. {
  886. glutInit(&argc, argv);
  887.  
  888. glutInitWindowSize(dim, dim);
  889.  
  890. glutInitWindowPosition(100, 100);
  891.  
  892. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  893.  
  894. glutCreateWindow(argv[0]);
  895.  
  896. Init();
  897.  
  898. glutReshapeFunc(Reshape);
  899.  
  900. glutKeyboardFunc(KeyboardFunc);
  901.  
  902. glutMouseFunc(MouseFunc);
  903.  
  904. glutDisplayFunc(Display);
  905.  
  906. glutMainLoop();
  907.  
  908. return 0;
  909. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement