File: tile.pl

package info (click to toggle)
golly 2.3-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 10,080 kB
  • sloc: cpp: 41,951; python: 6,339; sh: 3,912; perl: 1,172; java: 49; makefile: 47
file content (211 lines) | stat: -rw-r--r-- 5,458 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Tile current selection with pattern inside selection.
# Author: Andrew Trevorrow (andrew@trevorrow.com), June 2007.
# Updated to handle multi-state patterns, Aug 2008.

use strict;

my @selrect = g_getselrect();
g_exit("There is no selection.") if @selrect == 0;

my $selpatt = g_getcells(@selrect);
g_exit("No pattern in selection.") if @{$selpatt} == 0;

# determine if selpatt is one-state or multi-state
my $inc = 2;
if (@{$selpatt} & 1 == 1) { $inc = 3 }

# ------------------------------------------------------------------------------

# return a rect which is the minimal bounding box of given pattern
sub getminbox {
   my $cells = shift;
   my $len = @{$cells};
   return () if $len < 2;
   
   my $minx = $cells->[0];
   my $miny = $cells->[1];
   my $maxx = $minx;
   my $maxy = $miny;

   # ignore padding int if present
   $len -= 1 if ($inc == 3) and ($len % 3 == 1);
   
   for (my $x = 0; $x < $len; $x += $inc) {
      if ($cells->[$x] < $minx) { $minx = $cells->[$x] }
      if ($cells->[$x] > $maxx) { $maxx = $cells->[$x] }
   }
   for (my $y = 1; $y < $len; $y += $inc) {
      if ($cells->[$y] < $miny) { $miny = $cells->[$y] }
      if ($cells->[$y] > $maxy) { $maxy = $cells->[$y] }
   }
   
   return ($minx, $miny, $maxx - $minx + 1, $maxy - $miny + 1);
}

# ------------------------------------------------------------------------------

sub clip_left {
   my ($cells, $left) = @_;
   my $len = @{$cells};
   my $x = 0;
   if ($inc == 3) {
      #  ignore padding int if present
      $len -= 1 if $len % 3 == 1;
      while ($x < $len) {
         if ($cells->[$x] >= $left) {
            g_setcell($cells->[$x], $cells->[$x+1], $cells->[$x+2]);
         }
         $x += 3;
      }
   } else {
      while ($x < $len) {
         if ($cells->[$x] >= $left) {
            g_setcell($cells->[$x], $cells->[$x+1], 1);
         }
         $x += 2;
      }
   }
}

# ------------------------------------------------------------------------------

sub clip_right {
   my ($cells, $right) = @_;
   my $len = @{$cells};
   my $x = 0;
   if ($inc == 3) {
      #  ignore padding int if present
      $len -= 1 if $len % 3 == 1;
      while ($x < $len) {
         if ($cells->[$x] <= $right) {
            g_setcell($cells->[$x], $cells->[$x+1], $cells->[$x+2]);
         }
         $x += 3;
      }
   } else {   
      while ($x < $len) {
         if ($cells->[$x] <= $right) {
            g_setcell($cells->[$x], $cells->[$x+1], 1);
         }
         $x += 2;
      }
   }
}

# ------------------------------------------------------------------------------

sub clip_top {
   my ($cells, $top) = @_;
   my $len = @{$cells};
   my $y = 1;
   if ($inc == 3) {
      #  ignore padding int if present
      $len -= 1 if $len % 3 == 1;
      while ($y < $len) {
         if ($cells->[$y] >= $top) {
            g_setcell($cells->[$y-1], $cells->[$y], $cells->[$y+1]);
         }
         $y += 3;
      }
   } else {   
      while ($y < $len) {
         if ($cells->[$y] >= $top) {
            g_setcell($cells->[$y-1], $cells->[$y], 1);
         }
         $y += 2;
      }
   }
}

# ------------------------------------------------------------------------------

sub clip_bottom {
   my ($cells, $bottom) = @_;
   my $len = @{$cells};
   my $y = 1;
   if ($inc == 3) {
      #  ignore padding int if present
      $len -= 1 if $len % 3 == 1;
      while ($y < $len) {
         if ($cells->[$y] <= $bottom) {
            g_setcell($cells->[$y-1], $cells->[$y], $cells->[$y+1]);
         }
         $y += 3;
      }
   } else {   
      while ($y < $len) {
         if ($cells->[$y] <= $bottom) {
            g_setcell($cells->[$y-1], $cells->[$y], 1);
         }
         $y += 2;
      }
   }
}

# ------------------------------------------------------------------------------

# set selection edges
my $selleft = $selrect[0];
my $seltop = $selrect[1];
my $selright = $selleft + $selrect[2] - 1;
my $selbottom = $seltop + $selrect[3] - 1;

# find selpatt's minimal bounding box
my @bbox = getminbox($selpatt);
my $i;

# first tile selpatt horizontally, clipping where necessary
my $left = $bbox[0];
my $right = $left + $bbox[2] - 1;
$i = 0;
while ($left > $selleft) {
   $left -= $bbox[2];
   $i += 1;
   if ($left >= $selleft) {
      g_putcells($selpatt, -$bbox[2] * $i, 0);
   } else {
      my $tempcells = g_transform($selpatt, -$bbox[2] * $i, 0);
      clip_left($tempcells, $selleft);
   }
}
$i = 0;
while ($right < $selright) {
   $right += $bbox[2];
   $i += 1;
   if ($right <= $selright) {
      g_putcells($selpatt, $bbox[2] * $i, 0);
   } else {
      my $tempcells = g_transform($selpatt, $bbox[2] * $i, 0);
      clip_right($tempcells, $selright);
   }
}

# get new selection pattern and tile vertically, clipping where necessary
$selpatt = g_getcells(@selrect);
@bbox = getminbox($selpatt);
my $top = $bbox[1];
my $bottom = $top + $bbox[3] - 1;
$i = 0;
while ($top > $seltop) {
   $top -= $bbox[3];
   $i += 1;
   if ($top >= $seltop) {
      g_putcells($selpatt, 0, -$bbox[3] * $i);
   } else {
      my $tempcells = g_transform($selpatt, 0, -$bbox[3] * $i);
      clip_top($tempcells, $seltop);
   }
}
$i = 0;
while ($bottom < $selbottom) {
   $bottom += $bbox[3];
   $i += 1;
   if ($bottom <= $selbottom) {
      g_putcells($selpatt, 0, $bbox[3] * $i);
   } else {
      my $tempcells = g_transform($selpatt, 0, $bbox[3] * $i);
      clip_bottom($tempcells, $selbottom);
   }
}

g_fitsel() if !g_visrect(@selrect);