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