   (assoc state :points
          (generate-points (:n state) (:size state))))
 
-(defn change-n
-  "Updates n - number of points. fn is a function using which n is updated,
-  either inc or dec. Regenerates points after that."
-  [state fn]
-  (-> state
-      (update-in [:n] #(max 3 (fn %)))
-      regenerate-points))
-
-(defn key-pressed
-  "Process key event.
-  r - regenerate points
-  m,l - increase/decrease number of points and regenerate them
-  space - pause sketch"
-  [state event]
-  (condp = (:key event)
-    :r (regenerate-points state)
-    :up (change-n state inc)
-    :down (change-n state dec)
-    (keyword " ") (update-in state [:running?] not)
-    state))
-
-
 (defrecord EquilibriumLayer [state]
   Layer
   (setup-layer-state [this]
-                     (setup))
+    (setup))
   (update-layer-state [this state]
-                      (update-state state))
+    (update-state state))
   (draw-layer-state [this state]
-                    (draw-state state)))
-
-; (q/defsketch equilibrium
-;     :host "host"
-;     :size [500 500]
-;     :setup setup
-;     :update update-state
-;     :draw draw-state
-;     :mouse-pressed mouse-pressed
-;     :mouse-dragged mouse-dragged
-;     :key-pressed key-pressed
-;     :middleware [m/fun-mode])
+    (draw-state state)))
+
+;; (q/defsketch equilibrium
+;;     :host "host"
+;;     :size [500 500]
+;;     :setup setup
+;;     :update update-state
+;;     :draw draw-state
+;;     :mouse-pressed mouse-pressed
+;;     :mouse-dragged mouse-dragged
+;;     :key-pressed key-pressed
+;;     :middleware [m/fun-mode])

    (q/stroke 255 0 0)
    (draw-point p1)
    (draw-point p2)
    (q/stroke-weight 1)
    (q/line (+ mx a) (+ my b) (- mx a) (- my b))))

(defn- draw-state
  "Draws sketch state."
  [{:keys [points] :as state}]
  ;;(q/background 250)
  ; Use different point size depending on
  ; screen size.
  (let [weight (q/map-range (:size state)
                            200 500
                            7 10)]
    (q/stroke-weight weight))
  (q/stroke 0)
  (doseq [p points]
    (draw-point p))
  ; (doseq [ind (find-points points (q/mouse-x) (q/mouse-y))]
  ;   (draw-equidistance points (points ind))))
   (if (< (rand-int 30) 15)
    (doseq [ind (find-points points (rand-int (q/width)) (rand-int (q/width)))]
      (draw-equidistance points (points ind)))))

;;;
;;; User interaction
;;;

(defn mouse-dragged
  "Drags the selected point (if any). Sets coordinates of the selected
  point to be equals to the current mouse position."
  [state event]
  (if-let [ind (:dragging state)]
    (update-in state [:points ind] merge (select-keys event [:x :y]))
    state))

(defn mouse-pressed
  "Selects a point for dragging. Adds the point index to the state."
  [state event]
  (let [ind (-> (:points state)
                (find-points (:x event) (:y event))
                first)]
    (assoc state :dragging ind)))

(defn regenerate-points
  "Regenerates points in the state map."

          n (int (q/map-range size
                              200 500
                              15 25))]
      {:points (generate-points n size)
       :running? true
       :dragging nil
       :n n
       :size size})))

;;;
;;; Draw
;;;

(defn draw-point [p]
  (do
    (q/stroke (:r p) (:g p) (:b p))
    (q/point (:x p) (:y p))))

(defn find-points
  "Finds all points in 10-pixel approximity of point (x,y)."
