我一直在尝试思考如何实现一种算法来计算多边形相对于一个点的缠绕数。目前实现如下:(注意更新所以代码有效)
(defn winding-num
"Return winding number of polygon
see Alciatore "
[poly point]
; translate poly such that point is at origin
(let [translated-poly (map #(vec-f - % point) poly)]
; w is wind-num
(loop [vertices translated-poly w 0]
(cond
(= (count vertices) 1)
w
:else
(let [x1 (first (first vertices))
x2 (first (second vertices))
y1 (second (first vertices))
y2 (second (second vertices))]
(cond
(and (< (* y1 y2) 0)
(> (+ x1 (/ (* y1 (- x2 x1))
(- y1 y2)))
0))
(if (< y1 0)
(recur (rest vertices) (inc w))
(recur (rest vertices) (dec w)))
(and (zero? y1)
(> x1 0))
(if (> y2 0)
(recur (rest vertices) (+ w 0.5))
(recur (rest vertices) (- w 0.5)))
(and (zero? y2)
(> x2 0))
(if (< y1 0)
(recur (rest vertices) (+ w 0.5))
(recur (rest vertices) (- w 0.5)))
:else
(recur (rest vertices) w)))))))
我的问题是
- 人们说,在可能的情况下,最好使用比显式递归更高级别的循环构造。例如
map
,for
,reduce
等。 - rest 函数将向量转换为列表
我可以想到一个使用for
和索引的实现,但我也听说最好不要使用索引。
是否有一种惯用的方法来处理在每次迭代中都需要访问连续值的向量算法?