Re: Selecting and merging rectangles



eriwik@xxxxxxxxxxxxxxxxxxx wrote:
I'm having problems with coming up with a good algorithm for the
following:

Say you have a grid of squares (in this example 4x4 will do), then you
select the 4 squares in the center and merge these into one large
square. Then you select some new squares (which might or might not
include the new large square) how do you determine that the new
selection forms a rectangle?

Currently I'm representing each square/rectangle with the
X/Y-coordinates of the top-left corner and the width and height of the
square (so the top/left-most square in a 4x4-grid would be <0, 0, 1, 1>
and the merged center-squares would be <1, 1, 2, 2>), is there a better
way to represent this?

If you can make the assumption that the rectangles never overlap,
then it's fairly easy:

Just compute the sum of the areas of the rectangles. Then take the
minimum and maximum X coordinates and the minimum and maximum Y
coordinates and compute (maxX-minX)*(maxY-minY). In effect, you
are forming a new rectangle just exactly wide and tall enough to cover
every point in the set of rectangles when laid into the right position.

The individual rectangles fit together into a larger rectangle if and
only if the area of the larger rectangle equals the sum of the areas
of the smaller rectangles. (If the smaller rectangles' areas' sum is
less than the large rectangle, this means they don't cover the whole
area. If it's larger, then it would mean they overlap, which they
don't, by definition.)

- Logan
.



Relevant Pages

  • Re: help with a contest problem..
    ... Bhaskara Aditya wrote: ... I've tried to solve the foll problem.. ... and set number of white squares visible ... the squares covered by that rectangle. ...
    (comp.programming)
  • Re: Divide a rectangle (with cartesian coordinates) into equal
    ... squares and I need back each coordinates of this squares! ... whose are is half that of a given rectangle is pretty straightforward. ... If the question is to divide a given rectangle in half so it becomes to ... be no larger than delta. ...
    (comp.soft-sys.matlab)
  • Re: Shape Alignment Question
    ... When yo move a rectangle near anothe one it is magnetize to one of it edge ... Visio and are of various sizes. ... rectangle (area equals the total area of the squares) and then draw ... I overlay the squares on the rectangle one at a time. ...
    (microsoft.public.visio)
  • Re: Selecting and merging rectangles
    ... Say you have a grid of squares, ... selection forms a rectangle? ... rectilinear rectangle. ... Find the left most, top most, right most, and bottom most pixel ...
    (comp.programming)
  • least square nonlinear
    ... works for situations such as mine where you have function of squares ... Sum total of PE per node in the grid. ... xcell_var= xcnt; ...
    (comp.soft-sys.matlab)