如何找到距给定集合及其边界框最远的点

发布于 2024-10-03 22:20:22 字数 86 浏览 1 评论 0原文

我有一个边界框,里面有许多点。我想添加另一个点,其位置距离任何先前添加的点最远,并且距离框的边缘最远。

对于此类事情有通用的解决方案吗?谢谢!

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

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

发布评论

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

评论(2

︶葆Ⅱㄣ 2024-10-10 22:20:22

这是一个 Mathematica 程序。

虽然它只有两行代码 (!),但您可能需要更多传统语言的代码,以及能够找到最多函数的数学库。

我假设您不熟悉 Mathematica,所以我将逐行解释和评论。

首先,我们创建一个包含 {0,1}x{0,1} 中 10 个随机点的表,并将其命名为 p

p = Table[{RandomReal[], RandomReal[]}, {10}];

现在我们创建一个最大化函数:

f[x_, y_] = Min[ x^2, 
                 y^2, 
                 (1 - x)^2, 
                 (1 -  y)^2, 
                 ((x - #[[1]])^2 + (y - #[[2]])^2) & /@ p];  

哈!语法变得棘手!让我们解释一下:

该函数为您提供了 {0,1}x{0,1} 中任意点从该点到我们的集合 p 和边的最小距离。前四项是到边缘的距离,最后一项(我知道很难阅读)是包含到所有点的距离的集合。

接下来我们要做的是最大化这个函数,这样我们就会得到到目标的最小距离最大的点。

但首先让我们看一下 f[]。如果你仔细观察,你会发现它实际上并不是距离,而是距离的平方。我这样定义是因为这样函数更容易最大化并且结果是相同的。

另请注意,f[] 不是一个“漂亮”函数。如果我们在 {0,1} 中绘制它,我们会得到如下内容:

alt text

这就是为什么你需要一个很好的数学包找到最大值。

Mathematica 是一个非常好的软件包,我们可以直接最大化事情的结果:

max = Maximize[{f[x, y], {0 <= x <= 1, 0 <= y <= 1}}, {x, y}];

就是这样。 Maximize 函数返回点以及到其最近边界/点的平方距离。

alt text

哈!如果您需要翻译成另一种语言的帮助,请发表评论。

编辑

虽然我不是 C# 人员,但在 SO 和谷歌搜索中查找参考资料后,得出了这样的结论:

一个候选包是 DotNumerics

您应该遵循包中提供的以下示例:

 file: \DotNumerics Samples\Samples\Optimization.cs
 Example header:

  [Category("Constrained Minimization")]
  [Title("Simplex method")]
  [Description("The Nelder-Mead Simplex method. ")]
  public void OptimizationSimplexConstrained()

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.

p = Table[{RandomReal[], RandomReal[]}, {10}];

Now we create a function to maximize:

f[x_, y_] = Min[ x^2, 
                 y^2, 
                 (1 - x)^2, 
                 (1 -  y)^2, 
                 ((x - #[[1]])^2 + (y - #[[2]])^2) & /@ p];  

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:

alt text

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:

max = Maximize[{f[x, y], {0 <= x <= 1, 0 <= y <= 1}}, {x, y}];

And that is it. The Maximize function returns the point, and the squared distance to its nearest border/point.

alt text

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:

 file: \DotNumerics Samples\Samples\Optimization.cs
 Example header:

  [Category("Constrained Minimization")]
  [Title("Simplex method")]
  [Description("The Nelder-Mead Simplex method. ")]
  public void OptimizationSimplexConstrained()

HTH!

我喜欢麦丽素 2024-10-10 22:20:22

您要解决的问题的名称是最大的空球问题

在平面上可以很容易地在 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文