View difference between Paste ID: 1GFqvxj6 and BtMsDdev
SHOW: | | - or go back to the newest paste.
1
(defn quad-tree
2
  ([c l] {:mid c :len l})
3
  ([c l v] (assoc (quad-tree c l) :value v)))
4
5
(defn next-mid
6
  [qt quad]
7
  (let [[x y] (:mid qt)
8
        d (/ (:len qt) 2)]
9
    (case quad
10
      :I   [(+ x d) (+ y d)]
11
      :II  [(- x d) (+ y d)]
12
      :III [(- x d) (- y d)]
13
      :IV  [(+ x d) (- y d)]
14
      :else (println "Invalid quadrant"))))
15
16
(defn next-quad [[x y] [px py]]
17
  (cond
18
   (and (> px x) (> py y)) :I
19
   (and (< px x) (> py y)) :II
20
   (and (< px x) (< py y)) :III
21
   (and (> px x) (< py y)) :IV))
22
23
(declare add-points)
24
25
(defn add-point
26
  [qt point]
27
  (let [[x y] (:mid qt)
28
        [px py] point
29
        quad (next-quad [x y] point)
30
        val (:value qt)
31
        loc (quad qt)]
32
    (cond
33
     val (-> qt (dissoc :value) (add-points [val point]))
34
     loc (assoc qt quad (add-point loc point))
35
     :else (assoc qt quad (quad-tree (next-mid qt quad) (/ (:len qt) 2) point)))))
36
37
(defn add-points
38
  [qt pts] (reduce add-point qt pts))