Created
July 3, 2015 12:46
-
-
Save mindbound/592dca33456a494c7b0b to your computer and use it in GitHub Desktop.
Compile-time Mandelbrot set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// export SIZE_X=40 | |
// export SIZE_Y=21 | |
// export DEPTH=15 | |
// g++ -Wall -O2 -ftemplate-depth-$((($SIZE_X + 1) * $SIZE_Y + $DEPTH)) -fpermissive mandeltemp.cpp -o mandeltemp | |
#include <iostream> | |
static const int size_x = 40; | |
static const int size_y = 21; | |
static const int depth = 15; | |
static const int scale = 1024; | |
static const int limit = 10000; | |
static const double min_x = -2.5; | |
static const double max_x = 1.5; | |
static const double min_y = -1.5; | |
static const double max_y = 1.5; | |
static const int map_min_x = (int)(min_x * scale); | |
static const int map_max_x = (int)(max_x * scale); | |
static const int map_min_y = (int)(min_y * scale); | |
static const int map_max_y = (int)(max_y * scale); | |
static const int map_range_x = map_max_x - map_min_x; | |
static const int map_range_y = map_max_y - map_min_y; | |
static const int map_size_x = size_x - 1; | |
static const int map_size_y = size_y - 1; | |
static const int map_num_iter = depth - 1; | |
static const int map_limit = limit * scale; | |
template <int X, int Y, int I = 0> | |
class Value | |
{ | |
public: | |
enum | |
{ | |
z_real = Value<X, Y, I - 1>::real, | |
z_imag = Value<X, Y, I - 1>::imag, | |
c_real = map_range_x * X / map_size_x + map_min_x, | |
c_imag = map_range_y * Y / map_size_y + map_min_y, | |
real = (z_real * z_real - z_imag * z_imag) / scale + c_real, | |
imag = 2 * z_real * z_imag / scale + c_imag, | |
ret = ((real * real + imag * imag > map_limit) ? I : (Value<X, Y, I + 1>::ret)) | |
}; | |
}; | |
template <int X, int Y> | |
class Value<X, Y, -1> | |
{ | |
public: | |
enum | |
{ | |
real = 0, | |
imag = 0 | |
}; | |
}; | |
template <int X, int Y> | |
class Value<X, Y, map_num_iter> | |
{ | |
public: | |
enum | |
{ | |
ret = -1 | |
}; | |
}; | |
template <int X = 0, int Y = 0> | |
class Loop | |
{ | |
public: | |
static inline void f() | |
{ | |
std::cout << ((val == -1) ? ' ' : (char)('a' + val)); | |
Loop<X + 1, Y>::f(); | |
} | |
private: | |
enum | |
{ | |
val = Value<X, Y>::ret | |
}; | |
}; | |
template <int Y> | |
class Loop<size_x, Y> | |
{ | |
public: | |
static inline void f() | |
{ | |
std::cout << '\n'; | |
Loop<0, Y + 1>::f(); | |
} | |
}; | |
template <> | |
class Loop<0, size_y> | |
{ | |
public: | |
static inline void f() {} | |
}; | |
int main() | |
{ | |
Loop<>::f(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment