A simple problem: Growing a square in a cloud of points
#1 Members - Reputation: 134
Posted 09 February 2012 - 06:59 AM
I need the solution to keep it a square, and while I don't need global optimality, I do need the edge to go right up to the points. Any thoughts? I haven't had much sleep..so maybe my brain just isn't working and it's actually a really easy problem...
#4 Members - Reputation: 1147
Posted 09 February 2012 - 08:02 AM
#5 Members - Reputation: 134
Posted 09 February 2012 - 08:46 AM
Does the box need to be axis-aligned or oriented?
Yes, it needs to be axis-aligned. Not sure if oriented would be any easier...?
Get the farthest point relative to the center point on both x and y axis and resize the rectangle to those values?
I need the square to NOT contain any of the points (except for the center one, to avoid the trivial solution where you just put the square outside the point cloud).
#7 Crossbones+ - Reputation: 910
Posted 09 February 2012 - 09:37 AM
Does the box need to be axis-aligned or oriented?
Yes, it needs to be axis-aligned. Not sure if oriented would be any easier...?
Oriented would be harder. You can get at axis-aligned solution by just finding the point with the minimum distance in the x or y direction from your central point. This will define the half-width of the square.
-Josh
#8 Members - Reputation: 134
Posted 09 February 2012 - 10:32 AM
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?
EDIT: Hmm I think for the fixed-center case, you can split up the points into 4 regions delineated by the 2 diagonals of the square. So all points in the "top" and "bottom" regions, you consider their "grow distance" to be the Y distance to the center, and for the "left" and "right" region you consider it the X distance. Then, you can find the point with the minimum "grow distance" and use that as your half-width.
Still thinking about the movable case..perhaps applying this iteratively would work...
Thanks for taking the time to reply!
#9 Members - Reputation: 134
Posted 09 February 2012 - 10:37 AM
Humm...I hope this doesn't end up being some NP-complete or high-order polynomial algorithm where I have to brute force it (ie. try placing edges at every point and enumerating..??)...
#10 Members - Reputation: 521
Posted 09 February 2012 - 01:25 PM
1. Loop through all points and find lowest distance between center point and other points;
2. Create centered square in such way that the closest point would be on the line;
3. Test whether you have any extra points inside (extra test), and make square smaller by penetration distance.
#11 Members - Reputation: 1604
Posted 09 February 2012 - 05:16 PM
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[i], 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.
#12 Members - Reputation: 134
Posted 09 February 2012 - 05:20 PM
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?
#13 Members - Reputation: 1604
Posted 09 February 2012 - 05:30 PM
@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?
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));
#14 Members - Reputation: 134
Posted 09 February 2012 - 06:37 PM
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.
#16 Members - Reputation: 134
Posted 15 February 2012 - 01:38 AM
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...
#17 Members - Reputation: 134
Posted 22 February 2012 - 11:45 AM






