-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmandelbrot.cpp
190 lines (164 loc) · 5.25 KB
/
mandelbrot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
*
* File: mandelbrot.cpp
* Author: Dany Shaanan
* Website: http://danyshaanan.com
* File location: https://github.com/danyshaanan/mandelbrot/blob/master/cpp/mandelbrot.cpp
*
* Created somewhen between 1999 and 2002
* Rewritten August 2013
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <math.h>
////////////////////////////////////////////////////////////////////////////////
const int MAX_WIDTH_HEIGHT = 2000;
const int HUE_PER_ITERATION = 5;
const bool DRAW_ON_KEY = true;
////////////////////////////////////////////////////////////////////////////////
class State {
public:
double centerX;
double centerY;
double zoom;
int maxIterations;
int w;
int h;
State() {
centerX = -1.186340599860225;
centerY = -0.303652988644423;
zoom = 400;
maxIterations = 100;
w = 700;
h = 700;
}
void moveY(int change) {
centerY += change / zoom;
}
void moveX(int change) {
centerX += change / zoom;
}
void zoomBy(double r) {
zoom *= r;
}
void addIterations(int i) {
maxIterations += i;
}
};
////////////////////////////////////////////////////////////////////////////////
float iterationsToEscape(double x, double y, int maxIterations) {
double tempa;
double a = 0;
double b = 0;
for (int i = 0 ; i < maxIterations ; i++) {
tempa = a*a - b*b + x;
b = 2*a*b + y;
a = tempa;
if (a*a+b*b > 64) {
// return i; // discrete
return i - log(sqrt(a*a+b*b))/log(8); //continuous
}
}
return -1;
}
int hue2rgb(float t){
while (t>360) {
t -= 360;
}
if (t < 60) return 255.*t/60.;
if (t < 180) return 255;
if (t < 240) return 255. * (4. - t/60.);
return 0;
}
void writeImage(unsigned char *img, int w, int h) {
int filesize = 54 + 3*w*h;
unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
unsigned char bmppad[3] = {0,0,0};
bmpfileheader[ 2] = (unsigned char)(filesize );
bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
bmpfileheader[ 4] = (unsigned char)(filesize>>16);
bmpfileheader[ 5] = (unsigned char)(filesize>>24);
bmpinfoheader[ 4] = (unsigned char)( w );
bmpinfoheader[ 5] = (unsigned char)( w>> 8);
bmpinfoheader[ 6] = (unsigned char)( w>>16);
bmpinfoheader[ 7] = (unsigned char)( w>>24);
bmpinfoheader[ 8] = (unsigned char)( h );
bmpinfoheader[ 9] = (unsigned char)( h>> 8);
bmpinfoheader[10] = (unsigned char)( h>>16);
bmpinfoheader[11] = (unsigned char)( h>>24);
FILE *f;
f = fopen("temp.bmp","wb");
fwrite(bmpfileheader,1,14,f);
fwrite(bmpinfoheader,1,40,f);
for (int i=0; i<h; i++) {
fwrite(img+(w*(h-i-1)*3),3,w,f);
fwrite(bmppad,1,(4-(w*3)%4)%4,f);
}
fclose(f);
}
unsigned char *createImage(State state) {
int w = state.w;
int h = state.h;
if (w > MAX_WIDTH_HEIGHT) w = MAX_WIDTH_HEIGHT;
if (h > MAX_WIDTH_HEIGHT) h = MAX_WIDTH_HEIGHT;
unsigned char r, g, b;
unsigned char *img = NULL;
if (img) free(img);
img = (unsigned char *)malloc(3*w*h);
double xs[MAX_WIDTH_HEIGHT], ys[MAX_WIDTH_HEIGHT];
for (int px=0; px<w; px++) {
xs[px] = (px - w/2)/state.zoom + state.centerX;
}
for (int py=0; py<h; py++) {
ys[py] = (py - h/2)/state.zoom + state.centerY;
}
for (int px=0; px<w; px++) {
for (int py=0; py<h; py++) {
r = g = b = 0;
float iterations = iterationsToEscape(xs[px], ys[py], state.maxIterations);
if (iterations != -1) {
float h = HUE_PER_ITERATION * iterations;
r = hue2rgb(h + 120);
g = hue2rgb(h);
b = hue2rgb(h + 240);
}
int loc = (px+py*w)*3;
img[loc+2] = (unsigned char)(r);
img[loc+1] = (unsigned char)(g);
img[loc+0] = (unsigned char)(b);
}
}
return img;
}
////////////////////////////////////////////////////////////////////////////////
void draw(State state) {
unsigned char *img = createImage(state);
writeImage(img, state.w, state.h);
}
int main() {
State state;
draw(state);
int ch = 0;
initscr(); //curses
timeout(25); //curses ?
while (ch != 111) { //o
ch = getchar(); //curses
if (ch != ERR && ch != -1) {
if (ch == 101) {} //e
else if (ch == 97) state.moveX(-100); //a
else if (ch == 100) state.moveX(100); //d
else if (ch == 115) state.moveY(100); //s
else if (ch == 119) state.moveY(-100); //w
else if (ch == 114) state.zoomBy(2); //r
else if (ch == 102) state.zoomBy(0.5); //f
else if (ch == 116) state.addIterations(10); //t
else if (ch == 103) state.addIterations(-10); //g
// else printf("%i,", ch);
if (DRAW_ON_KEY || ch == 101) draw(state); //e
}
}
endwin(); //curses
}