将一组点输入到 clojure 函数

发布于 2024-10-19 19:36:00 字数 125 浏览 1 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2024-10-26 19:36:00

据推测,点的顺序很重要,因此形状 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.

暮凉 2024-10-26 19:36:00

通常您应该从最简单的解决方案开始,因此在这种情况下,我可能会使用点列表来完成,每个点表示为二维向量,例如,

(def my-polygon (list [0 0] [1 0] [1 1] [0 1]))

这应该适合您的应用程序。

然而,根据您将来最终想要如何使用/操作这些多边形,可以考虑一些替代方案:

  • 如果您希望与 Java 代码进行互操作(例如,使用 Swing/Java2D 在框架中绘制多边形),您可能需要使用实例适当的 Java 类(例如 java.awt.geom.Point2D)作为各个点。这将为您提供更少惯用的 Clojure 代码,但将为您提供更好的 Java 互操作性
  • 您可能想要使用向量而不是列表 - 特别是如果您要使用大多边形并应用不同的算法需要对各个点进行索引访问

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.

(def my-polygon (list [0 0] [1 0] [1 1] [0 1]))

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:

  • If you want interoperability with Java code (e.g. drawing the polygon in a frame using Swing/Java2D), you might want to use instances of an appropriate Java class (e.g. one of the subclasses of java.awt.geom.Point2D) as the individual points. This will give you less idiomatic Clojure code, but will give you better Java interop
  • You might want to use a vector instead of a list - particularly if you are going to be using large polygons and applying different algorithms that need indexed access to the individual points
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文