# Rotate hexagonal neighborhood pattern clockwise by 60 degrees.
# Author: Andrew Trevorrow (andrew@trevorrow.com), Oct 2011.
import golly as g
if g.empty(): g.exit("There is no pattern.")
rect = g.getrect()
x = rect[0]
y = rect[1]
wd = rect[2]
ht = rect[3]
# find central cell about which rotation will occur
cx = x + wd/2
cy = y + ht/2
# determine number of rings around central cell
numrings = wd
if ht > wd: numrings = ht
# the following transitions are used to calculate positions of cells
# in a ring, where the values start from the NW corner and assume the
# hexagonal neighborhood is emulated by ignoring the NE and SW corners:
#
# NW N NE NW N
# W C E -> W C E
# SW S SE S SE
#
transitions = [ (+1,0), (+1,+1), (0,+1), (-1,0), (-1,-1), (0,-1) ]
# rotate live cells within each ring, starting from the innermost ring
ring = 0
while ring < numrings:
ring += 1
x = cx-ring
y = cy-ring
# generate cell positions in current ring
cellpos = []
for i in xrange(6):
deltax, deltay = transitions[i]
# each transition element is repeated by the ring number
for j in xrange(ring):
cellpos.append( (x,y) )
x += deltax
y += deltay
# delete live cells in ring and remember their rotated positions
# by cycling to the right by the ring number
saveinfo = []
for i in xrange(6 * ring):
x, y = cellpos[i]
state = g.getcell(x, y)
if state > 0:
g.setcell(x, y, 0)
rotpos = (i + ring) % len(cellpos)
rotx, roty = cellpos[rotpos]
saveinfo.append( (rotx, roty, state) )
# create rotated cells
for info in saveinfo:
x, y, state = info
g.setcell(x, y, state)