将一组点输入到 clojure 函数
我正在 Clojure 中编写一个算法,它接受一组代表多边形的点。另一个输入是一个点,输出需要是该点是否位于多边形内部。
我的问题是如何将点集输入到函数中? clojure 中哪种数据结构最合适 - 集合、向量、列表等?
I am writing an algorithm in Clojure which takes in a set of points which represents an polygon. Another input is a point, and the output needs to be whether the point lies inside the polygon or not.
My question is how do I input the set of points to the function? What data structure in clojure would be most appropriate - A set, vector, list etc. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据推测,点的顺序很重要,因此形状 ABCD 与形状 ABDC 不一样?
在这种情况下,您需要某种保留顺序的数据结构。这意味着列表或向量是可接受的,但集合则不可接受。
但是您也可以编写函数来接受任何可排序的内容——这样,如果您以后想要从向量更改为列表,反之亦然,则不必更改您的函数。对接口进行编程,而不是对实现进行编程。
Presumably the order of points matters, so that the shape ABCD is not the same as the shape ABDC?
In which case you need some sort of data structure which preserves order. This means that a list or vector is acceptable, but a set is not.
But you could also write your function to take anything seqable -- so that if you later want to change from vector to list or vice versa, you don't have to change your function. Program to an interface, not to an implementation.
通常您应该从最简单的解决方案开始,因此在这种情况下,我可能会使用点列表来完成,每个点表示为二维向量,例如,
这应该适合您的应用程序。
然而,根据您将来最终想要如何使用/操作这些多边形,可以考虑一些替代方案:
Usually you should start with the simplest possible solution, so in this case I'd probably do it with a list of points, with each point expressed as a 2-dimensional vector, e.g.
This should be fine for your application.
However depending on how you ultimately want to use / maniplulate these polygons in the future there are some alternatives to consider: