Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #pragma warning(disable : 4996)
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <assert.h>
- #include <float.h>
- #include <GL/glut.h>
- // dimensiunea ferestrei in pixeli
- #define dim 300
- #define NRITER_MB 5000
- #define MODMAX_MB 10000000
- #define RX_MB 0.01
- #define RY_MB 0.01
- unsigned char prevKey, lastOption = 0;
- int nivel = 0;
- 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()
- {
- 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)) * 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 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 rez;
- rez.re = powl(re * 1.0, 2) - powl(im * 1.0, 2);
- rez.im = 2.0 * re * im;
- return rez;
- }
- 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 rez(c1.re + c2.re, c1.im + c2.im);
- return rez;
- }
- CComplex operator*(CComplex &c1, CComplex &c2)
- {
- CComplex rez(c1.re * c2.re - c1.im * c2.im,
- c1.re * c2.im + c1.im * c2.re);
- return rez;
- }
- class CMandelbrot
- {
- public:
- CMandelbrot()
- {
- m1.nriter = NRITER_MB;
- m1.modmax = MODMAX_MB;
- }
- CMandelbrot(CComplex &c)
- {
- m1.c = c;
- m1.nriter = NRITER_MB;
- m1.modmax = MODMAX_MB;
- m1.contor = 0;
- }
- ~CMandelbrot() {}
- void setmodmax(double v)
- {
- assert(v <= MODMAX_MB);
- m1.modmax = v;
- }
- double getmodmax() { return m1.modmax; }
- void setnriter(int v)
- {
- assert(v <= NRITER_MB);
- m1.nriter = v;
- }
- int getnriter() { return m1.nriter; }
- int isInMandelbrot(CComplex &x)
- {
- int rez = -1;
- CComplex z0(0.0, 0.0);
- int i = 0;
- CComplex z1;
- while (i < 10 && z1.getModul() <= 2.0)
- {
- z1 = z0 * z0 + x;
- z0 = z1;
- i++;
- }
- if (z1.getModul() <= 2.0)
- rez = 0;
- else
- rez = i;
- return rez;
- }
- void displayMandelbrout(double xmin, double ymin, double xmax, double ymax)
- {
- glPushMatrix();
- glLoadIdentity();
- glBegin(GL_POINTS);
- for (double x = xmin; x <= xmax; x += RX_MB)
- for (double y = ymin; y <= ymax; y += RY_MB)
- {
- CComplex c(x, y);
- int r = isInMandelbrot(c);
- if (r == 0)
- {
- glColor3f(1, 0.1, 0.1);
- glVertex3d(x / 2.0, y / 2.0, 0);
- }
- else
- {
- glColor3f(0.1 * r, 0, 0.1 * r);
- glVertex3d(x / 2.0, y / 2.0, 0);
- }
- }
- fprintf(stdout, "STOP\n");
- glEnd();
- glPopMatrix();
- }
- private:
- struct SDate
- {
- CComplex c;
- int nriter;
- double modmax;
- int contor;
- } m1;
- };
- class CImagine1
- {
- public:
- void imagine1(double l, int nivel, CPunct p)
- {
- if (nivel == 0)
- {
- CVector v(-1, 1);
- l /= 3;
- p = v.getDest(p, l * sqrt((double)2) / 2.0);
- v.rotatie(-135);
- for (int i = 0; i < 4; ++i)
- {
- v.deseneaza(p, l);
- p = v.getDest(p, l);
- v.rotatie(-90);
- }
- return;
- }
- imagine1(l, 0, p);
- CVector v(-1, -1);
- for (int i = 0; i < 8; ++i)
- {
- CPunct p1 = v.getDest(p, (i % 2 ? l : l * sqrt((double)2)) / 3.0);
- imagine1(l / 3, nivel - 1, p1);
- v.rotatie(45);
- }
- }
- void afisare(double lungime, int nivel)
- {
- CPunct p(0.0, 0.0);
- if (nivel > 0)
- imagine1(lungime * 3, 0, p);
- imagine1(lungime, nivel, p);
- }
- };
- class CImagine2
- {
- public:
- void imagine2(double lungime,
- int nivel,
- double factordiviziune,
- CPunct p,
- CVector v)
- {
- if (factordiviziune != 0)
- {
- CPunct p1, p2;
- if (nivel != 0)
- {
- v.rotatie(-45);
- v.deseneaza(p, lungime);
- p1 = v.getDest(p, lungime);
- imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
- v.rotatie(90);
- v.deseneaza(p, lungime);
- p1 = v.getDest(p, lungime);
- p2 = p1;
- v.rotatie(15);
- v.deseneaza(p1, lungime);
- p1 = v.getDest(p1, lungime);
- imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
- p1 = p2;
- v.rotatie(-60);
- v.deseneaza(p1, lungime);
- p1 = v.getDest(p1, lungime);
- p2 = p1;
- v.rotatie(-90);
- v.deseneaza(p1, lungime / 2);
- p1 = v.getDest(p1, lungime / 2);
- imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, v);
- p1 = p2;
- v.rotatie(120);
- v.deseneaza(p1, lungime / 2);
- p1 = v.getDest(p1, lungime / 2);
- imagine2(lungime * factordiviziune, nivel - 1, factordiviziune, p1, 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);
- imagine2(lungime, nivel, 0.4, p, v);
- }
- };
- class CImagine3
- {
- public:
- void imagine3(double lungime, int nivel, int unghi, CPunct &p, CVector &v)
- {
- if (nivel == 0)
- {
- v.deseneaza(p, lungime);
- p = v.getDest(p, lungime);
- }
- else
- {
- for (int i = 0; i < 3; i++)
- {
- imagine3(lungime / 2, nivel - 1, (i % 2) ? unghi : -unghi, p, v);
- if (i != 2)
- v.rotatie(unghi);
- }
- }
- }
- void afisare(double lungime, int nivel)
- {
- CVector v(0.0, 1.0);
- CPunct p(-0.5, -0.5);
- imagine3(lungime, nivel, (nivel % 2) ? -60 : 60, p, v);
- }
- };
- void Display1()
- {
- CComplex c;
- CMandelbrot cm(c);
- glColor3f(1.0, 0.1, 0.1);
- cm.setnriter(30);
- cm.displayMandelbrout(-2, -2, 2, 2);
- }
- void Display2()
- {
- CImagine1 cap;
- char c[3];
- sprintf(c, "%2d", nivel);
- glRasterPos2d(-0.98, -0.98);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- 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.93);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'm');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'g');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'n');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ' ');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '1');
- cap.afisare(1.0, nivel);
- nivel++;
- }
- void Display3()
- {
- CImagine2 cap;
- char c[3];
- sprintf(c, "%2d", nivel);
- glRasterPos2d(-0.98, -0.98);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- 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.93);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'm');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'g');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'n');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ' ');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '2');
- glPushMatrix();
- glLoadIdentity();
- glScaled(0.4, 0.4, 1);
- glTranslated(-0.5, -0.5, 0.0);
- cap.afisare(1, nivel);
- glPopMatrix();
- nivel++;
- }
- void Display4()
- {
- CImagine3 cap;
- char c[3];
- sprintf(c, "%2d", nivel);
- glRasterPos2d(-0.98, -0.98);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'N');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- 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.93);
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'm');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'g');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'i');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'n');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'e');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, 'a');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ' ');
- glutBitmapCharacter(GLUT_BITMAP_9_BY_15, '3');
- cap.afisare(1.0, nivel);
- nivel++;
- }
- void Init(void)
- {
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glLineWidth(1);
- glPolygonMode(GL_FRONT, GL_LINE);
- }
- void Display(void)
- {
- if (prevKey != lastOption)
- nivel = 0;
- switch (prevKey)
- {
- case '0':
- glClear(GL_COLOR_BUFFER_BIT);
- prevKey = lastOption;
- nivel = 0;
- fprintf(stderr, "nivel = %d\n", nivel);
- Display();
- 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;
- default:
- break;
- }
- lastOption = prevKey;
- 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