Compact yarns

I am looking for pointers to solving the following problem: I have a set of rectangles whose height is known and x-positions as well, and I want to pack them in a more compact form. With a small drawing (where all the rectangles have the same width, but the width can change in real life), I would like, not.

-r1-
  -r2--
     -r3--
       -r4-
        -r5--

sort of.

-r1-  -r3-- 
  -r2-- -r4-
         -r5--

All tips will be appreciated. I'm not necessarily looking for the “best” solution.

+5
source share
6 answers

- , , "binpacking". , .

+2

Topcoder 3D- . , .

+2

- ?

  • x-position
  • , , x

    Collection<Rectangle> overlaps (int startx, int endx, Collection<Rectangle> rects){
    ...
    }
    
  • Collection<Rectangle> toDraw;
    Collection<Rectangle> drawn;
    foreach (Rectangle r in toDraw){
    Collection<Rectangle> overlapping = overlaps (r.x, r.x+r.width, drawn);
    int y = 0;
    foreach(Rectangle overlapRect in overlapping){
    y += overlapRect.height;
    }
    drawRectangle(y, Rectangle);
    drawn.add(r);
    }
    
+1

- . , , . ( = ) . .

+1

? , , , (X, Y) " X" , Y ", X x Y.

"" , , . , , , .

, , , , - . -?

+1

. , , , , - , , . , ( ) - .

, y- , x- , .

Iterate over your array of rectangles. For each rectangle, initialize the y-coordinate of the rectangle by 0. Then loop, increasing this yy coordinate of the rectangle until it intersects with any of the previously placed rectangles (you need to track which rectangles were previously placed). Lock the y coordinate you just found and continue to process the next rectangle.

0
source

All Articles