我认为在这样的情况下,由于功能太小,仅仅编写函数比引入依赖关系更简单。
(defprotocol Line
(gradient [this] "The gradient of a line")
(intercept [this] "The intercept of a line on the y axis")
(f [this] "The line's function f(x) - takes x, returns y")
(f-inv [this] "The inverse function f-inv(x) - takes y, return x"))
(defn line
"Make a Line from a gradient and an intercept"
[gradient intercept]
(reify Line
(gradient [_] gradient)
(intercept [_] intercept)
(f [_] (fn [x] (+ (* gradient x) intercept)))
(f-inv [_] (fn [y] (/ (- y intercept) gradient)))))
(defn points->line
"Make a Line given two (different) points on the line"
[[x1 y1] [x2 y2]]
(let [gradient (/ (- y2 y1)
(- x2 x1))
intercept (- y1 (* gradient x1))]
(line gradient intercept)))
例子:
(def l (points->line [1 1] [4 2]))
(gradient l) ;=> 1/3
(intercept l) ;=> 2/3
((f l) 4) ;=> 2
((f-inv l) 2) ;=> 4
如果你不能找到它的[GitHub的(?https://github.com/clojure)或[Clojars(https://clojars.org/search?q = geometry),那么它可能不存在。为什么不创建一个? – ChrisDevo
看起来我可能不得不自己滚动它,因为我还没有找到任何东西。 –
https://github.com/danielgrigg/sligeom上有一些代码,尽管我没有时间去查看这是否是您需要的。如果是这样,请用它回答你自己的问题。 HTH – ClojureMostly