File: tile-with-clip.py

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 (64 lines) | stat: -rw-r--r-- 1,895 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
# Tile current selection with clipboard pattern.
# Author: Andrew Trevorrow (andrew@trevorrow.com), March 2006.
# Updated to use exit command, Nov 2006.
# Updated to handle multi-state patterns, Aug 2008.

from glife import *
import golly as g

# assume one-state cell list (may change below)
inc = 2

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

def clip_rb (patt, right, bottom):
   # remove any cells outside given right and bottom edges
   clist = list(patt)
   
   #  remove padding int if present
   if (inc == 3) and (len(clist) % 3 == 1): clist.pop()
   
   x = 0
   y = 1
   while x < len(clist):
      if (clist[x] > right) or (clist[y] > bottom):
         # remove cell from list
         clist[x : x+inc] = []
      else:
         x += inc
         y += inc
   
   # append padding int if necessary
   if (inc == 3) and (len(clist) & 1 == 0): clist.append(0)
   
   return pattern(clist)

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

selrect = rect( g.getselrect() )
if selrect.empty: g.exit("There is no selection.")

cliplist = g.getclip()                    # 1st 2 items are wd,ht
pbox = rect( [0, 0] + cliplist[0 : 2] )
cliplist[0 : 2] = []                      # remove wd,ht
p = pattern( cliplist )

if len(cliplist) & 1 == 1: inc = 3        # multi-state?

g.clear(inside)
if len(cliplist) > 0:
   # tile selrect with p, clipping right & bottom edges if necessary
   y = selrect.top
   while y <= selrect.bottom:
      bottom = y + pbox.height - 1
      x = selrect.left
      while x <= selrect.right:
         right = x + pbox.width - 1
         if (right <= selrect.right) and (bottom <= selrect.bottom):
            p.put(x, y)
         else:
            clip_rb( p(x, y), selrect.right, selrect.bottom ).put()
         x += pbox.width
      y += pbox.height

if not selrect.visible(): g.fitsel()