-
Notifications
You must be signed in to change notification settings - Fork 33
/
sierpinski_triangle.pl
executable file
·52 lines (44 loc) · 1.16 KB
/
sierpinski_triangle.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 20 December 2014
# https://github.com/trizen
# Generate a graphical Sierpinski triangle of a given size.
use 5.010;
use strict;
use warnings;
use GD::Simple;
sub sierpinski {
my ($n) = @_;
my @down = '*';
my $space = ' ';
foreach (1 .. $n) {
@down = (map({ $space . $_ . $space } @down), map({ $_ . ' ' . $_ } @down));
$space = $space . $space;
}
return @down;
}
my @lines = sierpinski(8);
my $size = $ARGV[0] // 2;
my $img = GD::Simple->new(length($lines[0]) * $size, scalar(@lines) * $size);
foreach my $i (0 .. $#lines) {
foreach my $j ($i * $size .. $i * $size + $size) {
$img->moveTo(0, $j);
my $row = $lines[$i];
while (1) {
if ($row =~ s/^(\s+)//) {
$img->fgcolor('black');
$img->line($size * length($1));
}
elsif ($row =~ s/^(\S+)//) {
$img->fgcolor('red');
$img->line($size * length($1));
}
else {
last;
}
}
}
}
open my $fh, '>:raw', 'triangle.png';
print $fh $img->png;
close $fh;