Find largest rectangle inside an SVG path - algorithm

Given an irregular shape made by an SVG path, how do you calculate the largest rectangle (with only horizontal and vertical borders) that can fit inside it?

I don't think you can find the largest rectangle in the general case. You should better consider the problem to find the largest rectangle that fits inside a shape that is drawn on a grid, it will give you a good approximation of what you are looking for and by decreasing the step of the grid, you can increase the precision of your approximation.
On a grid the problem can be solved in O(n) where n is the number of cells in the grid.

An SVG path consists of segments of lines, cubic Bezier paths, quadratic Bezier paths, and elliptic arcs. Therefore it is piecewise differentiable. It consists of a finite number of segments, not an infinite recurrence. Don't laugh, things like that can be represented easily in a "lazy" programming language such as Haskell, but they're not allowed in SVG. In particular, although an SVG path can look like a fractal to our eyes, it can't mathematically be a fractal. Furthermore the constants can only be integers or IDL floats, which are IEEE single-precision floating-point numbers. So the resolution of a grid that would have all of those numbers at grid points might be considered large, but it is surely finite.
Using those facts I claim that in general, if an SVG path encloses an area, then there exists a largest area for rectangles enclosed in the path; and that there is a tractable algorithm to find (at least) one rectangle whose area is the largest area.
Any algorithm needs to account for difficult cases such as (approximations to) space-filling curves, which could have a large number of small but still "largest" rectangles. I don't know an algorithm, so we can consider here how to develop one. Could you solve the problem for paths made only of line segments? Would a mesh generation algorithm help? Does it help to consider that rectangles that have the same center and area have their corners on a pair of hyperbolas? Does it help to know about convex hull algorithms? Would you need the differential calculus method called max-min, or perhaps not? Then, how would you extend your algorithm to allow the other types of path segments? Would it be necessary, or helpful, or unnecessary to approximate those path segments as polygonal paths?

Related

Algorithmic complexity of finding subset of 3D points in cube

