-
Notifications
You must be signed in to change notification settings - Fork 2
/
text_mandelbrot.cpp
82 lines (60 loc) · 2.27 KB
/
text_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
/*
Compile:
g++ -O2 text_buddhabrot.cpp -o bin/text_buddhabrot
Heavily cleaned up version based on Mandelbrot at:
* http://bisqwit.iki.fi/story/howto/openmp/
*/
#include <complex>
#include <stdio.h> // printf
#include <string.h> // memset
typedef std::complex<double> complex;
int MandelbrotCalculate( complex c, int max_depth )
{
// iterates z = z + c until |z| >= 2 or max_depth is reached,
// returns the number of iterations.
complex z = c;
int depth = 0;
for( depth = 0; depth < max_depth; ++depth )
{
// Optimization: Remove unnecessary square root by squaring both sides
//if( std::abs(z) >= 2.0) break;
if( (z.real()*z.real() + z.imag()*z.imag()) > 4.0) break;
z = z*z + c;
}
return depth;
}
void MandelbrotPlot( int depth, int x, int y, char *output, int width )
{
static const char charset[] = ".,c8M@jawrpogOQEPGJ";
char c = charset[ depth % (sizeof(charset)-1)];
output[ (y*width) + x ] = c;
}
int main()
{
const int width = 78, height = 44, num_pixels = width*height;
const complex center(-.7, 0),
span(2.7, -(4/3.0)*2.7*height/width), // 4/3 is aspect ratio
TopLeft = center - span/2.0;
const int max_depth = 100000;
char output[ height ][ width ];
memset( output, (int)' ', sizeof( output ) );
for(int pix=0; pix<num_pixels; ++pix)
{
const int x = pix%width, y = pix/width;
complex p = TopLeft + complex(
x * span.real() / (width +1.0),
y * span.imag() / (height+1.0));
int cur_depth = MandelbrotCalculate( p, max_depth );
if (cur_depth != max_depth)
{
MandelbrotPlot( cur_depth, x, y, (char*)output, width ); // output[ y ][ x ] = c;
}
}
// We could have a 1 character skirt on the right edge and bottom
// but reserve one column and row for outputing.
for( int y = 0; y < height-1; y++ )
output[ y ][ width-1 ] = '\n';
output[ height-1 ][ width-1 ] = 0;
printf( "%s\n", &output[0][0] );
return 0;
}