openGPMP
Open Source Mathematics Package
Mandelbrot.c
Go to the documentation of this file.
1 // C++ implementation for mandelbrot set fractals
2 #include <GL/glut.h>
3 #include <graphics.h>
4 #include <stdio.h>
5 
6 // #define MAXCOUNT 30
7 #define MAXCOUNT 100
8 
9 // Function to draw mandelbrot set
10 void fractal(float left, float top, float xside, float yside) {
11  glClear(GL_COLOR_BUFFER_BIT);
12  glBegin(GL_POINTS);
13 
14  float xscale, yscale, zx, zy, cx, tempx, cy;
15  int x, y, i, j;
16  int maxx, maxy, count;
17 
18  // getting maximum value of x-axis of screen
19  maxx = getmaxx();
20 
21  // getting maximum value of y-axis of screen
22  maxy = getmaxy();
23 
24  // setting up the xscale and yscale
25  xscale = xside / maxx;
26  yscale = yside / maxy;
27 
28  // calling rectangle function
29  // where required image will be seen
30  rectangle(0, 0, maxx, maxy);
31 
32  // scanning every point in that rectangular area.
33  // Each point represents a Complex number (x + yi).
34  // Iterate that complex number
35  for (y = 1; y <= maxy - 1; y++) {
36  for (x = 1; x <= maxx - 1; x++) {
37  // c_real
38  cx = x * xscale + left;
39 
40  // c_imaginary
41  cy = y * yscale + top;
42 
43  // z_real
44  zx = 0;
45 
46  // z_imaginary
47  zy = 0;
48  count = 0;
49 
50  // Calculate whether c(c_real + c_imaginary) belongs
51  // to the Mandelbrot set or not and draw a pixel
52  // at coordinates (x, y) accordingly
53  // If you reach the Maximum number of iterations
54  // and If the distance from the origin is
55  // greater than 2 exit the loop
56  while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT)) {
57  // Calculate Mandelbrot function
58  // z = z*z + c where z is a complex number
59 
60  // tempx = z_real*_real - z_imaginary*z_imaginary +
61  // c_real
62  tempx = zx * zx - zy * zy + cx;
63 
64  // 2*z_real*z_imaginary + c_imaginary
65  zy = 2 * zx * zy + cy;
66 
67  // Updating z_real = tempx
68  zx = tempx;
69 
70  // Increment count
71  count = count + 1;
72  }
73 
74  // To display the created fractal
75  putpixel(x, y, count);
76  }
77  }
78  glEnd();
79  glFlush();
80 }
81 
82 void display() {
83  float left, top, xside, yside;
84 
85  left = -1.74;
86  top = -0.25;
87  xside = 0.25;
88  yside = 0.45;
89 
90  fractal(left, top, xside, yside);
91 }
92 
93 // Driver code
94 int main(int argc, char **argv) {
95  // gm is Graphics mode which is
96  // a computer display mode that
97  // generates image using pixels.
98  // DETECT is a macro defined in
99  // "graphics.h" header file
100  int gd = DETECT, gm, errorcode;
101 
102  float left, top, xside, yside;
103 
104  // setting the left, top, xside and yside
105  // for the screen and image to be displayed
106 
107  /*
108  left = -1;
109  top = 0;
110  xside = 0;
111  yside = 0.2;
112  */
113  left = -1.74;
114  top = -0.25;
115  xside = 0.25;
116  yside = 0.45;
117 
118  char driver[] = "";
119 
120  // initgraph initializes the
121  // graphics system by loading a
122  // graphics driver from disk
123  initgraph(&gd, &gm, driver);
124  // glutInit(&gd, driver);
125  glutInit(&argc, argv);
126  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
127  glutInitWindowSize(1366, 768);
128  glutInitWindowPosition(0, 0);
129 
130  // Function calling
131  // fractal(left, top, xside, yside);
132 
133  // getch();
134 
135  // closegraph function closes the
136  // graphics mode and deallocates
137  // all memory allocated by
138  // graphics system
139  // closegraph();
140  glutCreateWindow("Mandelbrot Fractals with openGL");
141  glutDisplayFunc(display);
142  glutMainLoop();
143 
144  getch();
145 
146  return 0;
147 }
void display()
Definition: Mandelbrot.c:82
#define MAXCOUNT
Definition: Mandelbrot.c:7
void fractal(float left, float top, float xside, float yside)
Definition: Mandelbrot.c:10
int main(int argc, char **argv)
Definition: Mandelbrot.c:94