-
Notifications
You must be signed in to change notification settings - Fork 33
/
recursive_squares.pl
executable file
·86 lines (64 loc) · 1.84 KB
/
recursive_squares.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
#!/usr/bin/perl
# A nice recursive pattern, using the following rule:
# --- |---|
# | goes to | which goes to | and so on.
# --- |---|
use 5.014;
use Imager;
my $xsize = 800;
my $ysize = 800;
my $img = Imager->new(xsize => $xsize, ysize => $ysize, channels => 3);
my $color = Imager::Color->new('#ff0000');
sub a {
my ($x, $y, $len, $rep) = @_;
$img->line(
x1 => $x,
x2 => $x,
y1 => $y,
y2 => $y + $len,
color => $color,
);
f($x, $y, $len, $rep);
}
sub f {
my ($x, $y, $len, $rep) = @_;
$rep <= 0 and return;
$img->line(
x1 => $x - $len / 2,
x2 => $x + $len / 2,
y1 => $y,
y2 => $y,
color => $color,
);
g($x - $len / 2, $y, $len, $rep - 1);
$img->line(
x1 => $x - $len / 2,
x2 => $x + $len / 2,
y1 => $y + $len,
y2 => $y + $len,
color => $color,
);
g($x - $len / 2, $y + $len, $len, $rep - 1);
}
sub g {
my ($x, $y, $len, $rep) = @_;
$rep <= 0 and return;
$img->line(
x1 => $x,
x2 => $x,
y1 => $y - $len / 2,
y2 => $y + $len / 2,
color => $color,
);
f($x, $y - $len / 2, $len, $rep - 1);
$img->line(
x1 => $x + $len,
x2 => $x + $len,
y1 => $y - $len / 2,
y2 => $y + $len / 2,
color => $color,
);
f($x + $len, $y - $len / 2, $len, $rep - 1);
}
a($xsize / 2, $ysize / 2, sqrt($xsize + $ysize), 12);
$img->write(file => "recursive_squares.png");