Example #1
0
def rectingrid(r):
    # return True if all of given rectangle is inside grid
    if g.getwidth() > 0 and (r[0] < gridl or r[0] + r[2] - 1 > gridr):
        return False
    if g.getheight() > 0 and (r[1] < gridt or r[1] + r[3] - 1 > gridb):
        return False
    return True
def bruteForceSearch(searchPatt):
    cols = len(searchPatt[0])
    rows = len(searchPatt)
    matches = list()

    wd = golly.getwidth()
    ht = golly.getheight()

    [startWd, endWd] = getIndices(wd)
    [startHt, endHt] = getIndices(ht)
    #formatStr = 'Width: {0}   Height: {1}\nStart Pos: ({2},{3})\nEnd Pos:{4},{5}\np,q = {6} {7} ' 
    #golly.note(formatStr.format(wd,ht,startWd,startHt,endWd,endHt,p,q))

    # Only search the candidate matrix cells which can contain
    # the matrix we're searching for
    candidates = [(x,y) for x in range(startWd,endWd-cols+2) for y in range(startHt,endHt-rows+2)]
    for (iCol, iRow) in candidates:
        jCol = 0
        jRow = 0 
        while(golly.getcell(iCol + jCol,iRow + jRow) == searchPatt[jRow][jCol]):
            if (jCol == cols-1) and (jRow == rows-1):
                print '        Match = ({0},{1})'.format(iCol,iRow)
                matches.append((iCol,iRow))
                break
            jCol = jCol + 1
            if (jCol > cols - 1):
                jCol = 0
                jRow = jRow + 1
                if (jRow > rows - 1):
                    print '        Break = ({0},{1})'.format(iCol,iRow)
                    break ## End of search space, and no match found
    return [matches, candidates]
Example #3
0
def checkneighbor(x, y):
    # first check if x,y is outside bounded grid
    if g.getwidth() > 0 and (x < gridl or x > gridr): return
    if g.getheight() > 0 and (y < gridt or y > gridb): return
    if g.getcell(x, y) == 0: return
    # no need for next test because we kill cell after adding it to ncells
    # if (x, y) in ncells: return False
    ncells.append( (x, y, g.getcell(x,y)) )
    g.setcell(x, y, 0)
def checkneighbor(x, y):
   # first check if x,y is outside bounded grid
   if g.getwidth() > 0 and (x < gridl or x > gridr): return
   if g.getheight() > 0 and (y < gridt or y > gridb): return
   if g.getcell(x, y) == 0: return
   # no need for next test because we kill cell after adding it to ncells
   # if (x, y) in ncells: return False
   ncells.append( (x, y, g.getcell(x,y)) )
   g.setcell(x, y, 0)
Example #5
0
def get_minmax(g):
    """
  Calculate the min and max of the Golly toroid coordinates
  """
    # get height and width
    g_xspan = g.getwidth()
    g_yspan = g.getheight()
    # calculate min and max
    g_xmin = -int(g_xspan / 2)
    g_xmax = g_xspan + g_xmin
    g_ymin = -int(g_yspan / 2)
    g_ymax = g_yspan + g_ymin
    #
    return [g_xmin, g_xmax, g_ymin, g_ymax]
Example #6
0
def set_mag(g):
    """
  A function for setting the Golly screen magnification
  """
    # the maximum of the X span and the Y span
    g_maxspan = np.amax([g.getwidth(), g.getheight()])
    # the Golly magnification ratio is 2 to the power of mag
    if (g_maxspan < 80):
        mag = 5  # 2^5 = 32
    elif (g_maxspan < 160):
        mag = 4  # 2^4 = 16
    elif (g_maxspan < 320):
        mag = 3  # 2^3 = 8
    elif (g_maxspan < 640):
        mag = 2  # 2^2 = 4
    elif (g_maxspan < 1280):
        mag = 1  # 2^1 = 2
    else:
        mag = 0  # 2^0 = 1
    return mag
# Allow user to move the current selection.
# Author: Andrew Trevorrow ([email protected]), Jan 2011.

import golly as g

# set edges of bounded grid for later use
if g.getwidth() > 0:
   gridl = -int(g.getwidth()/2)
   gridr = gridl + g.getwidth() - 1
if g.getheight() > 0:
   gridt = -int(g.getheight()/2)
   gridb = gridt + g.getheight() - 1

helpmsg = " (hit 'h' for help)"

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

def showhelp1():
   g.note(
"""Hit the escape key to abort the script.

Note that alt-clicking in the selection allows you to COPY it
to another location (the original selection is not deleted).""")

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

