julia_set_complex.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 27 March 2016
  5. # Website: https://github.com/trizen
  6. # Generate 100 random Julia sets.
  7. # Formula: f(z) = z^2 + c
  8. # See also: https://en.wikipedia.org/wiki/Julia_set
  9. # https://rosettacode.org/wiki/Julia_set
  10. use strict;
  11. use warnings;
  12. use Imager;
  13. use Inline 'C';
  14. for (1 .. 100) {
  15. my ($w, $h) = (800, 600);
  16. my $zoom = 1;
  17. my $moveX = 0;
  18. my $moveY = 0;
  19. my $img = Imager->new(xsize => $w, ysize => $h, channels => 3);
  20. ##my $maxIter = int(rand(200))+50;
  21. my $maxIter = 50;
  22. ##my ($cx, $cy) = (-rand(1), rand(1));
  23. ##my ($cx, $cy) = (1-rand(2), 1-rand(2)); # cool
  24. my ($cx, $cy) = (1 - rand(2), rand(1)); # nice
  25. ##my ($cx, $cy) = (1 - rand(2), 2 - rand(3));
  26. ##my ($cx, $cy) = ((-1)**((1,2)[rand(2)]) * rand(2), (-1)**((1,2)[rand(2)]) * rand(2));
  27. my $color = Imager::Color->new('#000000');
  28. foreach my $x (0 .. $w - 1) {
  29. foreach my $y (0 .. $h - 1) {
  30. my $i = iterate(
  31. 3/2 * (2*($x+1) - $w) / ($w * $zoom) + $moveX,
  32. 1/1 * (2*($y+1) - $h) / ($h * $zoom) + $moveY,
  33. $cx, $cy, $maxIter
  34. );
  35. $color->set(hsv => [$i / $maxIter * 360 - 120, 1, $i]);
  36. $img->setpixel(x => $x, y => $y, color => $color);
  37. }
  38. }
  39. print "Writing new image...\n";
  40. $img->write(file => "i=$maxIter;c=$cx+$cy.png");
  41. }
  42. __END__
  43. __C__
  44. #include <complex.h>
  45. int iterate(double zx, double zy, double cx, double cy, int i) {
  46. double complex z = zx + zy * I;
  47. double complex c = cx + cy * I;
  48. while (cabs(z) < 2 && --i) {
  49. z = z*z + c;
  50. //z = z * cexp(z) + c;
  51. //z = ccosh(z) + c;
  52. //z = z * csinh(z) + c;
  53. //z = z * ccosh(z) + c;
  54. //z = clog(csinh(z)) + c;
  55. //z = csqrt(cexp(z) + ccosh(z)) + c;
  56. }
  57. return i;
  58. }