File: tile.py

package info (click to toggle)
golly 3.2-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 19,516 kB
  • sloc: cpp: 69,819; ansic: 25,894; python: 7,921; sh: 4,267; objc: 3,721; java: 2,781; xml: 1,362; makefile: 530; perl: 69
file content (146 lines) | stat: -rw-r--r-- 3,967 bytes parent folder | download | duplicates (5)
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
# Tile current selection with pattern inside selection.
# 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

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

selpatt = pattern( g.getcells(g.getselrect()) )
if len(selpatt) == 0: g.exit("No pattern in selection.")

# determine if selpatt is one-state or multi-state
inc = 2
if len(selpatt) & 1 == 1: inc = 3

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

def clip_left (patt, left):
    clist = list(patt)

    #  remove padding int if present
    if (inc == 3) and (len(clist) % 3 == 1): clist.pop()

    x = 0
    while x < len(clist):
        if clist[x] < left:
            clist[x : x+inc] = []      # remove cell from list
        else:
            x += inc

    # append padding int if necessary
    if (inc == 3) and (len(clist) & 1 == 0): clist.append(0)

    return pattern(clist)

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

def clip_right (patt, right):
    clist = list(patt)

    #  remove padding int if present
    if (inc == 3) and (len(clist) % 3 == 1): clist.pop()

    x = 0
    while x < len(clist):
        if clist[x] > right:
            clist[x : x+inc] = []      # remove cell from list
        else:
            x += inc

    # append padding int if necessary
    if (inc == 3) and (len(clist) & 1 == 0): clist.append(0)

    return pattern(clist)

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

def clip_top (patt, top):
    clist = list(patt)

    #  remove padding int if present
    if (inc == 3) and (len(clist) % 3 == 1): clist.pop()

    y = 1
    while y < len(clist):
        if clist[y] < top:
            clist[y-1 : y-1+inc] = []     # remove cell from list
        else:
            y += inc

    # append padding int if necessary
    if (inc == 3) and (len(clist) & 1 == 0): clist.append(0)

    return pattern(clist)

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

def clip_bottom (patt, bottom):
    clist = list(patt)

    #  remove padding int if present
    if (inc == 3) and (len(clist) % 3 == 1): clist.pop()

    y = 1
    while y < len(clist):
        if clist[y] > bottom:
            clist[y-1 : y-1+inc] = []     # remove cell from list
        else:
            y += inc

    # append padding int if necessary
    if (inc == 3) and (len(clist) & 1 == 0): clist.append(0)

    return pattern(clist)

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

# find selpatt's minimal bounding box
bbox = getminbox(selpatt)

# first tile selpatt horizontally, clipping where necessary
left = bbox.left
i = 0
while left > selrect.left:
    left -= bbox.width
    i += 1
    if left >= selrect.left:
        selpatt.put(-bbox.width * i, 0)
    else:
        clip_left( selpatt(-bbox.width * i, 0), selrect.left ).put()
right = bbox.right
i = 0
while right < selrect.right:
    right += bbox.width
    i += 1
    if right <= selrect.right:
        selpatt.put(bbox.width * i, 0)
    else:
        clip_right( selpatt(bbox.width * i, 0), selrect.right ).put()

# get new selection pattern and tile vertically, clipping where necessary
selpatt = pattern( g.getcells(g.getselrect()) )
bbox = getminbox(selpatt)
top = bbox.top
i = 0
while top > selrect.top:
    top -= bbox.height
    i += 1
    if top >= selrect.top:
        selpatt.put(0, -bbox.height * i)
    else:
        clip_top( selpatt(0, -bbox.height * i), selrect.top ).put()
bottom = bbox.bottom
i = 0
while bottom < selrect.bottom:
    bottom += bbox.height
    i += 1
    if bottom <= selrect.bottom:
        selpatt.put(0, bbox.height * i)
    else:
        clip_bottom( selpatt(0, bbox.height * i), selrect.bottom ).put()

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