A simple problem: Growing a square in a cloud of points

Started by
15 comments, last by stevesan 12 years, 2 months ago
Here's my Theta(n^2) try for the original problem:



struct Point
{
int x,y;
};

int MinAbsDistance(const Point &a, const Point &b)
{
return min(abs(a.x - b.x), abs(a.y - b.y));
}

/// Returns the size of the largest square centered at the point at index 'centerIndex'.
int SquareSizeCenteredAt(Point *points, int numPoints, int centerIndex)
{
int squareSize = INT_MAX;
for(int i = 0; i < numPoints; ++i)
if (i != centerIndex)
squareSize = min(squareSize, MinAbsDistance(points, points[centerIndex]));
return squareSize;
}

/// Finds the position and size of the largest square centered at some point of the input array.
void FindLargestSquareCenteredAt(Point *points, int numPoints, int *outCenterIndex, int *outSquareSize)
{
*outSquareSize = 0;
// Try each point in turn for the center point.
for(int i = 0; i < numPoints; ++i)
{
// What's the largest square that can be centered around this point?
int size = SquareSizeCenteredAt(points, numPoints, i);
// Beat the previous found record?
if (size > *outSquareSize)
{
*outSquareSize = size;
*outCenterIndex = i;
}
}
}


I ran it through the compiler to see it builds, but have not actually run it even once. It is not applicable as-is for your modified problem, where the center of the square does not have to lie at a center of an existing point.
Advertisement
@clb - wow thanks for taking the time to write that! Unfortunately I think it suffers from the same issue as I mentioned above:

Furthermore, unless I'm misunderstanding your suggestion, I don't see how that would work for my drawing here: http://www.makeitsha...om/drawing/8753 The minimum distance point from the red center would be the black point furthest to the right - it is closest to the center in the Y direction, and using that distance would make the square too small. I also assume you don't mean the Euclidian distance, as that would make the square too big. Am I misunderstanding?

@clb - wow thanks for taking the time to write that! Unfortunately I think it suffers from the same issue as I mentioned above: [quote name='stevesan' timestamp='1328805147' post='4911350']
Furthermore, unless I'm misunderstanding your suggestion, I don't see how that would work for my drawing here: http://www.makeitsha...om/drawing/8753 The minimum distance point from the red center would be the black point furthest to the right - it is closest to the center in the Y direction, and using that distance would make the square too small. I also assume you don't mean the Euclidian distance, as that would make the square too big. Am I misunderstanding?

[/quote]

Ah right, I got the min/max wrong way around. Replace the MinAbsDistance with the MaxAbsDistance metric, i.e. return return max(abs(a.x - b.x), abs(a.y - b.y));
@clb - Ah yes, I think it would work then. It's essentially the same as splitting up the points into 4 diagonally-delineated regions, but the max(abs(...)) formulation is certainly cleaner :)

The only ingredient missing from the solution is that the square should be allowed to move around, ie. it doesn't need to be centered at any point. So if it can shift up and get even larger, it should do that. I think this can be done using the following iteration:

- Grow the square using the algo above for a single given center point, call it s0
- Note which side is "touching" a point in s0, and "fix" it.
- Repeat the algo above, except instead now pretend that you're growing a 1:2 aspect rectangle centered at the midpoint of the fixed edge. So, instead of min(abs(dx), abs(dy)), it'd be min( abs(dx), 2*abs(dy) ) if the bottom edge was fixed. And you would also ignore all points at-and-below the bottom edge.

That's probably a bit confusing to just read...but I'll try implementing it soon and see how it goes.
Sorry, should be min(abs(..)) up there again.
Sorry, should be max(abs(...)) up there again.
Update: The problem has changed slightly. Instead of maximizing a square inside a cloud of points, now I have a set of planes (or oriented lines to use 2D terms) that restrict the square. And the cool thing is, it now becomes an instance of linear programming! The 3 variables are X and Y for the location of the center and L for the width/height of the square. Each plane defines four constraints, one for each corner. And our objective function is simply L.

It's a fairly small problem with only 3 variables and probably 20 constraints tops, so I'm hoping this will be doable on a per-frame basis when necessary. I'm gonna implement Simplex in JavaScript (heh) and see how it goes...
UPDATE: I implemented a linear programming formulation (along with a Simplex solver in JavaScript..heh), and it works quite well! There are some issues which I need to investigate, but it's quite robust 90% of the time. I'll post some videos and some code soon if there is interest..

This topic is closed to new replies.

Advertisement