-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathjulia_transform.pl
executable file
·104 lines (80 loc) · 2.12 KB
/
julia_transform.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 11 March 2017
# https://github.com/trizen
# Julia transform of an image.
# See also:
# https://en.wikipedia.org/wiki/Julia_set
use 5.010;
use strict;
use warnings;
use Imager;
use Math::GComplex;
my $file = shift(@ARGV) // die "usage: $0 [image]\n";
sub map_val {
my ($value, $in_min, $in_max, $out_min, $out_max) = @_;
#<<<
($value - $in_min)
* ($out_max - $out_min)
/ ($in_max - $in_min)
+ $out_min;
#>>>
}
my $img = Imager->new(file => $file)
or die Imager->errstr();
my $width = $img->getwidth;
my $height = $img->getheight;
sub transform {
my ($x, $y) = @_;
#<<<
my $z = Math::GComplex->new(
(2 * $x - $width ) / $width,
(2 * $y - $height) / $height,
);
#>>>
state $c = Math::GComplex->new(-0.4, 0.6);
my $i = 10;
while ($z->abs < 2 and --$i >= 0) {
$z = $z * $z + $c;
}
$z->reals;
}
my @matrix;
my ($min_x, $min_y) = ('inf') x 2;
my ($max_x, $max_y) = (-'inf') x 2;
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my ($new_x, $new_y) = transform($x, $y);
$matrix[$y][$x] = [$new_x, $new_y];
if ($new_x < $min_x) {
$min_x = $new_x;
}
if ($new_y < $min_y) {
$min_y = $new_y;
}
if ($new_x > $max_x) {
$max_x = $new_x;
}
if ($new_y > $max_y) {
$max_y = $new_y;
}
}
}
say "X: [$min_x, $max_x]";
say "Y: [$min_y, $max_y]";
my $out_img = Imager->new(xsize => $width,
ysize => $height);
foreach my $y (0 .. $height - 1) {
foreach my $x (0 .. $width - 1) {
my ($new_x, $new_y) = @{$matrix[$y][$x]};
$new_x = map_val($new_x, $min_x, $max_x, 0, $width - 1);
$new_y = map_val($new_y, $min_y, $max_y, 0, $height - 1);
$out_img->setpixel(
x => $new_x,
y => $new_y,
color => $img->getpixel(x => $x, y => $y),
);
}
}
$out_img->write(file => 'julia_transform.png');