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 8233121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:01:40+00:00 2026-06-07T18:01:40+00:00

I’m having the requirement, to insert a specific amount of rectangles (which have a

  • 0

I’m having the requirement, to insert a specific amount of rectangles (which have a defined width but a random height) into another rectangle (which has a defined height and the same defined width as the rectangles to insert). The goal here is, that those inserted rectangles should fill-out the target rectangle as much as possible.

For instance:

enter image description here

I don’t need to get as much rectangles as possible into the black, the goal is to fillout the black rect as much as possible, best case, entirely.

In reality, there are many “black” rectangles and thousands of “reds”, I’m looking for an effective algorithm to calculate. I have to implement this in ECMA-/Javascript so it’s not really the fastest of all platforms.

I looked into some algos like Richard E. Korf’s “Optimal Rectangle Packing” or “Bin packings problems”, but I couldn’t really translate those for this specific problem.

Can somebody recommend me a method/algorithm ?

  • 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-07T18:01:42+00:00Added an answer on June 7, 2026 at 6:01 pm

    Because your red and black triangles both have a defined width, you can reduce the problem to a number line, so to speak. Basically, if you ever flipped a red one on its side, you’d most likely end up with wasted space – much more wasted space than putting it in the ‘normal fitting’ way.

    So with this in mind, you can reduce the problem exactly to the traditional knapsack problem where the capacity is the height of the black rectangle and the ‘weight’ of the red triangles are their height. The width can be entirely abstracted out of the problem.

    Another advantage (as pointed out by xvatar) is that the value density of the candidates are all equal. That is to say that you don’t have the “brick-ring” issue the traditional knapsack problem has. Given the choice between bricks and rings to fill your knapsack with, the rings are obvious candidates. In this case, they’re all the same so there are no obvious candidates.

    It would seem the big blocks are easy candidates, but this greedy approach doesn’t fly. Consider the scenario where there are 5 units of space left, and we have bricks of 4, 3 and 2. If we go with the greedy solution, we add the 4, leaving 1 open space. If we would instead have gone with 3 and 2, we would not have the 1 open space remaining.

    It also stands to note that once you have identified what rectangles go in, it doesn’t matter what order they go in.

    Further reading: http://en.wikipedia.org/wiki/Knapsack_problem

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

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.