Skip to content

Instantly share code, notes, and snippets.

@catharinejm
Last active December 27, 2015 14:19
Show Gist options
  • Select an option

  • Save catharinejm/7339673 to your computer and use it in GitHub Desktop.

Select an option

Save catharinejm/7339673 to your computer and use it in GitHub Desktop.
Interpolate points with lazy-seq
(ns interpolation.core)
(defn full-list [prtl-lis st end]
(if (<= st end)
(let [lis (drop-while #(< (first %) st) prtl-lis)
slope (fn slope
[[x1 y1 :as p1] [x2 y2 :as p2]]
(if (and p1 p2)
(/ (- y2 y1) (- x2 x1))
0))
interp (fn interp
[ls cur & [prev-pt slp]]
(lazy-seq
(if (<= cur end)
(let [[x y :as pt] (first ls)]
(if (or (nil? x) (> x cur))
(let [slp (if prev-pt
(or slp (slope prev-pt pt)))
[pvx pvy] prev-pt
new-y (if prev-pt
(+ (* slp (- cur pvx)) pvy)
0)
new-pt [cur new-y]]
(cons new-pt
(interp ls (inc cur) prev-pt slp)))
(cons pt (interp (rest ls) (inc cur) pt)))))))]
(interp lis st))))
(def points
[ [ [1, 10], [3, 20], [5, 13] ], [ [2, 13], [3, 50], [6, 10] ]
[ [8 14] [11 6]]])
(defn max-x [lol]
(apply max (map #(or (first (last %)) Float/NEGATIVE_INFINITY) lol)))
(defn min-x [lol]
(apply min (map #(or (ffirst %) Float/POSITIVE_INFINITY) lol)))
(defn infer-points [point-lists]
(let [st (min-x point-lists)
end (max-x point-lists)]
(map #(full-list % st end) point-lists)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment