Algorithm to determine (x,y) coordinates for rectangles so the area of the surrounding rectangle is minimal? - algorithm

I hope my title makes sense, but here is what I a trying to do:
I have n rectangles, each with widths of Wn and heights of Hn that I need to arrange so on a two dimensional (x,y) plane the rectangle that they all fit in takes up the least area. I also need to be able to establish what (x,y) corresponds to what rectangle.
I would prefer something in psuedo-code, but can work with many languages.
Thanks for helping.

This is a difficult problem to be solved optimally, however there are some solutions that are not too hard to implement and that represent a good approximations for many uses (e.g. for textures). Try googling for "rect(angle) packing" ... you will find a lot of code that solves the problem.

Sounds NP-complete to me. Kinda like the knapsack problem. Which means there is no real solution. Only good approximations.

It's a variant on 2D bin packing. The fact that your container is flexible, allows for more optimization as well making it more challenging (as in difficult). Drools Planner (open source java) can handle this. At least one implementation (see user mailing list) with Drools Planner exists (not open source). If you need an open source example to start from, probably the cloud balance in drools-planner-examples is a good example to start from.
For an overview of the algorithms you can use, see my answer on a similar question.

Your problem is known as the 2D packing problem. Even the 1D problem is NP-hard. See here for a good article on some approaches along with sample C# code.
Also, see the following questions:
How can I programmatically determine how to fit smaller boxes into a larger package?
Where can I find open source 2d bin packing algorithms?

Related

Subdividing a polygon into boxes of varying size

I would like to be pointed to information / resources for creating algorithms like the one illustrated on this blog, which is a subdivision of a polygon (in my case a voronoi cell) into several boxes of varying size:
http://procworld.blogspot.nl/2011/07/city-lots.html
In the comments a paper by among others the author of the blog can be found, however the only formula listed is about candidate location suitability:
http://www.groenewegen.de/delft/thesis-final/ProceduralCityLayoutGeneration-Preprint.pdf
Any language will do, but if examples can be given Javascript is preferred (as it is the language i am currently working with)
A similar question is this one: https://gamedev.stackexchange.com/questions/27055/what-is-an-efficient-packing-algorithm-for-packing-rectangles-into-a-polygon
[edit]: I have found something to start with, but it is not what i was looking for entirely:
http://www2.stetson.edu/~efriedma/squintri/
I have solved my problem in a completely different, easier way.
As i was looking for my problem, it turned out to be a fairly complex one, both measured in difficulty to implement as algorithm (my opinion) and algorithm complexity class(es).
If anyone is having a similar problem, these problems are classified as 'packing problems' in general, with specific problems like the 'pallet loading problem'.
The problem i was interested in, is illustrated at the bottom of this page:
https://www.ime.usp.br/~egbirgin/packing/
and a paper about this problem, with algorithm descriptions of how to solve the packing problem for convex polygons and curved shapes:
http://www.ime.usp.br/~egbirgin/publications/bmnr.pdf
Some more information on these kinds of problems:
http://lagrange.ime.usp.br/~lobato/utdc/
http://mathworld.wolfram.com/SquarePacking.html

Algorithm for filling container with elements, knowing their vertical position

My problem is a bit similar to this topic :
Fit elements into box
Although in my case I have a container of some size, and some predefined elements with know vertical positions. The elements are to be allocated inside the container without overlapping, and taking full available width. If two or more would overlap, they should have the same width.
Anyone knows any algorithms that may be helpful here ?
Well one way would be to model this problem as a convex optimization problem and then use a solver to solve it (tons of solvers are available online). You can find more info about this approach in floor planning chapter (page 438) of Boyds convex optimization book. They have a matlab implementation of it in the examples folder of cvx software. I hope I haven't made it more complicated, it would be interesting to see other solutions.

Is there some well-known algorithm which turns user's drawings into smoothed shapes?

My requirements:
A user should be able to draw something by hand. Then after he takes off his pen (or finger) an algorithm smooths and transforms it into some basic shapes.
To get started I want to transform a drawing into a rectangle which resembles the original as much as possible. (Naturally this won't work if the user intentionally draws something else.) Right now I'm calculating an average x and y position, and I'm distinguishing between horizontal and vertical lines. But it's not yet a rectangle but some kind of orthogonal lines.
I wondered if there is some well-known algorithm for that, because I saw it a few times at some touchscreen applications. Do you have some reading tip?
Update: Maybe a pattern recognition algorithm would help me. There are some phones which request the user to draw a pattern to unlock it's keys.
P.S.: I think this question is not related to a particular programming language, but if you're interested, I will build a web application with RaphaelGWT.
The Douglas-Peucker algorithm is used in geography (to simplify a GPS track for instance) I guess it could be used here as well.
Based on your description I guess you're looking for a vectorization algorithm. Here are some pointers that might help you:
https://en.wikipedia.org/wiki/Image_tracing
http://outliner.codeplex.com/ - open source vectorizer of the edges in the raster pictures.
http://code.google.com/p/shapelogic/wiki/vectorization - describes different vectorization algorithm implementations
http://cardhouse.com/computer/vector.htm
There are a lot of resources on vectorization algorithms, I'm sure you'll be able to find something that fits your needs. I don't know how complex these algorithms are to implement them, though,

Position elements without overlap

I have a number of rectangular elements that I want to position in a 2D space. I calculate an ideal position for each element. Now my problem is that many elements overlap as very often the ideal positions are concentrated in one region. I want to avoid overlap as much as possible (doesn't have to be perfect, though). How can I do this?
I've heard physics simulations are suitable for this - is that correct? And can anyone provide an example/tutorial?
By the way: I'm using XNA, if you know any .NET library that does exactly this job - tell me!
One way the physics engine can be used:
Put positive electric charges (or some kind of repulsive force) on each rectangles and simulate the forces and movements. Also, as Eyal was kind enough to point out, you also need some attractive forces to keep them from drifting away. This can be modelled by springs (again as Eyal points out). They will hopefully end up in some sort of equilibrium which might involve non-overlapping rectangles.
I believe similar ideas (force based heuristics) are used in determining nice looking layouts of graphs (the nodes and edges one).
Disclaimer: I haven't used this myself.
Hope that helps!
Box2D is a widly used (free) physics library that can achieve the needed task: Link
The algorithm that you are looking for is linear interpolation. XNA has its own lerp function.

Position boxes like in Exposé

Does anyone know a way to locate rectangles to best fit a certain area? The rectangles can be scaled up to a certain limit, but they should keep their proportions.
I basically want to rebuild Mac OS' Exposé:
Picture
Thanks,
eWolf
The problem is NP Hard, but that is only for the optimal solution. What I guess you really want is a solution that just looks good.
So I suggest looking for algorithms that make data look good. Once I wanted to layout pictures for the DeepZoom Composer and I tried to recycle a graph drawing force-interaction based algorithm. http://en.wikipedia.org/wiki/Force-based_algorithms
It worked really well even for 600 hundred rectangles, you just have to play with the details of the simulation.
How the distance is calculated?
What functions do you use for the attraction and repulsion forces?
How much overlap are you going to accept?
The only problem I could not solve entirely, was that the rectangles formed a circle shape, rather then a big rectangle shape, which is natural when you are simulating physics. But you can work around that with carefully placed auxiliary force sources.

Resources