简体   繁体   English

适合点周围的矩形

[英]Fit rectangle around points

I'm trying to fit a rectangle around a set of 8 2D-Points, while trying to minimize the covered area . 我试图在一组8个2D点周围放置一个矩形,同时尽量减少覆盖区域

Example: 例:

在此输入图像描述

The rectangle may be scaled and rotated. 可以缩放和旋转矩形。 However it needs to stay a rectangle. 但是它需要保持矩形。

My first approach was to brute force each possible rotation, fit the rectangle as close as possible, and calculate the covered area. 我的第一种方法是对每个可能的旋转进行强力逼近,使矩形尽可能接近,并计算覆盖区域。 The best fit would be then the rotation with the lowest area. 最合适的是具有最低面积的旋转。

However this does not really sound like the best solution. 然而,这听起来并不是最好的解决方案。

Is there any better way for doing this? 有没有更好的方法来做到这一点?

I don't know what you mean by "try every possible rotation", as there are infinitely many of them, but this basic idea actually yields a very efficient solution: 我不知道你的意思是“尝试每一个可能的轮换”,因为它们中有无数的,但这个基本思想实际上产生了一个非常有效的解决方案:

The first step is to compute the convex hull. 第一步是计算凸包。 How much this actually saves depends on the distribution of your data, but for points picked uniformly from a unit disk, the number of points on the hull is expected to be O(n^1/3) . 实际节省多少取决于数据的分布,但对于从单位磁盘统一选取的点,船体上的点数预计为O(n ^ 1/3) There are a number of ways to do that : 很多方法可以做到这一点

  • If the points are already sorted by one of their coordinates, the Graham scan algorithm does that in O(n). 如果点已经按其坐标之一进行排序,则Graham扫描算法在O(n)中进行。 For every point in the given order, connect it to the previous two in the hull and then remove every concave point (the only candidate are those neighboring the new point) on the new hull. 对于给定顺序中的每个点,将其连接到船体中的前两个点,然后移除新船体上的每个凹点(唯一的候选者是与新点相邻的那些)。
  • If the points are not sorted, the gift-wrapping algorithm is a simple algorithm that runs at O(n*h). 如果点未被排序,则礼品包装算法是在O(n * h)处运行的简单算法。 For each point on the hull starting from the leftmost point of the input, check every point to see if it's the next point on the hull. 对于从输入的最左侧点开始的船体上的每个点,检查每个点以查看它是否是船体上的下一个点。 h is the number of points on the hull. h是船体上的点数。
  • Chen's algorithm promises O(n log h) performance, but I haven't quite explored how it works. Chen的算法承诺O(n log h)性能,但我还没有完全探究它是如何工作的。
  • another simle idea would be to sort the points by their azimuth and then remove the concave ones. 另一个类似的想法是通过方位角对点进行排序,然后去除凹点。 However, this only seems like O(n+sort) at first, but I'm afraid it actually isn't. 然而,这一开始只看起来像O(n + sort),但我恐怕实际上并非如此。

At this point, checking every angle collected thus far should suffice (as conjenctured by both me and Oliver Charlesworth, and for which Evgeny Kluev offered a gist of a proof ). 在这一点上,检查到目前为止收集的每个角度都应该足够了(由我和Oliver Charlesworth共同完成,并且Evgeny Kluev为此提供了证据的要点 )。 Finally, let me refer to the relevant reference in Lior Kogan's answer . 最后,让我参考Lior Kogan的答案中的相关参考。

For each direction, the bounding box is defined by the same four (not necessarily distinct) points for every angle in that interval. 对于每个方向,边界框由该间隔中的每个角度的相同四个(不一定是不同的)点定义。 For the candidate directions, you will have at least one arbitrary choice to make. 对于候选方向,您将至少有一个任意选择。 Finding these points might seem like an O(h^2) task until you realise that the extremes for the axis-aligned bounding box are the same extremes that you start the merge from, and that consecutive intervals have their extreme points either identical or consecutive. 找到这些点可能看起来像是一个O(h ^ 2)任务,直到您意识到轴对齐边界框的极值与开始合并的极值相同,并且连续间隔的极值点相同或连续。 Let us call the extreme points A,B,C,D in the clockwise order, and let the corresponding lines delimiting the bounding box be a,b,c,d . 让我们以顺时针顺序调用极值点A,B,C,D ,并使界定边界框的相应行为a,b,c,d

