How to find the point most distant from a given set and its bounding box

后端 未结 2 1591
小蘑菇
小蘑菇 2021-02-06 09:05

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

相关标签:
2条回答
  • 2021-02-06 09:24

    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.)

    0 讨论(0)
  • 2021-02-06 09:30

    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!

    0 讨论(0)
提交回复
热议问题