Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:24:03+00:00 2026-06-09T12:24:03+00:00

Using this 2d bin-packing algorithm (EDIT: Fixed demo) which is a variation of this

  • 0

Using this 2d bin-packing algorithm (EDIT: Fixed demo) which is a variation of this how can I get the final bin width and height of each bin?

My demo code follows:

 var blocks = [
    {w: 1000, h: 800},
    {w: 500, h: 700},
    {w: 500, h: 700},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 500, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350},
    {w: 250, h: 350}
];

var sheets = [];

while(blocks.length) {
    var packer = new GrowingPacker(1000,800);
    packer.fit(blocks);

    sheet = [];
    for (var i=blocks.length-1; i>=0; i--) {
        if (blocks[i].fit !== undefined && blocks[i].fit !== null) {
            //console.log(blocks[i].fit);
            sheet.unshift(blocks[i]);
            blocks.splice(i,1);
        }
    }
    //console.log(sheet[sheet.length-1].fit.y + sheet[sheet.length-1].h);
    //console.log(sheet);
    sheets.push(sheet);
}


for(var i=0; i<sheets.length; i++) {
    var sheet = sheets[i];
    var sheetWidth = sheet[sheet.length-1].w + sheet[sheet.length-1].fit.x;
    var sheetHeight = sheet[sheet.length-1].h + sheet[sheet.length-1].fit.y;

    for(var j=0; j<sheet.length; j++) {
        console.log("SHEET #" + i + " - W: " + sheetWidth + " H: " + sheetHeight + " BLOCK #" + j + " - W: " + sheet[j].w + " H: " + sheet[j].h + " X: " + sheet[j].fit.x + " Y: " + sheet[j].fit.y);
    }
}

The original algorithm only deals with a single, ever-expanding bin so I modified it to take a max width and height. Then I run through the array of blocks, call the packer, push fit blocks to a new array and unset them from ‘blocks’ until ‘blocks’ is empty. Whether that was the best approach is the subject of another question.

Anyway, I’ve tried modifying growNode like so:

growNode: function(w, h) {
    var canGrowRight  = (w <= this.root.w && this.root.w + w <= maxW);
    var canGrowDown = (h <= this.root.h && this.root.h + h <= maxH);

    if (canGrowRight) {
        this.sheetW = this.root.w + w; //<--------------added
        return this.growRight(w, h);
    }
    else if (canGrowDown) {
        this.sheetH = this.root.h + h; //<--------------added
        return this.growDown(w, h);
    }
    else

        return null; // need to ensure sensible root starting size to avoid this happening
},

which works for every sheet but the first one. I tried to add these lines in a few other methods as well with no success. I also tried to get the sheet size from the last block in the sheet’s width + x but that only works if the sheet is full.

My question again is how can I get the final sheet size for each sheet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:24:05+00:00Added an answer on June 9, 2026 at 12:24 pm

    I eventually figured it out. I added two lines to findNode()

    findNode: function(root, w, h) {
        if (root.used) {
            return this.findNode(root.right, w, h) || this.findNode(root.down, w, h);
        }
        else if ((w <= root.w && w <= this.maxW) && (h <= root.h && w <= this.maxW)) {
            this.binWidth = this.root.w <= this.maxW ? this.root.w : this.maxW;
            this.binHeight = this.root.h <= this.maxH ? this.root.h : this.maxH;
            return root;
        }
        else {
            return null;
        }
    },
    

    Here is the jsfiddle if you want to play with it. I recommend trying something like this for the input:

    100x80
    50x70
    50x35x2
    25x35x4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to delete a file into recycle bin. I using this code. SHFILEOPSTRUCT
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
I'm using this minifier: #!/usr/bin/perl use Getopt::Std; use strict; my %opts; getopts('f:t:h', \%opts) ||
I'm using this plist gem to generate a plist file, which my Rails application
how to get MD5 fingerprint key . I am using this command : C:\Program
I'm using this script for building application from command line: #!/bin/bash TARGET=signtest CONFIGURATION=Debug SDK=iphoneos
I'm using this to convert a PDF to PNG: exec('/usr/bin/convert -density 96 -quality 85
Tried using this function on a paragraph consisting of 3 strings and abbreviations. #!/usr/bin/env
Solved using this post: Can you convert an ASP.NET MVC Application to a Web
I am using this in a Bash script to get the path where I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.