-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze3_anim.pl
executable file
·99 lines (80 loc) · 2.85 KB
/
maze3_anim.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
#!/usr/bin/perl -w
use strict;
use Image::Magick;
my $SIZEX = 20;
my $SIZEY = 20;
# ----------------------------
my $MAXX = $SIZEX * 2 + 1;
my $MAXY = $SIZEY * 2 + 1;
my $WALL = "#";
my $EMPTY = " ";
my $arefTmp = [];
my $animation = Image::Magick->new();
# Generate an empty maze (Just the borders)
sub initMaze {
foreach my $iX (1 .. $MAXX) {
$arefTmp->[$iX][1] = $WALL;
$arefTmp->[$iX][$MAXY] = $WALL;
}
foreach my $iY (2 .. $MAXY-1) {
$arefTmp->[1][$iY] = $WALL;
foreach my $iX (2 .. $MAXX-1) {
$arefTmp->[$iX][$iY] = $EMPTY;
}
$arefTmp->[$MAXX][$iY] = $WALL;
}
}
sub getRamdomWall {
my ($a, $b) = @_;
my $iPosibles = ($b - $a - 2) / 2; # Posible holes
my $iWall = 2 + $a + (int(rand($iPosibles)) ) * 2; # Get one random hole
return $iWall;
}
sub getRamdomDoor {
my ($a, $b) = @_;
my $iPosibles = ($b - $a) / 2; # Posible holes
my $iDoor = $a - 1 + (int(rand($iPosibles)) + 1) * 2; # Get one random hole
return $iDoor;
}
sub divideVertical {
my ($x1, $y1, $x2, $y2) = @_;
my $xmiddle = getRamdomWall($x1, $x2); # Middle of the area
my $iCut = getRamdomDoor($y1, $y2); # Get one random hole
foreach my $iY ($y1+1 .. $y2-1) { # Draw vertical wall
$arefTmp->[$xmiddle][$iY] = ($iY == $iCut ? $EMPTY : $WALL); # But the hole
}
addAnimationFrame();
divideHorizontal($x1, $y1, $xmiddle, $y2) if ($xmiddle - $x1 > 3);
divideHorizontal($xmiddle, $y1, $x2, $y2) if ($x2 - $xmiddle > 3);
}
sub divideHorizontal {
my ($x1, $y1, $x2, $y2) = @_;
my $ymiddle = getRamdomWall($y1, $y2); # Middle of the area
my $iCut = getRamdomDoor($x1, $x2); # Get one random hole
foreach my $iX ($x1+1 .. $x2-1) { # Draw horizontal wall
$arefTmp->[$iX][$ymiddle] = ($iX == $iCut ? $EMPTY : $WALL); # But the hole
}
addAnimationFrame();
divideVertical($x1, $y1, $x2, $ymiddle) if ($ymiddle - $y1 > 3);
divideVertical($x1, $ymiddle, $x2, $y2) if ($y2 - $ymiddle > 3);
}
sub addAnimationFrame {
my $image = Image::Magick->new;
$image->Set(size=>'' . $MAXX . 'x' . $MAXY . '');
$image->ReadImage('xc:white');
foreach my $iY (1 .. $MAXY) {
foreach my $iX (1 .. $MAXX) {
$image->Set('pixel[' . ($iX - 1) . ',' . ($iY - 1) . ']'=>'red') if ($arefTmp->[$iX][$iY] eq $WALL);
}
};
$image->Set('pixel[1,0]'=>'green');
$image->Set('pixel[' . ($MAXX - 2) . ',' . ($MAXY - 1) . ']'=>'green');
push @$animation, $image;
}
initMaze();
divideVertical(1, 1, $MAXX, $MAXY);
addAnimationFrame();
$animation->[0]->Coalesce();
$animation->Set(delay => 5);
$animation->Scale( width => $MAXX * 10, height => $MAXY * 10 );
$animation->Write( "maze3_anim.gif" );