So, let's do the math. 那么,让我们做数学。 The bounding box area is given by |a,c| * |b,d| 边界框区域由|a,c| * |b,d| |a,c| * |b,d| . But |a,c| 但是|a,c| is just the vector (AC) projected onto the rectangle's direction. 只是投影到矩形方向上的矢量(AC) Let u be a vector parallel to a and c and let v be the perpendicular vector. u是与ac平行的向量,让v为垂直向量。 Let them vary smoothly across the range. 让它们在整个范围内平滑变化。 In the vector parlance, the area becomes ((AC).v) / |v| * ((BD).u) / |u| 在矢量用语中,区域变为((AC).v) / |v| * ((BD).u) / |u| ((AC).v) / |v| * ((BD).u) / |u| = {((AC).v) ((BD).u)} / {|u| |v|} = {((AC).v) ((BD).u)} / {|u| |v|} {((AC).v) ((BD).u)} / {|u| |v|} . {((AC).v) ((BD).u)} / {|u| |v|} Let us also choose that u = (1,y) . 我们也选择u = (1,y) Then v = (y, -1) . 然后v = (y, -1) If u is vertical, this poses a slight problem involving limits and infinities, so let's just choose u to be horizontal in that case instead. 如果u是垂直的,这会引起一个涉及极限和无穷大的轻微问题,所以让我们在这种情况下选择u是水平的。 For numerical stability, let's just rotate 90° every u that is outside (1,-1)..(1,1) . 对于数值稳定性,我们只需旋转90°,每u即外(1,-1)..(1,1) Translating the area to the cartesian form, if desired, is left as an exercise for the reader. 如果需要,将该区域翻译成笛卡尔形式,留给读者练习。

It has been shown that the minimum area rectangle of a set of points is collinear with one of the edges of the collection's convex hull polygon ["Determining the Minimum-Area Encasing Rectangle for an Arbitrary Closed Curve" [Freeman, Shapira 1975] 已经证明,一组点的最小面积矩形与集合的凸包多边形的一个边缘共线[“确定任意闭合曲线的最小面积包含矩形” [Freeman,Shapira 1975]

An O(nlogn) solution for this problem was published in "On the computation of minimum encasing rectangles and set diameters" [Allison, Noga, 1981] 这个问题的O(nlogn)解决方案发表在“关于计算最小包围矩形和设置直径” [Allison,Noga,1981]

A simple and elegant O(n) solution was published in "A Linear time algorithm for the minimum area rectangle enclosing a convex polygon" [Arnon, Gieselmann 1983] when the input is the convex hull (The complexity of constructing a convex hull is equal to the complexity of sorting the input points). 一个简单而优雅的O(n)解决方案发表在“包含凸多边形的最小面积矩形的线性时间算法” [Arnon,Gieselmann 1983]中,当输入是凸包时( 构造凸包的复杂性相等)对输入点进行排序的复杂性)。 The solution is based on the Rotating calipers method described in Shamos, 1978 . 该解决方案基于Shamos,1978中描述的旋转卡尺方法。 An online demonstration is available here . 这里有在线演示。

They first thing that came to mind when I saw this problem was to use principal component analysis. 当我看到这个问题时,首先想到的是使用主成分分析。 I conjecture that the smallest rectangle is the one that satisfies two conditions: that the edges are parallel with the principal axes and that at least four points lie on the edges (bounded points). 我猜想最小的矩形是满足两个条件的矩形:边缘与主轴平行,边缘至少有四个点(有界点)。 There should be an extension to n dimensions. 应该有n维的扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM