• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
stevesan

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

16 posts in this topic

I've encountered a problem in a 2d game I'm working on right now that seems super simple, but I can't seem to find an optimal solution for it. I want to have a box that's as large as possible, which contains a single point (let's assume the middle) but does not contain any other points. [url="http://www.makeitshareit.com/drawing/8753"]A drawing here[/url]

I need the solution to [i]keep it a square[/i], 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...
0

Share this post


Link to post
Share on other sites
And in case you're curious, here's the [url="http://pupilgame.wordpress.com"]dev blog for the game[/url].
0

Share this post


Link to post
Share on other sites
Get the farthest point relative to the center point on both x and y axis and resize the rectangle to those values?
0

Share this post


Link to post
Share on other sites
[quote name='jjd' timestamp='1328793072' post='4911282']
Does the box need to be axis-aligned or oriented?
[/quote]

Yes, it needs to be axis-aligned. Not sure if oriented would be any easier...?

[quote name='Waterlimon' timestamp='1328796165' post='4911299']
Get the farthest point relative to the center point on both x and y axis and resize the rectangle to those values?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='stevesan' timestamp='1328798783' post='4911316']
[quote name='jjd' timestamp='1328793072' post='4911282']
Does the box need to be axis-aligned or oriented?
[/quote]

Yes, it needs to be axis-aligned. Not sure if oriented would be any easier...?

[/quote]

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
0

Share this post


Link to post
Share on other sites
I don't need the square to actually be centered about the central point. So it should be allowed to move around, as long as it still [i]contains [/i]the central point.

Furthermore, unless I'm misunderstanding your suggestion, I don't see how that would work for my drawing here: [url="http://www.makeitshareit.com/drawing/8753"]http://www.makeitsha...om/drawing/8753[/url] 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!
0

Share this post


Link to post
Share on other sites
After some digging, it is [i]similar[/i] although not quite this problem here: [url="http://chaoxuprime.com/2011/02/an-open-problem-axis-aligned-rectangle-packing-density"]http://chaoxuprime.com/2011/02/an-open-problem-axis-aligned-rectangle-packing-density[/url]

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

Share this post


Link to post
Share on other sites
I can only think of a way to make centered square. Not sure how correct it'll be.

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

Share this post


Link to post
Share on other sites
Here's my Theta(n^2) try for the original problem:

[code]

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;
}
}
}
[/code]

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

Share this post


Link to post
Share on other sites
@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]
0

Share this post


Link to post
Share on other sites
[quote name='stevesan' timestamp='1328829600' post='4911479']
@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: [url="http://www.makeitsha...om/drawing/8753"]http://www.makeitsha...om/drawing/8753[/url] 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]
[/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));
0

Share this post


Link to post
Share on other sites
@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.
0

Share this post


Link to post
Share on other sites
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...
0

Share this post


Link to post
Share on other sites
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..
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0