I\'m trying to fit a rectangle around a set of 8 2D-Points, while trying to minimize the covered area.
Example:
The rectangle may be sc
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). There are a number of ways to do that:
h
is the number of points on the hull.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). Finally, let me refer to the relevant reference in Lior Kogan's answer.
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. 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
.
So, let's do the math. The bounding box area is given by |a,c| * |b,d|
. But |a,c|
is just the vector (AC)
projected onto the rectangle's direction. Let u
be a vector parallel to a
and c
and let v
be the perpendicular vector. Let them vary smoothly across the range. In the vector parlance, the area becomes ((AC).v) / |v| * ((BD).u) / |u|
= {((AC).v) ((BD).u)} / {|u| |v|}
. Let us also choose that u = (1,y)
. Then 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. For numerical stability, let's just rotate 90° every u
that is outside (1,-1)..(1,1)
. Translating the area to the cartesian form, if desired, is left as an exercise for the reader.