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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:51:51+00:00 2026-05-17T14:51:51+00:00

I have N scalable square tiles (buttons) that need to be placed inside of

  • 0

I have N scalable square tiles (buttons) that need to be placed inside of fixed sized rectangular surface (toolbox). I would like to present the buttons all at the same size.

How could I solve for the optimal size of the tiles that would provide the largest area of the rectangular surface being covered by tiles.

  • 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-17T14:51:52+00:00Added an answer on May 17, 2026 at 2:51 pm

    Let W and H be the width and height of the rectangle.

    Let s be the length of the side of a square.

    Then the number of squares n(s) that you can fit into the rectangle is floor(W/s)*floor(H/s). You want to find the maximum value of s for which n(s) >= N

    If you plot the number of squares against s you will get a piecewise constant function. The discontinuities are at the values W/i and H/j, where i and j run through the positive integers.

    You want to find the smallest i for which n(W/i) >= N, and similarly the smallest j for which n(H/j) >= N. Call these smallest values i_min and j_min. Then the largest of W/i_min and H/j_min is the s that you want.

    I.e. s_max = max(W/i_min,H/j_min)

    To find i_min and j_min, just do a brute force search: for each, start from 1, test, and increment.

    In the event that N is very large, it may be distasteful to search the i‘s and j‘s starting from 1 (although it is hard to imagine that there will be any noticeable difference in performance). In this case, we can estimate the starting values as follows. First, a ballpark estimate of the area of a tile is W*H/N, corresponding to a side of sqrt(W*H/N). If W/i <= sqrt(W*H/N), then i >= ceil(W*sqrt(N/(W*H))), similarly j >= ceil(H*sqrt(N/(W*H)))

    So, rather than start the loops at i=1 and j=1, we can start them at i = ceil(sqrt(N*W/H)) and j = ceil(sqrt(N*H/W))). And OP suggests that round works better than ceil — at worst an extra iteration.

    Here’s the algorithm spelled out in C++:

    #include <math.h>
    #include <algorithm>
    // find optimal (largest) tile size for which
    // at least N tiles fit in WxH rectangle
    double optimal_size (double W, double H, int N)
    {
        int i_min, j_min ; // minimum values for which you get at least N tiles 
        for (int i=round(sqrt(N*W/H)) ; ; i++) {
            if (i*floor(H*i/W) >= N) {
                i_min = i ;
                break ;
            }
        }
        for (int j=round(sqrt(N*H/W)) ; ; j++) {
            if (floor(W*j/H)*j >= N) {
                j_min = j ;
                break ;
            }
        }
        return std::max (W/i_min, H/j_min) ;
    }
    

    The above is written for clarity. The code can be tightened up considerably as follows:

    double optimal_size (double W, double H, int N)
    {
        int i,j ;
        for (i = round(sqrt(N*W/H)) ; i*floor(H*i/W) < N ; i++){}
        for (j = round(sqrt(N*H/W)) ; floor(W*j/H)*j < N ; j++){}
        return std::max (W/i, H/j) ;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be good/scalable user session alternative in following scenario: users don't have to
I want to have a movable/scalable/rotable view inside another view. The inner view can
I have a data grid where I need everything to have a fully scalable
I am making a site in django [mysql] that will have to be scalable,
I have one small online sale business but I want to make it scalable
This a VERY open question. Basically, I have a computing application that launches test
I have a simple site to develop and would like to learn PHP as
I have a probability problem, which I need to simulate in a reasonable amount
I have a pie chart which is made using UIBezierPath's. I now need those
I have a project that I have recently started working on seriously but had

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.