openGPMP
Open Source Mathematics Package
Mandelbrot.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  *
3  * Project
4  * _____ _____ __ __ _____
5  * / ____| __ \| \/ | __ \
6  * ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
7  * / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
8  *| (_) | |_) | __/ | | | |__| | | | | | | |
9  * \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
10  * | |
11  * |_|
12  *
13  * Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
14  *
15  * This software is licensed as described in the file LICENSE, which
16  * you should have received as part of this distribution. The terms
17  * among other details are referenced in the official documentation
18  * seen here : https://akielaries.github.io/openGPMP/ along with
19  * important files seen in this project.
20  *
21  * You may opt to use, copy, modify, merge, publish, distribute
22  * and/or sell copies of the Software, and permit persons to whom
23  * the Software is furnished to do so, under the terms of the
24  * LICENSE file.
25  *
26  *
27  *
28  * This software is distributed on an AS IS basis, WITHOUT
29  * WARRANTY OF ANY KIND, either express or implied.
30  *
31  ************************************************************************/
32 
33 // C++ implementation for mandelbrot set fractals
34 #include <GL/glut.h>
35 #include <graphics.h>
36 #include <stdio.h>
37 
38 // #define MAXCOUNT 30
39 #define MAXCOUNT 100
40 
41 // Function to draw mandelbrot set
42 void fractal(float left, float top, float xside, float yside) {
43  float xscale, yscale, zx, zy, cx, tempx, cy;
44  int x, y, i, j;
45  int maxx, maxy, count;
46 
47  // getting maximum value of x-axis of screen
48  maxx = getmaxx();
49 
50  // getting maximum value of y-axis of screen
51  maxy = getmaxy();
52 
53  // setting up the xscale and yscale
54  xscale = xside / maxx;
55  yscale = yside / maxy;
56 
57  // calling rectangle function
58  // where required image will be seen
59  rectangle(0, 0, maxx, maxy);
60 
61  // scanning every point in that rectangular area.
62  // Each point represents a Complex number (x + yi).
63  // Iterate that complex number
64  for (y = 1; y <= maxy - 1; y++) {
65  for (x = 1; x <= maxx - 1; x++) {
66  // c_real
67  cx = x * xscale + left;
68 
69  // c_imaginary
70  cy = y * yscale + top;
71 
72  // z_real
73  zx = 0;
74 
75  // z_imaginary
76  zy = 0;
77  count = 0;
78 
79  // Calculate whether c(c_real + c_imaginary) belongs
80  // to the Mandelbrot set or not and draw a pixel
81  // at coordinates (x, y) accordingly
82  // If you reach the Maximum number of iterations
83  // and If the distance from the origin is
84  // greater than 2 exit the loop
85  while ((zx * zx + zy * zy < 4) && (count < MAXCOUNT)) {
86  // Calculate Mandelbrot function
87  // z = z*z + c where z is a complex number
88 
89  // tempx = z_real*_real - z_imaginary*z_imaginary +
90  // c_real
91  tempx = zx * zx - zy * zy + cx;
92 
93  // 2*z_real*z_imaginary + c_imaginary
94  zy = 2 * zx * zy + cy;
95 
96  // Updating z_real = tempx
97  zx = tempx;
98 
99  // Increment count
100  count = count + 1;
101  }
102 
103  // To display the created fractal
104  putpixel(x, y, count);
105  }
106  }
107 }
108 
109 // Driver code
110 int main() {
111  // gm is Graphics mode which is
112  // a computer display mode that
113  // generates image using pixels.
114  // DETECT is a macro defined in
115  // "graphics.h" header file
116  int gd = DETECT, gm, errorcode;
117 
118  float left, top, xside, yside;
119 
120  // setting the left, top, xside and yside
121  // for the screen and image to be displayed
122 
123  /*
124  left = -1;
125  top = 0;
126  xside = 0;
127  yside = 0.2;
128  */
129  left = -1.74;
130  top = -0.25;
131  xside = 0.25;
132  yside = 0.45;
133 
134  char driver[] = "";
135 
136  // initgraph initializes the
137  // graphics system by loading a
138  // graphics driver from disk
139  initgraph(&gd, &gm, driver);
140 
141  // Function calling
142  fractal(left, top, xside, yside);
143 
144  getch();
145 
146  // closegraph function closes the
147  // graphics mode and deallocates
148  // all memory allocated by
149  // graphics system
150  closegraph();
151 
152  return 0;
153 }
#define MAXCOUNT
Definition: Mandelbrot.cpp:39
void fractal(float left, float top, float xside, float yside)
Definition: Mandelbrot.cpp:42
int main()
Definition: Mandelbrot.cpp:110