Animated fractal

Following up previous post, I've decided to add some motion.
The process of image generation will remain mostly the same. To introduce some action, we will change exponent value in time.
Let's start with precomputing fractals data.

  1. x = single(linspace(-2.3, 2.3, 500));
  2. y = single(linspace(-2.3, 2.3, 500));
  3. l = single(linspace(-5, 5, 500));
  4. [X,Y]=meshgrid(x,y);
  5. C=X+1i*Y;
  6. Z = zeros([size(C), length(l)], 'single');
  7. iter_num = 10;
  8. for it = 1:iter_num
  9. disp(it);
  10. for L = 1:length(l)
  11. Z(:,:,L)=Z(:,:,L).^l(L)+C;
  12. end
  13. end
  14. len_l = length(l);
  15. W = exp(-abs(Z));
  16. W(W<1e-10) = 0.1*max(W(:))*angle(Z(W<1e-10))/pi;
  17. W = W-min(W(:));
  18. W = W/max(W(:));

Execution of this code requires ~2G of memory, so be careful. Singles are used to reduce memory consumption. Calls to clear function serve the same purpose. More efficient way is to create and write to file one image at a time. That should reduce memory requirements in half a thousand times. But having all the data precomputed gives more flexibility for representation and allows to generate sophisticated images based on some other time axis.

Next part is saving animation to GIF file.

  1. d = 256;
  2. dt = 0.03;
  3. filename = 'image.gif';
  4. for L = 1:len_l
  5. if L==1
  6. imwrite(1+round(W(:,:,L)*(d-1)), summer(d), filename, 'gif', 'LoopCount', Inf, 'DelayTime', dt);
  7. else
  8. imwrite(1+round(W(:,:,L)*(d-1)), summer(d), filename, 'gif', 'WriteMode', 'append', 'DelayTime', dt);
  9. end
  10. end

This code is based on Matalb documentation for imwrite function. In Octave (at least in 3.6.4) this code doesn't work. After some changes it executes with warning, but produces GIF with only one image. A possible workaround is to write separate image for each frame and then combine them in one GIF using some other tool.

As a result, you'll get the following animation (warning, size: ~35M): GIF-file.

Leave a Reply

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload CAPTCHA.