Given an array of 3D integers, what is the algorithmic complexity of determining which of those integers exist within a cube? I'm assuming the points can be represented in a number of concurrent data structures, each sorted in one or more dimensions.
My intuition tells me given a sorted array of points in 1D one can determine the subset of points between some lower and upper bound in something like O(log(n), but I would be very grateful for any insights others can offer on this notion (and any help others can offer generalizing to the multidimensional case!).
If you're unfamiliar with the math involved, I recommend doing this problem in two dimensions first, with a rectangle. That way, you can get familiar with the math, which is really just a bit of basic trigonometry. After that, stepping up to three dimensions isn't very difficult.
The problem is much simpler if the cube (or rectangle) is axis aligned, so you probably should do that first. For an example of determining the rotation you need, see How to calculate rotation angle from rectangle points?.
Once you've determined the rotation angle, you can translate the rectangle to the origin and rotate it by doing the first two steps in the accepted answer here: Drawing a Rotated Rectangle.
You now have an axis-aligned rectangle that's centered at the origin.
Finally, for each of your points:
Apply the same translation and rotation that you applied to the rectangle.
Test to see if the x and y coordinates in the resulting point are within the rectangle. This is a matter of, at most, four bounds checks.
If the point is in the rectangle, save it.
Once you've done this in two dimensions, you should be able to apply those concepts to three dimensions.
The algorithm is O(n), where n is the number of points.

Approximate area of overlap of rotated rectangles

I need to estimate the overlap ratio of two rectangles of arbitrary size and orientation.
I know how to perform the exact computation, using the Sutherland-Hodgman algorithm, which can be optimized for this case.
Anyway as I need to use that function intensively and perfect accuracy isn't required (say 10% error can be tolerated), I was wondering if it cannot be evaluated in a faster way, preferably branchless.
If that helps, one can assume the same aspect ratio for both rectangles, and ratio of the areas not exceeding 4.
Update:
For unrotated rectangles, the formula is
(min(W0,DX+W1) - max(-W0,DX-W1)).(min(H0,DY+H1) - max(H0,DY-H1))
or zero if any of the two factors is negative, where DX, DY are the differences between center coordinates, W and H denote the respective half sizes.
It is probably worthwhile to look at the curve of the common area for given placement of the centers and given sizes, when you vary the relative rotation angle.
Sutherland-Hodgman algorithm is intended for intersection of convex clip polygon with arbitrary one. I would expect that algorithm especially designed for convex-convex case is faster.
I had good practical experience with O'Rourke algorithm (O(m+n)) for alike problem
(area of intersections between two sets of thousands of rotated rectangles).
Code link is here - convconv. AFAIR, some simplifications were possible for rectangles.
Another algorithm
I doubt that any approximation approach could find a result significantly faster and with controlled accuracy.

Cover a polygon with K rectangles minimizing the rectangles area

Given a general polygon P (might contain holes) and a number of rectangles K, I want to find K rectangles {r1,r2,...,rk} such that the polygon is contained in the union of these rectangles. Also, I want to minimize the area of the union of the rectangles.
Note that the rectangles may overlap each other. Also, they're axis-aligned.
As an example, for K=1, the algorithm should report the minimum bounding box.
I've read some papers on the subject, namely "A linear-time approximation algorithm for minimum rectangular covering" and "Covering a polygonal region by rectangles".
The first article approaches the problem by dividing the polygon into simpler polygons, but it considers only rectangles that are within the polygon.
The second article assumes that the rectangles dimension are fixed and simply translates them around and tries to cover the polygon with the minimum number of rectangles.
I know that there is an article named "covering a polygon with few rectangles" that has what I believe to be exactly what I'm looking for, but I would have to pay in order to have access to it and I'd like to dig depeer before spending any money.
Since this is an np-complete or np-hard (not sure) problem, i'm not expecting fast exact algorithms, therefore approximations are also relevant.
Does anyone have knowledge of previous work on this particular problem? Any work that might relate is also welcome. Also, if you have ideas on how to address the problem it would be great!
Thanks in advance.

Algorithm to find the largest empty rectangle amid other polygons

The scenario : There is a rectangular space inside which there are arbitrarily placed polygons of arbitrary orientations. The aim is to find the largest empty rectangle that can be fitted inside the empty regions of the rectangular space. These images below illustrate the scenario with the polygons in blue and the dotted line representing the maximum empty rectangle that can be fitted in each scenario.
The problem : Apparently, finding largest empty rectangles is a well known problem in computational geometry, but the algorithms I found in this area dealt with finding empty rectangles amid points (CGAL has implemented this) and line segments. Is there a way to adapt these existing techniques for my scenario? Or is there a simpler way to do this?
Unfortunately, most of the computational geometry literature with which I am familiar seems to generate beautiful descriptions of algorithms and proofs of their correctness without actually providing implementations. Perhaps this is because the implementations are generally rather involved.
You don't mention what degree of inaccuracy you can tolerate. If you have some tolerance, this answer's for you.
My suggestion is that you turn this hard problem into an easier problem.
Find the bounding box of your polygon collection.
Divide the bounding box into a grid. The finer the grid the better your accuracy, but the longer it will take to find a solution.
Find how much area of each grid cell (cast as a rectangular polygon) intersects with the polygon set.
If the overlap is sufficient (greater than some minimum value you specify), mark the grid cell with a zero; otherwise, mark it with a one.
You now have a rectangular array of zeros and ones. This forms the basis of the easier problem: what is the largest rectangular subset of this grid which is composed entirely of ones?
This easier problem has a number of accessible solutions all over the internet (e.g. 1, 2, 3, 4, 5, 6).

Polygon packing 2D

I have problem of packing 2 arbitrary polygons. I.e. we have 2 arbitrary polygons. We are to find such placement of this polygons (we could make rotations and movements), when rectangle, which circumscribes this polygons has minimal area.
I know, that this is a NP-complete problem. I want to choose an efficient algorithm for solving this problem. I' looking for No-Fit-Polygon approach. But I could't find anywhere the simple and clear algorithm for finding the NFP of two arbitrary polygons.
The parameter space does not seem too big and testing it is not too bad either. If you fix one polygon, the other ploygon can be shifted along x-axis by X, and shifted along y-axis by Y and rotated by r.
The interesting region for X and Y can be determined by finding some bounding box for for the polygons. r of course is between and 360 degrees.
So how about you tried a set of a set of equally spaced intervals in the interesting range for X,Y and r. Perhaps, once you found the interesting points in these dimensions, you can do more finer grained search.
If its NP-complete then you need heuristics, not algorithms. I'd try putting each possible pair of sides together and then sliding one against the other to minimise area, constrained by possible overlap if they are concave of course.
There is an implementation of a robust and comprehensive no-fit polygon generation in a C++ library using an orbiting approach: https://github.com/kallaballa/libnfporb
(I am the author of libnfporb)

Resources