Created
August 1, 2017 14:15
-
-
Save johnmcfarlane/c7f47599a52e1339f5380454e605e097 to your computer and use it in GitHub Desktop.
Minimal Mandelbrot generator using CNL
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
#include <cnl/fixed_point.h> | |
#include <array> | |
using namespace cnl; | |
using namespace std; | |
constexpr int mandelbrot(float f_x, float f_y, int const limit) { | |
using scalar = fixed_point<int64_t, -28>; | |
auto c_x = scalar{f_x}; | |
auto c_y = scalar{f_y}; | |
constexpr auto r = scalar{2}; | |
constexpr auto rr = r*r; | |
auto x = c_x, y = c_y; | |
for (auto i = 0; i != limit; ++ i) { | |
auto xx = x*x; | |
auto yy = y*y; | |
if (xx+yy>rr) { | |
return i; | |
} | |
y = scalar{2 * x * y} + c_y; | |
x = scalar{xx - yy} + c_x; | |
} | |
return limit; | |
} | |
int main(int, char**) { | |
for (auto y = -2.f; y <= 2; y += 1.f/8) { | |
for (auto x = -2.f; x <= 2; x += 1.f/16) { | |
auto i = mandelbrot(x, y, 95); | |
putchar((i == 95 ? 0 : i) + ' '); | |
} | |
putchar('\n'); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment