From 719245cb644926416239dd9cc9a2bfa79286342f Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Sat, 15 Sep 2012 15:47:29 +0200 Subject: [PATCH] added a mandelbrot fractal --- ImageStreams/main.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/ImageStreams/main.cpp b/ImageStreams/main.cpp index 22a6939..041d27f 100644 --- a/ImageStreams/main.cpp +++ b/ImageStreams/main.cpp @@ -114,6 +114,33 @@ void logistic2(std::string filename) { } } +inline int mandelbrot_thing(double const x, double const y){ + double zx = 0.0, zy = 0.0; + for(int i = 0; i < 255; ++i){ + double t = zx; + zx = zx*zx - zy*zy + x; + zy = 2.0 * t * zy + y; + + if(zx*zx + zy*zy > 100) return 10*i; + } + return 0; +} + +template +void mandelbrot(std::string filename) { + size_t width = 1280; + size_t height = 800; + + ImageType image(width, height, filename); + for(int y = 0; y < height; ++y){ + double dy = y / double(height - 1) * 2.0 - 1.0; + for(int x = 0; x < width; ++x){ + double dx = x / double(width - 1) * 2.0 * (width/double(height)) - 2.5; + image << typename ImageType::pixel(mandelbrot_thing(dx, dy)); + } + } +} + int main(int argc, const char * argv[]){ xor_color("xor_color.png"); xor_color("xor_color.bmp"); @@ -132,5 +159,8 @@ int main(int argc, const char * argv[]){ logistic2("logistic2.png"); logistic2("logistic2.bmp"); + + mandelbrot("mandelbrot.png"); + mandelbrot("mandelbrot.bmp"); }