-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathascii_julia_set.pl
executable file
·55 lines (41 loc) · 1.44 KB
/
ascii_julia_set.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 03 January 2018
# https://github.com/trizen
# ASCII generation of a Julia set (+ANSI colors).
# See also:
# https://en.wikipedia.org/wiki/Julia_set
use 5.020;
use warnings;
use experimental qw(signatures);
use Math::GComplex;
use Term::ANSIColor qw(:constants);
my @colors = (
(BLACK), (RED), (GREEN), (YELLOW), (BLUE), (MAGENTA),
(CYAN), (WHITE), (BRIGHT_BLACK), (BRIGHT_RED), (BRIGHT_GREEN), (BRIGHT_YELLOW),
(BRIGHT_BLUE), (BRIGHT_MAGENTA), (BRIGHT_CYAN), (BRIGHT_WHITE),
);
my @chars = (' ', '`', '.', ',', ':', ';', '!', '-', '+', '*', '%', '#');
sub range_map ($value, $in_min, $in_max, $out_min, $out_max) {
($value - $in_min)
* ($out_max - $out_min)
/ ($in_max - $in_min)
+ $out_min;
}
sub julia_set ($z, $I = 12, $L = 2, $C = Math::GComplex->new(-0.835, -0.2321)) {
my $n = 0;
while (abs($z) < $L and ++$n <= $I) {
$z = $z * $z + $C;
}
return (($I - $n) / $I);
}
for (my $y = 1 ; $y >= -1 ; $y -= 0.05) {
for (my $x = -2 ; $x <= 2 ; $x += 0.0315) {
my $num = julia_set(Math::GComplex->new($x, $y));
my $color_index = sprintf('%.0f', range_map($num, 0, 1, 0, $#colors));
my $char_index = sprintf('%.0f', range_map($num, 0, 1, 0, $#chars));
print($colors[$color_index] . $chars[$char_index]);
}
print "\n";
}
print(RESET);