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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:42:43+00:00 2026-05-22T16:42:43+00:00

I have a fixed size 2D space that I would like to fill with

  • 0

I have a fixed size 2D space that I would like to fill with an arbitrary number of equal sized squares. I’d like an algorithm that determines the exact size (length of one side) that these squares should be in order to fit perfectly into the given space.

  • 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-22T16:42:43+00:00Added an answer on May 22, 2026 at 4:42 pm

    Notice that there must be an integer number of squares which fill the width and height. Therefore, the aspect ratio must be a rational number.

    Input: width(float or int), height(float or int)

    Algorithm:

    aspectRatio = RationalNumber(width/height).lowestTerms  #must be rational number
    
    # it must be the case that our return values
    # numHorizontalSqaures/numVerticalSquares = aspectRatio
    
    return {
        numHorizontalSquares = aspectRatio.numerator,
        numVerticalSquares = aspectRatio.denominator,
        squareLength = width/aspectRatio.numerator
    }
    

    If the width/height is a rational number, your answer is merely any multiple of the aspect ratio! (e.g. if your aspect ratio was 4/3, you could fill it with 4×3 squares of length width/4=height/3, or 8×6 squares of half that size, or 12×9 squares of a third that size…) If it is not a rational number, your task is impossible.

    You convert a fraction to lowest terms by factoring the numerator and denominator, and removing all duplicate factor pairs; this is equivalent to just using the greatest common divisor algorithm GCD(numer,denom) , and dividing both numerator and denominator by that.

    Here is an example implementation in python3:

    from fractions import Fraction
    def largestSquareTiling(width, height, maxVerticalSquares=10**6):
        """
            Returns the minimum number (corresponding to largest size) of square
            which will perfectly tile a widthxheight rectangle.
    
            Return format:
                (numHorizontalTiles, numVerticalTiles), tileLength
        """
        if isinstance(width,int) and isinstance(height,int):
            aspectRatio = Fraction(width, height)
        else:
            aspectRatio = Fraction.from_float(width/height)
    
        aspectRatio2 = aspectRatio.limit_denominator(max_denominator=maxVerticalSquares)
        if aspectRatio != aspectRatio2:
            raise Exception('too many squares') #optional
        aspectRatio = aspectRatio2
    
        squareLength = width/aspectRatio.numerator
        return (aspectRatio.numerator, aspectRatio.denominator), squareLength
    

    e.g.

    >>> largestSquareTiling(2.25, 11.75)
    ((9, 47), 0.25)
    

    You can tune the optional parameter maxVerticalSquares to give yourself more robustness versus floating-point imprecision (but the downside is the operation may fail), or to avoid a larger number of vertical squares (e.g. if this is architecture code and you are tiling a floor); depending on the range of numbers you are working with, a default value of maxVerticalSquares=500 might be reasonable or something (possibly not even including the exception code).

    Once you have this, and a range of desired square lengths (minLength, maxLength), you just multiply:

    # inputs    
    desiredTileSizeRange = (0.9, 0.13)
    (minHTiles, minVTiles), maxTileSize = largestSquareTiling(2.25, 11.75)
    
    # calculate integral shrinkFactor
    shrinkFactorMin = maxTileSize/desiredTileSizeRange[0]
    shrinkFactorMax = maxTileSize/desiredTileSizeRange[1]
    shrinkFactor = int(scaleFactorMax)
    if shrinkFactor<shrinkFactorMin:
        raise Exception('desired tile size range too restrictive; no tiling found')
    

    If shrinkFactor is now 2 for example, the new output value in the example would be ((9*2,47*2), 0.25/2).

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

Sidebar

Related Questions

I'd like to place an arbitrary number of rectangles into a fixed size parent
I have a CDHTMLDialog running in IE that has a fixed size that I
I have a LinearLayout that works great to produce something like: [ fixed image
GIVEN that you have a fixed area of memory already allocated that you would
Possible Duplicate: Auto-size dynamic text to fill fixed size container. Let's say i have
I have two columns. One with fixed size (220px) and the other that fills
I have a need for a fixed-size (selectable at run-time when creating it, not
I have a div with a fixed size of 100px. scrollWidth and scrollHeight works
If I have an array of a fixed size depending on how it is
Suppose you have a large file made up of a bunch of fixed size

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.