Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>Input: <code>width</code>(float or int), <code>height</code>(float or int)</p> <p>Algorithm:</p> <pre><code>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 } </code></pre> <p>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 4x3 squares of length <code>width/4</code>=<code>height/3</code>, or 8x6 squares of half that size, or 12x9 squares of a third that size...) If it is not a rational number, your task is impossible.</p> <p>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 <code>GCD(numer,denom)</code> , and dividing both numerator and denominator by that. </p> <p>Here is an example implementation in python3:</p> <pre><code>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 </code></pre> <p>e.g.</p> <pre><code>&gt;&gt;&gt; largestSquareTiling(2.25, 11.75) ((9, 47), 0.25) </code></pre> <p>You can tune the optional parameter <code>maxVerticalSquares</code> 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 <code>maxVerticalSquares=500</code> might be reasonable or something (possibly not even including the exception code).</p> <p>Once you have this, and a range of desired square lengths <code>(minLength, maxLength)</code>, you just multiply:</p> <pre><code># 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&lt;shrinkFactorMin: raise Exception('desired tile size range too restrictive; no tiling found') </code></pre> <p>If <code>shrinkFactor</code> is now <code>2</code> for example, the new output value in the example would be <code>((9*2,47*2), 0.25/2)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload