我有一个x,y点列表存储为矢量矢量,我想找出界限.
例如,鉴于此:
[[0 0] [20 30] [-50 -70] [200 300]]
Run Code Online (Sandbox Code Playgroud)
结果将是:
{:x -50, :y -70, :x2 200, :y2 300}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所拥有的.它给出了理想的结果,但看起来很冗长,而且对我来说并不是很好.
(defn get-stroke-bounds [vector-of-points]
(reduce (fn [m [x y]]
{:x (min (:x m Integer/MAX_VALUE) x)
:y (min (:y m Integer/MAX_VALUE) y)
:x2 (max (:x2 m Integer/MIN_VALUE) x)
:y2 (max (:y2 m Integer/MIN_VALUE) y)})
{}
(vector-of-points)))
Run Code Online (Sandbox Code Playgroud)
关于如何改进它的任何想法?谢谢!
如果我已经使用向量作为输入点,我希望返回值采用相同的格式。考虑到这一点,我认为这是一个很好的惯用解决方案:
(defn bounds
[points]
(let [xs (sort (map first points))
ys (sort (map second points))]
(list [(first xs) (first ys)]
[(last xs) (last ys)])))
Run Code Online (Sandbox Code Playgroud)