def showhelp2():
   g.note(
"""While moving the selection the following keys can be used:

x -- flip selection left-right
Example #8
0
def moveselection():
    global oldcells, selrect, selpatt

    # wait for 1st click in selection
    while True:
        event = g.getevent()
        if event.startswith("click"):
            # event is a string like "click 10 20 left none"
            evt, xstr, ystr, butt, mods = event.split()
            x = int(xstr)
            y = int(ystr)
            if cellinrect(x, y, selrect):
                oldmouse = xstr + ' ' + ystr
                firstx = x
                firsty = y
                xoffset = firstx - selrect[0]
                yoffset = firsty - selrect[1]
                if mods == "alt":
                    # don't delete pattern in selection
                    oldcells = g.getcells(selrect)
                break
        elif event == "key h none":
            showhelp1()
        else:
            g.doevent(event)

    # wait for 2nd click while moving selection
    g.show("Move mouse and click again..." + helpmsg)
    gotclick = False
    while not gotclick:
        event = g.getevent()
        if event.startswith("click"):
            evt, x, y, butt, mods = event.split()
            mousepos = x+' '+y
            gotclick = True
        else:
            if len(event) > 0:
                lookforkeys(event, x - firstx, y - firsty)
                # update xoffset,yoffset in case selection was rotated
                xoffset = x - selrect[0]
                yoffset = y - selrect[1]
            mousepos = g.getxy()
        if len(mousepos) > 0 and mousepos != oldmouse:
            # mouse has moved, so move selection rect and pattern
            g.clear(0)
            if len(oldcells) > 0: g.putcells(oldcells)
            xstr, ystr = mousepos.split()
            x = int(xstr)
            y = int(ystr)
            selrect[0] = x - xoffset
            selrect[1] = y - yoffset

            if g.getwidth() > 0:
                # ensure selrect doesn't move beyond left/right edge of grid
                if selrect[0] < gridl:
                    selrect[0] = gridl
                    x = selrect[0] + xoffset
                elif selrect[0] + selrect[2] - 1 > gridr:
                    selrect[0] = gridr + 1 - selrect[2]
                    x = selrect[0] + xoffset
            if g.getheight() > 0:
                # ensure selrect doesn't move beyond top/bottom edge of grid
                if selrect[1] < gridt:
                    selrect[1] = gridt
                    y = selrect[1] + yoffset
                elif selrect[1] + selrect[3] - 1 > gridb:
                    selrect[1] = gridb + 1 - selrect[3]
                    y = selrect[1] + yoffset

            g.select(selrect)
            oldcells = g.getcells(selrect)
            g.putcells(selpatt, x - firstx, y - firsty)
            oldmouse = mousepos
            g.update()
Example #9
0
# Allow user to move the current selection.
# Author: Andrew Trevorrow ([email protected]), Jan 2011.

import golly as g

# set edges of bounded grid for later use
if g.getwidth() > 0:
    gridl = -int(g.getwidth()/2)
    gridr = gridl + g.getwidth() - 1
if g.getheight() > 0:
    gridt = -int(g.getheight()/2)
    gridb = gridt + g.getheight() - 1

helpmsg = " (hit 'h' for help)"

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

def showhelp1():
    g.note(
 """Hit the escape key to abort the script.

 Note that alt-clicking in the selection allows you to COPY it
 to another location (the original selection is not deleted).""")

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

def showhelp2():
    g.note(
 """While moving the selection the following keys can be used:

 x -- flip selection left-right
Example #10
0
def moveobject():
    global oldcells, object, object1

    # wait for click in or near a live cell
    while True:
        event = g.getevent()
        if event.startswith("click"):
            # event is a string like "click 10 20 left none"
            evt, xstr, ystr, butt, mods = event.split()
            result = findlivecell(int(xstr), int(ystr))
            if len(result) > 0:
                prevx = int(xstr)
                prevy = int(ystr)
                oldmouse = xstr + ' ' + ystr
                g.show("Extracting object...")
                x, y = result
                object = getobject(x, y)
                object1 = list(object)     # save in case user aborts script
                if mods == "alt":
                    # don't delete object
                    oldcells = list(object)
                break
            else:
                g.warn("Click on or near a live cell belonging to the desired object.")
        elif event == "key h none":
            showhelp()
        else:
            g.doevent(event)

    # wait for mouse-up while moving object
    g.show("Move mouse and release button...")
    mousedown = True
    while mousedown:
        event = g.getevent()
        if event.startswith("mup"):
            mousedown = False
        elif len(event) > 0:
            lookforkeys(event)
        mousepos = g.getxy()
        if len(mousepos) > 0 and mousepos != oldmouse:
            # mouse has moved, so move object
            g.putcells(object, 0, 0, 1, 0, 0, 1, "xor")  # erase object
            if len(oldcells) > 0: g.putcells(oldcells)
            xstr, ystr = mousepos.split()
            x = int(xstr)
            y = int(ystr)

            if g.getwidth() > 0:
                # ensure object doesn't move beyond left/right edge of grid
                obox = getminbox( g.transform(object, x - prevx, y - prevy) )
                if obox.left < gridl:
                    x += gridl - obox.left
                elif obox.right > gridr:
                    x -= obox.right - gridr
            if g.getheight() > 0:
                # ensure object doesn't move beyond top/bottom edge of grid
                obox = getminbox( g.transform(object, x - prevx, y - prevy) )
                if obox.top < gridt:
                    y += gridt - obox.top
                elif obox.bottom > gridb:
                    y -= obox.bottom - gridb

            object = g.transform(object, x - prevx, y - prevy)
            oldcells = underneath(object)
            g.putcells(object)
            prevx = x
            prevy = y
            oldmouse = mousepos
            g.update()
def getstate(x, y):
   # first check if x,y is outside bounded grid
   if g.getwidth() > 0 and (x < gridl or x > gridr): return 0
   if g.getheight() > 0 and (y < gridt or y > gridb): return 0
   return g.getcell(x, y)
def moveobject():
   global oldcells, object, object1
   
   # wait for 1st click in live cell
   while True:
      event = g.getevent()
      if event.startswith("click"):
         # event is a string like "click 10 20 left none"
         evt, xstr, ystr, butt, mods = event.split()
         result = findlivecell(int(xstr), int(ystr))
         if len(result) > 0:
            prevx = int(xstr)
            prevy = int(ystr)
            oldmouse = xstr + ' ' + ystr
            g.show("Extracting object...")
            x, y = result
            object = getobject(x, y)
            object1 = list(object)     # save in case user aborts script
            if mods == "alt":
               # don't delete object
               oldcells = list(object)
            break
         else:
            g.warn("Click on or near a live cell belonging to the desired object.")
      elif event == "key h none":
         showhelp1()
      else:
         g.doevent(event)
   
   # wait for 2nd click while moving object
   g.show("Move mouse and click again..." + helpmsg)
   gotclick = False
   while not gotclick:
      event = g.getevent()
      if event.startswith("click"):
         evt, x, y, butt, mods = event.split()
         mousepos = x+' '+y
         gotclick = True
      else:
         if len(event) > 0: lookforkeys(event)
         mousepos = g.getxy()
      if len(mousepos) > 0 and mousepos != oldmouse:
         # mouse has moved, so move object
         g.putcells(object, 0, 0, 1, 0, 0, 1, "xor")  # erase object
         if len(oldcells) > 0: g.putcells(oldcells)
         xstr, ystr = mousepos.split()
         x = int(xstr)
         y = int(ystr)

         if g.getwidth() > 0:
            # ensure object doesn't move beyond left/right edge of grid
            obox = getminbox( g.transform(object, x - prevx, y - prevy) )
            if obox.left < gridl:
               x += gridl - obox.left
            elif obox.right > gridr:
               x -= obox.right - gridr
         if g.getheight() > 0:
            # ensure object doesn't move beyond top/bottom edge of grid
            obox = getminbox( g.transform(object, x - prevx, y - prevy) )
            if obox.top < gridt:
               y += gridt - obox.top
            elif obox.bottom > gridb:
               y -= obox.bottom - gridb

         object = g.transform(object, x - prevx, y - prevy)
         oldcells = underneath(object)
         g.putcells(object)
         prevx = x
         prevy = y
         oldmouse = mousepos
         g.update()
Example #13
0
# Fill clicked region with current drawing state.
# Author: Andrew Trevorrow ([email protected]), Jan 2011.

import golly as g
from time import time

# avoid an unbounded fill
if g.empty():
    if g.getwidth() == 0 or g.getheight() == 0:
        g.exit("You cannot fill an empty universe that is unbounded!")
else:
    # set fill limits to the pattern's bounding box
    # (these will be extended below if the grid is bounded)
    r = g.getrect()
    minx = r[0]
    miny = r[1]
    maxx = minx + r[2] - 1
    maxy = miny + r[3] - 1

# allow filling to extend to the edges of bounded grid
if g.getwidth() > 0:
    minx = -int(g.getwidth()/2)
    maxx = minx + g.getwidth() - 1
if g.getheight() > 0:
    miny = -int(g.getheight()/2)
    maxy = miny + g.getheight() - 1

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

def checkneighbor(x, y, oldstate):
    # first check if x,y is outside fill limits
Example #14
0
# Fill clicked region with current drawing state.
# Author: Andrew Trevorrow ([email protected]), Jan 2011.

import golly as g
from time import time

# avoid an unbounded fill
if g.empty():
    if g.getwidth() == 0 or g.getheight() == 0:
        g.exit("You cannot fill an empty universe that is unbounded!")
else:
    # set fill limits to the pattern's bounding box
    # (these will be extended below if the grid is bounded)
    r = g.getrect()
    minx = r[0]
    miny = r[1]
    maxx = minx + r[2] - 1
    maxy = miny + r[3] - 1

# allow filling to extend to the edges of bounded grid
if g.getwidth() > 0:
    minx = -int(g.getwidth() / 2)
    maxx = minx + g.getwidth() - 1
if g.getheight() > 0:
    miny = -int(g.getheight() / 2)
    maxy = miny + g.getheight() - 1

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


def checkneighbor(x, y, oldstate):
def moveselection():
   global oldcells, selrect, selpatt
   
   # wait for 1st click in selection
   while True:
      event = g.getevent()
      if event.startswith("click"):
         # event is a string like "click 10 20 left none"
         evt, xstr, ystr, butt, mods = event.split()
         x = int(xstr)
         y = int(ystr)
         if cellinrect(x, y, selrect):
            oldmouse = xstr + ' ' + ystr
            firstx = x
            firsty = y
            xoffset = firstx - selrect[0]
            yoffset = firsty - selrect[1]
            if mods == "alt":
               # don't delete pattern in selection
               oldcells = g.getcells(selrect)
            break
      elif event == "key h none":
         showhelp1()
      else:
         g.doevent(event)
   
   # wait for 2nd click while moving selection
   g.show("Move mouse and click again..." + helpmsg)
   gotclick = False
   while not gotclick:
      event = g.getevent()
      if event.startswith("click"):
         evt, x, y, butt, mods = event.split()
         mousepos = x+' '+y
         gotclick = True
      else:
         if len(event) > 0:
            lookforkeys(event, x - firstx, y - firsty)
            # update xoffset,yoffset in case selection was rotated
            xoffset = x - selrect[0]
            yoffset = y - selrect[1]
         mousepos = g.getxy()
      if len(mousepos) > 0 and mousepos != oldmouse:
         # mouse has moved, so move selection rect and pattern
         g.clear(0)
         if len(oldcells) > 0: g.putcells(oldcells)
         xstr, ystr = mousepos.split()
         x = int(xstr)
         y = int(ystr)
         selrect[0] = x - xoffset
         selrect[1] = y - yoffset
         
         if g.getwidth() > 0:
            # ensure selrect doesn't move beyond left/right edge of grid
            if selrect[0] < gridl:
               selrect[0] = gridl
               x = selrect[0] + xoffset
            elif selrect[0] + selrect[2] - 1 > gridr:
               selrect[0] = gridr + 1 - selrect[2]
               x = selrect[0] + xoffset
         if g.getheight() > 0:
            # ensure selrect doesn't move beyond top/bottom edge of grid
            if selrect[1] < gridt:
               selrect[1] = gridt
               y = selrect[1] + yoffset
            elif selrect[1] + selrect[3] - 1 > gridb:
               selrect[1] = gridb + 1 - selrect[3]
               y = selrect[1] + yoffset
         
         g.select(selrect)
         oldcells = g.getcells(selrect)
         g.putcells(selpatt, x - firstx, y - firsty)
         oldmouse = mousepos
         g.update()
def rectingrid(r):
   # return True if all of given rectangle is inside grid
   if g.getwidth() > 0 and (r[0] < gridl or r[0] + r[2] - 1 > gridr): return False
   if g.getheight() > 0 and (r[1] < gridt or r[1] + r[3] - 1 > gridb): return False
   return True
Example #17
0
def getstate(x, y):
    # first check if x,y is outside bounded grid
    if g.getwidth() > 0 and (x < gridl or x > gridr): return 0
    if g.getheight() > 0 and (y < gridt or y > gridb): return 0
    return g.getcell(x, y)