-
Notifications
You must be signed in to change notification settings - Fork 34
/
julia_set_random.pl
executable file
·71 lines (52 loc) · 1.56 KB
/
julia_set_random.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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 26 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 $color = Imager::Color->new('#000000');
foreach my $x (0 .. $w - 1) {
foreach my $y (0 .. $h - 1) {
my $zx = 3/2 * (2*($x+1) - $w) / ($w * $zoom) + $moveX;
my $zy = 1/1 * (2*($y+1) - $h) / ($h * $zoom) + $moveY;
my $i = iterate($zx, $zy, $cX, $cY, $maxIter);
$color->set(hsv => [$i / $maxIter * 360, 1, $i]);
$img->setpixel(x => $x, y => $y, color => $color);
}
}
$img->write(file => "i=$maxIter;x=$cX;y=$cY.png");
}
__END__
__C__
int iterate(double zx, double zy, double cX, double cY, int i) {
double tmp1;
double tmp2;
while(1) {
tmp1 = zx*zx;
tmp2 = zy*zy;
if (!((tmp1 + tmp2 < 4) && (--i > 0))) {
break;
}
zy = 2 * zx*zy + cY;
zx = tmp1 - tmp2 + cX;
}
return i;
}