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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:51:55+00:00 2026-05-14T00:51:55+00:00

I have N rectangular items with an aspect ratio Aitem (X:Y). I have a

  • 0

I have N rectangular items with an aspect ratio Aitem (X:Y).
I have a rectangular display area with an aspect ratio Aview

The items should be arranged in a table-like layout (i.e. r rows, c columns).

what is the ideal grid rows x columns, so that individual items are largest?
(rows * colums >= N, of course – i.e. there may be “unused” grid places).

A simple algorithm could iterate over rows = 1..N, calculate the required number of columns, and keep the row/column pair with the largest items.

I wonder if there’s a non-iterative algorithm, though (e.g. for Aitem = Aview = 1, rows / cols can be approximated by sqrt(N)).

  • 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-05-14T00:51:56+00:00Added an answer on May 14, 2026 at 12:51 am

    Note: I couldn’t quite understand Frédéric’s answer, so I worked the problem out myself and came up with what appears to be the same solution. I figured I might as well explain what I did in case it is helpful.

    First I normalized the aspect ratio of the view to that of the items. (I’m assuming you don’t want to rotate the items.)

    a = (view_width/view_height) / (item_width/item_height)
    

    Now packing a rectangle of width/height ratio a with squares is equivalent to packing the view with items. The ideal case would be for our grid (of squares now) to fill the rectangle completely, which would give us

    a = c/r
    

    where r and c are the numbers of rows and columns:

    N = r*c
    

    Multiplying/dividing these two equations gives us

    N*a = c^2              N/a = r^2
    c = sqrt(N*a)          r = sqrt(N/a)
    

    If the grid is perfect, r and c will be integers, but if not, you have to try the three options Frédéric mentioned and keep the one where r*c is smallest but still more than N:

    • floor(r), ceil(c)
    • ceil(r), floor(c)
    • ceil(r), ceil(c)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page which has a rectangular area with text and icons in
I have a script that appends some rows to a table. One of the
I have N squares. I have a Rectangular box. I want all the squares
I am using an unordered list where the actual list items have a rounded
I have a menu the have rectangular boxes 90x50. Some have single line text,
I have a fluid grid (in height and width). The LIs are always rectangular
In the below image I am trying to achieve the following.I have a table
I have a snippet to create a 'Like' button for our news site: <iframe
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could

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.