     ar[i] = ar[j]
     ar[j] = t
   ar
+
+# a division of the universe that knows its neighborhood
+class Cell
+  constructor: ( universe, x, y, width ) ->
+    @universe    = universe
+    @x           = x
+    @y           = y
+    @farX        = x + width
+    @farY        = y + width
+    @width       = width
+    @neighbors   = []
+    @inhabitants = []
+    @moved       = []
+  # whether this cell is the appropriate container for the thing
+  has: (thing) ->
+    @x <= thing.x < @farX && @y <= thing.y < @farY
+  # shift the thing to another cell as appropriate
+  move: (thing) ->
+    unless @has thing
+      @rem thing
+      @universe.place thing, true
+  # remove the thing from this cell's purview
+  rem: (thing) ->
+    for t, i in @inhabitants
+      if t == thing
+        @inhabitants.splice i, 1
+        break
+  # the minimum distance between a point in one cell and a point in the other
+  distance: (other) ->
+    if @x < other.x
+      x1 = @farX
+      x2 = other.x
+    else if @x > other.x
+      x1 = @x
+      x2 = other.farX
+    else
+      x1 = x2 = 0
+    if @y < other.y
+      y1 = @farY
+      y2 = other.y
+    else if @y > other.y
+      y1 = @y
+      y2 = other.farY
+    else
+      y1 = y2 = 0
+    x = x1 - x2
+    y = y1 - y2
+    Math.sqrt( x * x + y * y )
+  # introduce potentially neighboring cells to each other
+  introduce: ( other, maxDistance=@universe.maxDistance ) ->
+    d = @distance other
+    if d <= maxDistance
+      @neighbors.push [ other, d ]
+      other.neighbors.push [ @, d ]
+  # collect a candidate set of things potentially within the given radius of the thing
+  # thing should be an inhabitant of this cell
+  near: ( thing, distance=@universe.maxDistance ) ->
+    ret = []
+    for t in @inhabitants when t != thing
+      ret.push t
+      thing.others[t.id] ||= @universe.trig thing, t
+    for n in @neighbors when n[1] <= distance
+      for t in n[0].inhabitants
+        ret.push t
+        thing.others[t.id] ||= @universe.trig thing, t
+    ret
 
 # something in the universe
 class Thing

      c = @canvas
      @ctx.clearRect 0, 0, c.width, c.height
  describe: ->
    map @things, (t) -> t.describe()

  # debugging/development utility methods
  # profiling utility
  time: ( obj, name, time ) ->
    now = new Date()

class Universe
  # default initialization parameters
  defaults: ->
    width:  500
    height: 500
    cell:   20
    pause:  10
    maxDistance: 60
    initialCreatures:
      stones: num: 40
