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

  • Home
  • SEARCH
  • 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 85711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:07:56+00:00 2026-05-10T22:07:56+00:00

My Situation I have a N rectangles The rectangles all have the same shape

  • 0

My Situation

  • I have a N rectangles
  • The rectangles all have the same shape (for example 2 inches wide x 1 inch tall) – Let’s refer to this size as Sw and Sh for the width and height
  • I want to position these rectangles in a grid such that the rects completely on top and next to each other – like what you would see in a spreadsheet
  • What I need is this: Given N, Sw, and Sh what are the number of rows (R) and columns (C) that would stack these rects into the most square-like arrangement possible
  • It is understood that R & C may provide more cells than in needed (for example if N=15,Sw=1,Sh=1 then R=4,C=4 yielding 16 ‘slots’ for 15 rectangles – that is OK.
  • If Sw=Sh then my humble math skills are enough – when they rectangles have differing widths and heights – well frankly that’s beyond me.

Some Notes

  • Yes I have read this question: Stacking rectangles to take as little space as possible and no it did not help. Also it isnt the same question. That question is about rectangles that could be of different sizes, in this question the rectangles have the same size
  • Yes I have searched on wolfram.com, etc and no luck there
  • I don’t have a strong math background so I the way I phrasing this problem may itself be preventing me from finding the answer – I’ve tried related searches relating to tiling, dissecting, decomposing, and not had any success there either

Some examples

the * indicates the edges of the rects the | indicates that a cell is 'filled-in' Notice that not all R*C cells are filled in, but only and exactly N cells  IF N=1, Sw=2, Sh=1 THEN R=1, C=1  ******** *||||||* ********  IF N=2, Sw=2, Sh=1 THEN R=2, C=1  ******** *||||||* ******** *||||||* ********  IF N=3, Sw=2, Sh=1 THEN R=2, C=2   *************** *||||||*      * *************** *||||||*||||||* ***************  IF N=4, Sw=2, Sh=1 THEN R=2, C=2   *************** *||||||*||||||* *************** *||||||*||||||* ***************  IF N=5, Sw=2, Sh=1 THEN R=3, C=2   *************** *||||||*      * *************** *||||||*||||||* *************** *||||||*||||||* *************** 

Implementation of AaronofTomorrow’s answer

# Implementation of AaronofTomorrow's answer # implemented in python 2.6 # reasonable output # works in constant time  import math  def f( N, Sw, Sh ) :     cols = math.sqrt( float(N) * float(Sh) / float(Sw) )     cols = round(cols)     rows = float(N) / float(cols)     rows = math.ceil(rows)     return (int(cols),int(rows)) 

Another implementation inspired by Will’s answer (Updated on 2008-12-08) – this is the one I finally used

# Another implementation inspired by Will's answer # implemented in python 2.6 # reasonable output - a bit better in yielding more squarelike grids # works in time proportional to number of rects # # strategy used it to try incrementaly adding a rect. # if the resulting rect requires more space then two # possibilities are checked - adding a new row or adding a new col # the one with the best aspect ratio (1:1) will be chosen    def g( N, Sw, Sh ) :     slope = float(Sh)/float(Sw)     cols = 1     rows = 1     for i in xrange( N ) :         num_to_fit =i+1         allocated_cells= cols* rows         if ( num_to_fit <= allocated_cells ) :             pass # do nothing         else :             hc,wc = float(Sh * rows), float(Sw * (cols+1))             hr,wr = float(Sh * (rows+1)), float(Sw * cols)             thetac = math.atan( hc/wc)             thetar = math.atan( hr/wr)             alpha = math.pi/4.0             difr = abs(alpha-thetar)             difc = abs(alpha-thetac)             if ( difr < difc ) :                 rows = rows +1             else:                 cols = cols + 1      return (cols,rows) 
  • 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. 2026-05-10T22:07:56+00:00Added an answer on May 10, 2026 at 10:07 pm

    Building on Will Dean’s response, find the derivative of his formula (with respect to nCols):

    -N*Sh / nCols + Sw

    Then set it to 0 and solve for nCols, which gives:

    nCols = sqrt(N * Sh / Sw)

    Round that and you should have the optimum number of columns:

    cols = round(sqrt(N * Sh / Sw))
    rows = ceil(N / cols)

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

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I have been told that a good toolkit for doing… May 11, 2026 at 11:49 am
  • added an answer This should work $(window).bind('resize', function() { alert('Resizing now'); }); May 11, 2026 at 11:49 am
  • added an answer At this point you should break up your 250 projects… May 11, 2026 at 11:49 am

Related Questions

My Situation I have a N rectangles The rectangles all have the same shape
Here is my situation: I have an application that use a configuration file. The
I have a bit of a hybrid situation on my hands. I'm writing an
I have a very tricky situation (for my standards) in hand. I have a
I have a fairly standard inheritance situation in my current LINQ-to-SQL project. I have
My coworker and I have encountered a nasty situation where we have to use
My situation: I have several components, which sometimes have changes to them, and are
I know questions of this kind have been asked before, but my situation differs
In my project there are situations where we have to send xml messages (as
My Situation: I love e editor , however I'm on a new computer and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.