Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #pragma warning(disable: 4996)
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <assert.h>
- #include <float.h>
- #include <iostream>
- #include "glut.h"
- // dimensiunea ferestrei in pixeli
- #define dim 300
- #define NRITER_MAN 3000
- #define MODMAX_MAN 10000000
- #define RX_MAN 0.01
- #define RY_MAN 0.01
- unsigned char prevKey;
- int nivel = 0;
- class CComplex {
- public:
- CComplex() : re(0.0), im(0.0) {}
- CComplex(double re1, double im1) : re(re1 * 1.0), im(im1 * 1.0) {}
- CComplex(const CComplex &c) : re(c.re), im(c.im) {}
- ~CComplex() {}
- CComplex &operator=(const CComplex &c)
- {
- re = c.re;
- im = c.im;
- return *this;
- }
- double getRe() { return re; }
- void setRe(double re1) { re = re1; }
- double getIm() { return im; }
- void setIm(double im1) { im = im1; }
- double getModul() { return sqrt(re * re + im * im); }
- int operator==(CComplex &c1)
- {
- return ((re == c1.re) && (im == c1.im));
- }
- CComplex pow2()
- {
- CComplex rezultat;
- rezultat.re = powl(re * 1.0, 2) - powl(im * 1.0, 2);
- rezultat.im = 2.0 * re * im;
- return rezultat;
- }
- friend CComplex operator+(const CComplex &c1, const CComplex &c2);
- friend CComplex operator*(CComplex &c1, CComplex &c2);
- void print(FILE *f)
- {
- fprintf(f, "%.20f%+.20f i", re, im);
- }
- private:
- double re, im;
- };
- CComplex operator+(const CComplex &c1, const CComplex &c2)
- {
- CComplex rezultat(c1.re + c2.re, c1.im + c2.im);
- return rezultat;
- }
- CComplex operator*(CComplex &c1, CComplex &c2)
- {
- CComplex rezultat(c1.re * c2.re - c1.im * c2.im,
- c1.re * c2.im + c1.im * c2.re);
- return rezultat;
- }
- class C2coord
- {
- public:
- C2coord()
- {
- m.x = m.y = 0;
- }
- C2coord(double x, double y)
- {
- m.x = x;
- m.y = y;
- }
- C2coord(const C2coord &p)
- {
- m.x = p.m.x;
- m.y = p.m.y;
- }
- C2coord &operator=(C2coord &p)
- {
- m.x = p.m.x;
- m.y = p.m.y;
- return *this;
- }
- int operator==(C2coord &p)
- {
- return ((m.x == p.m.x) && (m.y == p.m.y));
- }
- protected:
- struct SDate
- {
- double x, y;
- } m;
- };
- class CPunct : public C2coord
- {
- public:
- CPunct() : C2coord(0.0, 0.0)
- {}
- CPunct(double x, double y) : C2coord(x, y)
- {}
- CPunct &operator=(const CPunct &p)
- {
- m.x = p.m.x;
- m.y = p.m.y;
- return *this;
- }
- void getxy(double &x, double &y)
- {
- x = m.x;
- y = m.y;
- }
- int operator==(CPunct &p)
- {
- return ((m.x == p.m.x) && (m.y == p.m.y));
- }
- void marcheaza()
- {
- glColor3f(1.0, 0.1, 0.1);
- glBegin(GL_POINTS);
- glVertex2d(m.x, m.y);
- glEnd();
- }
- void print(FILE *fis)
- {
- fprintf(fis, "(%+f,%+f)", m.x, m.y);
- }
- };
- class CVector : public C2coord
- {
- public:
- CVector() : C2coord(0.0, 0.0)
- {
- normalizare();
- }
- CVector(double x, double y) : C2coord(x, y)
- {
- normalizare();
- }
- CVector &operator=(CVector &p)
- {
- m.x = p.m.x;
- m.y = p.m.y;
- return *this;
- }
- int operator==(CVector &p)
- {
- return ((m.x == p.m.x) && (m.y == p.m.y));
- }
- CPunct getDest(CPunct &orig, double lungime)
- {
- double x, y;
- orig.getxy(x, y);
- CPunct p(x + m.x * lungime, y + m.y * lungime);
- return p;
- }
- void rotatie(double grade)
- {
- double x = m.x;
- double y = m.y;
- double t = 2 * (4.0 * atan(1.0)) * grade / 360.0;
- m.x = x * cos(t) - y * sin(t);
- m.y = x * sin(t) + y * cos(t);
- normalizare();
- }
- void deseneaza(CPunct p, double lungime)
- {
- double x, y;
- p.getxy(x, y);
- glColor3f(1.0, 0.1, 0.1);
- glBegin(GL_LINE_STRIP);
- glVertex2d(x, y);
- glVertex2d(x + m.x * lungime, y + m.y * lungime);
- glEnd();
- }
- void print(FILE *fis)
- {
- fprintf(fis, "%+fi %+fj", C2coord::m.x, C2coord::m.y);
- }
- private:
- void normalizare()
- {
- double d = sqrt(C2coord::m.x * C2coord::m.x + C2coord::m.y * C2coord::m.y);
- if (d != 0.0)
- {
- C2coord::m.x = C2coord::m.x * 1.0 / d;
- C2coord::m.y = C2coord::m.y * 1.0 / d;
- }
- }
- };
- class CCurbaKoch
- {
- public:
- void segmentKoch(double lungime, int nivel, CPunct &p, CVector v)
- {
- CPunct punct;
- if (nivel == 0)
- {
- v.deseneaza(p, lungime);
- }
- else
- {
- // v.print(stderr);
- // fprintf(stderr, "\n");
- segmentKoch(lungime / 3.0, nivel - 1, p, v);
- punct = v.getDest(p, lungime / 3.0);
- v.rotatie(60);
- // v.print(stderr);
- // fprintf(stderr, "\n");
- segmentKoch(lungime / 3.0, nivel - 1, punct, v);
- punct = v.getDest(punct, lungime / 3.0);
- v.rotatie(-120);
- // v.print(stderr);
- // fprintf(stderr, "\n");
- segmentKoch(lungime / 3.0, nivel - 1, punct, v);
- punct = v.getDest(punct, lungime / 3.0);
- v.rotatie(60);
- // v.print(stderr);
- // fprintf(stderr, "\n");
- segmentKoch(lungime / 3.0, nivel - 1, punct, v);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CVector v1(sqrt(3.0) / 2.0, 0.5);
- CPunct punct(-1.0, 0.0);
- CVector v2(0.0, -1.0);
- CPunct p2(0.5, sqrt(3.0) / 2.0);
- CVector v3(-sqrt(3.0) / 2.0, 0.5);
- CPunct p3(0.5, -sqrt(3.0) / 2.0);
- segmentKoch(lungime, nivel, punct, v1);
- segmentKoch(lungime, nivel, p2, v2);
- segmentKoch(lungime, nivel, p3, v3);
- }
- };
- class CArboreBinar
- {
- public:
- void arboreBinar(double lungime, int nivel, CPunct &p, CVector v)
- {
- CPunct punct;
- if (nivel == 0)
- {
- v.deseneaza(p, lungime);
- }
- else
- {
- arboreBinar(lungime, nivel - 1, p, v);
- punct = v.getDest(p, lungime);
- v.rotatie(-45);
- arboreBinar(lungime / 2.0, nivel - 1, punct, v);
- v.rotatie(90);
- arboreBinar(lungime / 2.0, nivel - 1, punct, v);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CVector v(0.0, -1.0);
- CPunct p(0.0, 1.0);
- arboreBinar(lungime, nivel, p, v);
- }
- };
- class CArborePerron
- {
- public:
- void arborePerron(double lungime,
- int nivel,
- double factordiviziune,
- CPunct p,
- CVector v)
- {
- assert(factordiviziune != 0);
- CPunct punct, p2;
- if (nivel == 0)
- {
- }
- else
- {
- v.rotatie(30);
- v.deseneaza(p, lungime);
- punct = v.getDest(p, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- v.rotatie(-90);
- v.deseneaza(p, lungime);
- punct = v.getDest(p, lungime);
- p2 = punct;
- v.rotatie(-30);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- punct = p2;
- v.rotatie(90);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- p2 = punct;
- v.rotatie(30);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- punct = p2;
- v.rotatie(-90);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CVector v(0.0, 1.0);
- CPunct p(0.0, -1.0);
- v.deseneaza(p, 0.25);
- p = v.getDest(p, 0.25);
- arborePerron(lungime, nivel, 0.4, p, v);
- }
- };
- class CCurbaHilbert
- {
- public:
- void curbaHilbert(double lungime, int nivel, CPunct &p, CVector &v, int d)
- {
- if (nivel == 0)
- {
- }
- else
- {
- v.rotatie(d * 90);
- curbaHilbert(lungime, nivel - 1, p, v, -d);
- v.deseneaza(p, lungime);
- p = v.getDest(p, lungime);
- v.rotatie(-d * 90);
- curbaHilbert(lungime, nivel - 1, p, v, d);
- v.deseneaza(p, lungime);
- p = v.getDest(p, lungime);
- curbaHilbert(lungime, nivel - 1, p, v, d);
- v.rotatie(-d * 90);
- v.deseneaza(p, lungime);
- p = v.getDest(p, lungime);
- curbaHilbert(lungime, nivel - 1, p, v, -d);
- v.rotatie(d * 90);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CVector v(0.0, 1.0);
- CPunct p(0.0, 0.0);
- curbaHilbert(lungime, nivel, p, v, 1);
- }
- };
- class CImagine1
- {
- public:
- void deseneaza_patrat(CPunct& p, double segment)
- {
- glColor3f(1, 0.1, 0.1);
- glBegin(GL_LINE_STRIP);
- double x, y;
- p.getxy(x, y);
- glVertex2f(x - segment / 2, y + segment / 2);
- glVertex2f(x + segment / 2, y + segment / 2);
- glVertex2f(x + segment / 2, y - segment / 2);
- glVertex2f(x - segment / 2, y - segment / 2);
- glVertex2f(x - segment / 2, y + segment / 2);
- glEnd();
- }
- void deseneaza_recursiv(double lungime, int nivel, CPunct &punct)
- {
- deseneaza_patrat(punct, lungime);
- if (nivel == 0)
- {
- }
- else
- {
- double _lungime = lungime / 3;
- double _distanta = lungime;
- double x, y;
- punct.getxy(x, y);
- punct = CPunct(x + _distanta, y);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- punct = CPunct(x + _distanta, y + _distanta);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- punct = CPunct(x - _distanta, y);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- punct = CPunct(x - _distanta, y - _distanta);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- punct = CPunct(x, y - _distanta);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- punct = CPunct(x + _distanta, y - _distanta);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- CPunct punct = CPunct(x, y + _distanta);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- punct = CPunct(x - _distanta, y + _distanta);
- deseneaza_recursiv(_lungime, nivel - 1, punct);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CPunct punct(0.0, 0.0);
- deseneaza_recursiv(lungime, nivel, punct);
- }
- };
- class CImagine2
- {
- public:
- void afisare(double lungime, int nivel)
- {
- CVector v(0.0, -1.0);
- CPunct p(0.0, 2.0);
- v.deseneaza(p, 0.25);
- p = v.getDest(p, 0.25);
- arborePerron(lungime, nivel, 0.4, p, v);
- }
- void arborePerron(double lungime,
- int nivel,
- double factordiviziune,
- CPunct p,
- CVector v)
- {
- if (nivel == 0)
- {
- return;
- }
- assert(factordiviziune != 0);
- CPunct punct, p2;
- v.rotatie(-45);
- v.deseneaza(p, lungime);
- punct = v.getDest(p, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- v.rotatie(90);
- v.deseneaza(p, lungime);
- punct = v.getDest(p, lungime);
- p2 = punct;
- v.rotatie(15);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- punct = p2;
- v.rotatie(-60);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- p2 = punct;
- lungime *= (1 - factordiviziune);
- v.rotatie(30);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- punct = p2;
- v.rotatie(-120);
- v.deseneaza(punct, lungime);
- punct = v.getDest(punct, lungime);
- arborePerron(lungime * factordiviziune, nivel - 1, factordiviziune, punct, v);
- }
- };
- class CImagine3
- {
- public:
- void first_koch(double lungime, int nivel, CPunct &p, CVector v)
- {
- CPunct punct;
- if (nivel == 0)
- {
- v.deseneaza(p, lungime);
- }
- else
- {
- double scale = 2;
- punct = p;
- v.rotatie(-60);
- second_koch(lungime / scale, nivel - 1, punct, v);
- punct = v.getDest(punct, lungime / scale);
- v.rotatie(60);
- first_koch(lungime / scale, nivel - 1, punct, v);
- punct = v.getDest(punct, lungime / scale);
- v.rotatie(60);
- second_koch(lungime / scale, nivel - 1, punct, v);
- }
- }
- void second_koch(double lungime, int nivel, CPunct &p, CVector v)
- {
- CPunct punct;
- if (nivel == 0)
- {
- v.deseneaza(p, lungime);
- }
- else
- {
- double scale = 2;
- punct = p;
- v.rotatie(60);
- first_koch(lungime / scale, nivel - 1, punct, v);
- punct = v.getDest(punct, lungime / scale);
- v.rotatie(-60);
- second_koch(lungime / scale, nivel - 1, punct, v);
- punct = v.getDest(punct, lungime / scale);
- v.rotatie(-60);
- first_koch(lungime / scale, nivel - 1, punct, v);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CVector v1(0, -2);
- CPunct punct(-0.9, 1.0);
- second_koch(2 * lungime, nivel, punct, v1);
- }
- };
- class CMandelbrot {
- public:
- CMandelbrot()
- {
- m.max_mod = MODMAX_MAN;
- m.nr_iterations = NRITER_MAN;
- }
- CMandelbrot(CComplex &c)
- {
- m.c = c;
- m.nr_iterations = NRITER_MAN;
- m.max_mod = MODMAX_MAN;
- }
- void set_nr_iterations(int v)
- {
- m.nr_iterations = v;
- }
- int is_inside(CComplex &c, int &iter, int nr_iter)
- {
- iter = -1;
- int rezultat = 0;
- CComplex complex_first;
- CComplex complex_second;
- for (int i = 1; i < nr_iter; i++)
- {
- complex_second = complex_first * complex_first + c;
- if (complex_second.getModul() > m.max_mod)
- {
- rezultat = 1;
- break;
- }
- else if (complex_second.getModul() > 2)
- {
- rezultat = -1;
- break;
- }
- iter = i;
- complex_first = complex_second;
- }
- return rezultat;
- }
- double dr[10] = { 0.1, 0.3, 0.5, 0.7, 0.9, 1};
- double dg[10] = { 0.1, 0.3, 0.5, 0.7, 0.9, 1 };
- double db[10] = { 0.1, 0.3, 0.5, 0.7, 0.9, 1 };
- void afisare(double xmin, double ymin, double xmax, double ymax)
- {
- glPushMatrix();
- glLoadIdentity();
- int k = 0;
- for (int i = 0; i < m.nr_iterations; i++)
- {
- glBegin(GL_POINTS);
- for (double x = xmin; x <= xmax; x += RX_MAN)
- {
- for (double y = ymin; y <= ymax; y += RY_MAN)
- {
- int iter = -1;
- CComplex complex(x, y);
- k++;
- int rezultat = is_inside(complex, iter, i);
- if (rezultat == 0)
- {
- double scale_x = x / 2 + 0.25;
- double scale_y = y / 2;
- glColor3f(dr[iter % 6], db[(4 + 2 * iter) % 6], db[(2 + 3 * iter) % 6]);
- glVertex3d(scale_x, scale_y, 0);
- }
- }
- }
- glEnd();
- }
- glPopMatrix();
- }
- private:
- struct SDate {
- CComplex c;
- // nr. de iteratii
- int nr_iterations;
- // modulul maxim
- double max_mod;
- } m;
- };
- // helper for info display
- void display_text(const char *name)
- {
- char c[3];
- sprintf(c, "%2d", nivel);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'v');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'l');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '=');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[0]);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c[1]);
- glRasterPos2d(-1.0,-0.9);
- for (int i = 0; i < strlen(name); i++)
- {
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, name[i]);
- }
- nivel++;
- }
- // afisare curba lui Koch "fulg de zapada"
- void Display1() {
- CCurbaKoch cck;
- cck.afisare(sqrt(3.0), nivel);
- display_text("curba lui Koch");
- }
- // afisare arbore binar
- void Display2() {
- CArboreBinar cab;
- cab.afisare(1, nivel);
- display_text("arbore binar");
- }
- // afisare arborele lui Perron
- void Display3() {
- CArborePerron cap;
- display_text("arbore Perron");
- glPushMatrix();
- glLoadIdentity();
- glScaled(0.4, 0.4, 1);
- glTranslated(-0.5, -0.5, 0.0);
- cap.afisare(1, nivel);
- glPopMatrix();
- }
- // afisare curba lui Hilbert
- void Display4() {
- CCurbaHilbert cch;
- cch.afisare(0.05, nivel);
- display_text("curba Hilbert");
- }
- // afisare mandelbrot
- void Display5() {
- display_text("mandelbrot");
- glColor3f(1.0, 0.1, 0.1);
- CComplex complex(0.0, 0.0);
- CMandelbrot mandelbrot(complex);
- mandelbrot.set_nr_iterations(nivel);
- mandelbrot.afisare(-2, -2, 2, 2);
- }
- void Display6()
- {
- display_text("imagine 1 patrat");
- CImagine1 imagine1;
- imagine1.afisare(0.66, nivel);
- }
- void Display7()
- {
- display_text("imagine 2 arbore");
- glPushMatrix();
- glLoadIdentity();
- glScaled(0.4, 0.4, 1);
- glTranslated(-0.5, -0.5, 0.0);
- CImagine2 imagine2;
- imagine2.afisare(1, nivel);
- glPopMatrix();
- }
- void Display8()
- {
- display_text("imagine 3 koch");
- CImagine3 imagine3;
- imagine3.afisare(1, nivel);
- }
- void Init(void) {
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glLineWidth(1);
- glPointSize(1);
- glPolygonMode(GL_FRONT, GL_LINE);
- }
- void Display(void)
- {
- switch (prevKey)
- {
- case '0':
- glClear(GL_COLOR_BUFFER_BIT);
- nivel = 0;
- fprintf(stderr, "nivel = %d\n", nivel);
- break;
- case '1':
- glClear(GL_COLOR_BUFFER_BIT);
- Display1();
- break;
- case '2':
- glClear(GL_COLOR_BUFFER_BIT);
- Display2();
- break;
- case '3':
- glClear(GL_COLOR_BUFFER_BIT);
- Display3();
- break;
- case '4':
- glClear(GL_COLOR_BUFFER_BIT);
- Display4();
- break;
- case '5':
- glClear(GL_COLOR_BUFFER_BIT);
- Display5();
- break;
- case '6':
- glClear(GL_COLOR_BUFFER_BIT);
- Display6();
- break;
- case '7':
- glClear(GL_COLOR_BUFFER_BIT);
- Display7();
- break;
- case '8':
- glClear(GL_COLOR_BUFFER_BIT);
- Display8();
- break;
- default:
- break;
- }
- glFlush();
- }
- void Reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- }
- void KeyboardFunc(unsigned char key, int x, int y)
- {
- prevKey = key;
- if (key == 27) // escape
- exit(0);
- glutPostRedisplay();
- }
- void MouseFunc(int button, int state, int x, int y)
- {
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitWindowSize(dim, dim);
- glutInitWindowPosition(100, 100);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutCreateWindow(argv[0]);
- Init();
- glutReshapeFunc(Reshape);
- glutKeyboardFunc(KeyboardFunc);
- glutMouseFunc(MouseFunc);
- glutDisplayFunc(Display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement