Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _itemsChanged(items) {
- /*
- items[n]
- {
- x: xi,
- y: yi,
- w: wi,
- h: hi
- }
- */
- let maxW = 1,
- maxH = 1;
- for ( let i = 0, e; e = items[i]; ++i ) {
- if (e.h > maxH) maxH = e.h;
- if (e.w > maxW) maxW = e.w;
- }
- if (maxW > this.columns) this.columns = maxW;
- let szmp = [];
- for ( let i = 1; i <= maxH; ++i ) {
- szmp.push([]);
- for ( let j = 0; j <= maxW; ++j ) {
- szmp[i-1].push([1,1]);
- }
- }
- /* Moongom Algorithm */
- for ( let i = 0,e; e = items[i]; ++i ) {
- items[i].index = i;
- let [px1,py1] = szmp[e.h-1][e.w-1];
- let px2 = px1 + e.w - 1;
- let py2 = py1 + e.h - 1;
- seeking:
- while (true) {
- while ( px2 <= this.columns ) {
- let hasCollision = false;
- for ( let j = i-1; j >= 0 && !hasCollision; --j) {
- let x1 = items[j].x,
- x2 = items[j].x + items[j].w - 1,
- y1 = items[j].y,
- y2 = items[j].y + items[j].h - 1;
- hasCollision = (px1 > x2 || px2 < x1 || py1 > y2 || py2 < y1) ? false : true;
- }
- if (!hasCollision) {
- szmp[e.h-1][e.w-1] = [px2+1,py1];
- e.x = px1;
- e.y = py1;
- e.szmpX = px2+1;
- e.szmpY = py1;
- break seeking;
- }
- ++px1;
- ++px2;
- }
- px1 = 1;
- ++py1;
- px2 = px1 + e.w -1;
- ++py2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement