如何找到距给定集合及其边界框最远的点
我有一个边界框,里面有许多点。我想添加另一个点,其位置距离任何先前添加的点最远,并且距离框的边缘最远。
对于此类事情有通用的解决方案吗?谢谢!
I have a bounding box, and a number of points inside of it. I'd like to add another point whose location is farthest away from any previously-added points, as well as far away from the edges of the box.
Is there a common solution for this sort of thing? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 Mathematica 程序。
虽然它只有两行代码 (!),但您可能需要更多传统语言的代码,以及能够找到最多函数的数学库。
我假设您不熟悉 Mathematica,所以我将逐行解释和评论。
首先,我们创建一个包含 {0,1}x{0,1} 中 10 个随机点的表,并将其命名为 p。
现在我们创建一个最大化函数:
哈!语法变得棘手!让我们解释一下:
该函数为您提供了 {0,1}x{0,1} 中任意点从该点到我们的集合 p 和边的最小距离。前四项是到边缘的距离,最后一项(我知道很难阅读)是包含到所有点的距离的集合。
接下来我们要做的是最大化这个函数,这样我们就会得到到目标的最小距离最大的点。
但首先让我们看一下 f[]。如果你仔细观察,你会发现它实际上并不是距离,而是距离的平方。我这样定义是因为这样函数更容易最大化并且结果是相同的。
另请注意,f[] 不是一个“漂亮”函数。如果我们在 {0,1} 中绘制它,我们会得到如下内容:
这就是为什么你需要一个很好的数学包找到最大值。
Mathematica 是一个非常好的软件包,我们可以直接最大化事情的结果:
就是这样。 Maximize 函数返回点以及到其最近边界/点的平方距离。
哈!如果您需要翻译成另一种语言的帮助,请发表评论。
编辑
虽然我不是 C# 人员,但在 SO 和谷歌搜索中查找参考资料后,得出了这样的结论:
一个候选包是 DotNumerics
您应该遵循包中提供的以下示例:
HTH!
Here is a little Mathematica program.
Although it is only two lines of code (!) you'll probably need more in a conventional language, as well as a math library able to find maximum of functions.
I assume you are not fluent in Mathematica, so I'll explain and comment line by line.
First we create a table with 10 random points in {0,1}x{0,1}, and name it p.
Now we create a function to maximize:
Ha! Syntax got tricky! Let's explain:
The function gives you for any point in {0,1}x{0,1} the minimum distance from that point to our set p AND the edges. The first four terms are the distances to the edges and the last (difficult to read, I know) is a set containing the distance to all points.
What we will do next is maximizing this function, so we will get THE point where the minimum distance to our targets in maximal.
But first lets take a look at f[]. If you look at it critically, you'll see that it is not really the distance, but the distance squared. I defined it so, because that way the function is much easier to maximize and the results are the same.
Also note that f[] is not a "pretty" function. If we plot it in {0,1}, we get something like:
That's why you will need a nice math package to find the maximum.
Mathematica is such a nice package, that we can maximize the thing straightforward:
And that is it. The Maximize function returns the point, and the squared distance to its nearest border/point.
HTH! If you need help translating to another language, leave a comment.
Edit
Although I'm not a C# person, after looking for references in SO and googling, came to this:
One candidate package is DotNumerics
You should follow the following example provided in the package:
HTH!
您要解决的问题的名称是最大的空球问题。
在平面上可以很容易地在 O(n^4) 时间内求解。只需考虑所有 O(n^3) 个三元组点并计算它们的外心即可。其中一点就是您想要的点。 (好吧,就你的情况而言,你还必须将“一边”视为三点之一,因此你不仅可以找到外心,还可以找到更一般的点,例如与两点和一条边等距的点。)
作为维基百科链接上面表明,该问题也可以通过计算 Voronoi 图在 O(n log n) 时间内解决。更具体地说,您想要的点是您的点的 Delaunay 三角剖分(这是 Voronoi 图的对偶)中的一个三角形的外心,其中只有 O(n)。 (同样,为了完全适应您的问题,您必须考虑盒子侧面的影响。)
The name of the problem you're solving is the largest empty sphere problem.
It can easily be solved in O(n^4) time in the plane. Just consider all O(n^3) triples of points and compute their circumcenter. One of these points is your desired point. (Well, in your case, you also have to consider "a side" as one of your three points, so you not only find circumcenters but slightly more general points, like ones equidistant from two points and a side.)
As the Wikipedia link above indicates, the problem can also be solved in O(n log n) time by computing a Voronoi diagram. More specifically, then your desired point is the circumcenter of one of the triangles in the Delaunay triangulation of your points (which is the dual of the Voronoi diagram), of which there are only O(n). (Again, to adapt exactly to your problem, you'll have to consider the effects of the sides of the box.)