 (def state (atom initial-state))
 
 (defn random-cluster [n]
-  (let [cx (+ 50 (rand 700))
-        cy (+ 50 (rand 300))]
+  (let [cx (+ 50 (rand (- (:width svg-size) 100)))
+        cy (+ 50 (rand (- (:height svg-size) 100)))]
     (for [_ (range n)]
       [(+ cx -40 (rand 80)) (+ cy -40 (rand 80))])))
 


(defn add-grid []
  (swap! state (fn [state]
                 (assoc state :points (for [x (range 10 800 20) y (range 10 400 20)] [x y]))))
  (enable-buttons true)
  (visualize))


  (step)
  (when (not= (@state :next-step) :done) (recur)))

(defn reset []
  (swap! state (constantly initial-state))
  (visualize))

(append-button (dom/query "#legend") "Clear" #(do (swap! state (constantly initial-state)) (visualize)))
(append-button (dom/query "#options") "Add Random Cluster" add-random-cluster)
(append-button (dom/query "#legend") "Add Grid" add-grid)
(append-button (dom/query "#legend") "Step" step "step-button")
(append-button (dom/query "#legend") "Run" run "run-button")

(events/on (dom/query "#reset-button") :click reset)
(events/on (dom/query "#step-button") :click step)
(events/on (dom/query "#run-button") :click run)