-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathrandom_looking_pattern_triangle.pl
More file actions
executable file
·59 lines (46 loc) · 1.02 KB
/
random_looking_pattern_triangle.pl
File metadata and controls
executable file
·59 lines (46 loc) · 1.02 KB
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
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 26 May 2015
# https://github.com/trizen
#
## Generate a random-looking pattern triangle (but it's not random!)
#
use 5.010;
use strict;
use warnings;
use GD::Simple;
sub generate {
my ($n, $data) = @_;
my $sum = 0;
foreach my $i (1 .. $n) {
if ($sum >= $i) {
$data->{$sum} = 1;
$sum -= int(sqrt($i) + 1); # this is the "random" line
}
else {
$sum += $i;
}
}
return $n;
}
say "** Generating...";
my %data;
my $max = generate(100000, \%data);
my $limit = int(sqrt($max)) - 1;
# create a new image
my $img = GD::Simple->new($limit * 2, $limit + 1);
my $i = 1;
my $j = 1;
for my $m (reverse(0 .. $limit)) {
$img->moveTo($m, $i - 1);
for my $n ($j .. $i**2) {
$img->fgcolor(exists($data{$j}) ? 'red' : 'black');
$img->line(1);
++$j;
}
++$i;
}
open my $fh, '>:raw', "random_looking_triangle.png";
print $fh $img->png;
close $fh;