algorithm - Rectangle packing in Java -
i'm trying implement rectangle packing algorithm.
i have rectanges on input each rectangle have heigth , width(and index number, able identify them when log them)
i have matrix (an integer[][]), , want store rectangles in matrix. after initialization matrix cannot modified, has fixed size.
let's see , example, have 2 rectangles:
new rectangle(2,3,1) //height=2, width=3, index=1 new rectangle(1,2,2) //height=1, width=1, index=2
and let have 3x3 matrix:
integer[][] matrix = new integer[4][3]; //height=4, width=3
after i'm done running algorithm, see (if print matrix system.out):
1 1 1 1 1 1 2 2 0 0 0 0
the first 2 row stores 1st rectangle (it's 2 high , 3 wide), , find 2nd rectangle in third row. 0s mean there empty spaces left (we store other rectangle there well)
i have trouble finding way greedy (or any) algorithm.
i sort rectangles biggest smallest. should startsomehow storing rectangles: if matrix have enough space, put there, if not, try cell. continue on every row , on every cell until find place.
can point me right direction? description algorith, pseudo-code, or use figure way out.
thanks, mark
you want "draw" rectangles in matrix, , have rectangles sorted size.
you can/should make cycle that:
- gets rectangle sizes (depends, if it's provided constructor)
- checks if there enough space in matrix rectangle; 1
- draws (use or while loops draw).
1 use matrix[0].length
, matrix.length
matrix size , compare rectangles.
i think biggest challenge not lost in variables. need keep count on number of rows need start draw , when stop , update variable.
Comments
Post a Comment