 E("btn-set-rule").addEventListener "click", doSetRule
 E("btn-set-grid").addEventListener "click", doSetGrid
 
-test_drawOrder2NMeighbors = ->
-  coordsWithPaths = []
-  
-  #Initial cell
-  cell = null
-  
-  for nei in getNeighbors cell
-    for nei2 in getNeighbors nei
-      if cells.get nei2
-        console.log "already present! #{showNode nei2}"
-      else
-        cells.put nei2, 1
-        cellCenter = M.mulv nodeMatrixRepr(nei2, tessellation.group), [0,0,1]
-        found = false
-        for [z, paths], i in coordsWithPaths
-          if M.approxEqv z, cellCenter
-            console.log "Path #{showNode nei2} already has equals:"
-            for p, j in paths
-              console.log "  #{j+1}) #{showNode p}"
-              #throw new Error "Stop"
-            paths.push nei2
-            found = true
-            break
-        if not found
-          console.log "First occurence of #{showNode nei2}"
-          coordsWithPaths.push [cellCenter, [nei2]]
-            
-
-  console.log "Population is #{cells.count}"    
-  console.log JSON.stringify rewriteRuleset
-    
-  #cells.put null, 1
-  #cells.put newNode('a', 1, newNode('b',1,null)), 1
-  #cells.put newNode('a', 1, newNode('b',1,newNode('a', 2, newNode('b',1,null)))), 1
-                          
-
-  context.save()
-  s = Math.min( canvas.width, canvas.height ) / 2
-  context.scale s, s
-  context.translate 1, 1
-
-  drawCells cells, tfm, tessellation, context
-  context.restore()
-
-#test_drawOrder2NMeighbors()
+E('rule-entry').value = transitionFunc.toString()
+redraw()

  xyk = dx*dy*k
  yyk = dy*dy*k
  
  moveMatrix =[xxk+1, xyk,   dx,
               xyk,   yyk+1, dy,
               dx,     dy,     dt]
  tfm = M.mul moveMatrix, tfm
  redraw()
    
rotateView = (angle) ->
  s = Math.sin angle
  c = Math.cos angle
  rotMatrix = [c, s, 0.0,
               -s,  c, 0.0,
               0,  0, 1.0]
  tfm = M.mul rotMatrix, tfm
  redraw()        
  
redraw()
class MovingDragger
  constructor: (@x0, @y0) ->
  mouseMoved: (e)->
    [x, y] = getCanvasCursorPosition e, canvas

  redraw()

doStep = ->
  cells = evaluateWithNeighbors cells, getNeighbors, transitionFunc.evaluate.bind(transitionFunc)
  redraw()

#Flag to avoid requestAnimationFrame congestion. It really helps a lot!
frameRequested = false
redraw = ->
  s = Math.min( canvas.width, canvas.height ) / 2

  if not frameRequested
    frameRequested = true
    window.requestAnimationFrame ->
      frameRequested = false
      context.clearRect 0, 0, canvas.width, canvas.height
      context.save()
      context.scale s, s
      context.translate 1, 1
      drawVisibleCells visibleCells, cells, tfm, tessellation, context
      context.restore()

updatePopulation = ->
  #console.log "Redraw. Population is #{cells.count}"
  E("population").innerHTML = ""+cells.count

toggleCellAt = (x,y) ->
  s = Math.min( canvas.width, canvas.height ) * 0.5
