Advertisement
Guest User

Untitled

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