     state))
 
 (defn- scale [state xy]
-  (map-indexed #(* %2 (/ (/ (window-size %1) 2)
-                         (get-in state [:grid-size %1]) )) xy))
+  (map-indexed #(* %2 (float (/ (window-half-size %1)
+                                (get-in state [:grid-size %1])))) xy))
 
 ; each bound is a position vector
 (defn- in-bounds? [min-bound max-bound pos]

      (doseq [line lines]
        (apply q/line (apply concat (map #(scale state %) line))))

      (q/stroke 0xFF000000)
      (q/fill 0xFFFFFFFF)
      (doseq [leef leaves]
        (let [pos (:pos leef)]
          (ellipse state pos [0.5 0.5])
          ))

      (q/stroke 0xFF000000)
      (q/fill 0xFF000000)
      (doseq [active-node active]
        (let [pos (:pos active-node)]
          (ellipse state pos [0.5 0.5])
          ))
      ))

    (when-not (zero? (:gif-seconds state))
      (let [anim-frames (* (:gif-seconds state) (:frame-rate state))]
        (gil/save-animation "quil.gif" anim-frames 0)
        (when (> (:frame state) anim-frames) (q/exit))))
  )

(defn main []

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def window-size [1000 1000])
(def window-half-size (apply vector (map #(float (/ %1 2)) window-size)))

(defn- new-state []
  {:frame-rate 30
   :exit-wait-frames 40
   :tail-length 40
   :frame 0
   :gif-seconds 0
   :grid-size [50 50] ; width/height from center
   :ghost (-> (ghost/new-ghost grid/euclidean)
              (ghost/new-active-node [0 0])
              )
   })

(defn- curr-second [state]
  (float (/ (:frame state) (:frame-rate state))))

(defn- positive [n] (if (> 0 n) (- n) n))

(defn- spawn-chance [state]
  (let [period-seconds 1
        period-frames (* (:frame-rate state) period-seconds)]
  (if (zero? (rem (:frame state) period-frames))
    1 100)
    ))

  ;(let [period-seconds 1
  ;      rad-per-second (float (/ (/ Math/PI 2) period-seconds))
  ;      rad (* rad-per-second (curr-second state))
  ;      chance-raw (positive (q/sin rad))
  ;      ]
  ;    (if (> chance-raw 0.97) 3 50)
  ;  ))


(defn- mk-poss-fn [state]
  (let [chance (spawn-chance state)]
    (fn [pos adj-poss]
      (if (zero? (rand-int chance))
        adj-poss
        (take 1 (shuffle adj-poss))))
    ))

(defn setup []
  (let [state (new-state)]
