Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your mention of rows and columns suggests that you envisaged arranging the rectangles in a grid, possibly with a few spaces (e.g. some of the bottom row) unfilled. Assuming this is the case:</p> <p>Suppose you scale the objects such that (an as-yet unknown number) <code>n</code> of them fit across the screen. Then</p> <pre><code>objectScale=screenWidth/(n*objectWidth) </code></pre> <p>Now suppose there are N objects, so there will be </p> <pre><code>nRows = ceil(N/n) </code></pre> <p>rows of objects (where ceil is the <a href="http://en.wikipedia.org/wiki/Floor_and_ceiling_functions" rel="nofollow noreferrer">Ceiling function</a>), which will take up </p> <pre><code>nRows*objectScale*objectHeight </code></pre> <p>of vertical height. We need to find <code>n</code>, and want to choose the smallest <code>n</code> such that this distance is smaller than <code>screenHeight</code>.</p> <p>A simple mathematical expression for <code>n</code> is made trickier by the presence of the ceiling function. If the number of columns is going to be fairly small, probably the easiest way to find <code>n</code> is just to loop through increasing <code>n</code> until the inequality is satisfied. </p> <p><strong>Edit:</strong> We can start the loop with the upper bound of</p> <pre><code>floor(sqrt(N*objectHeight*screenWidth/(screenHeight*objectWidth))) </code></pre> <p>for n, and work down: the solution is then found in O(sqrt(N)). An O(1) solution is to assume that</p> <pre><code>nRows = N/n + 1 </code></pre> <p>or to take</p> <pre><code>n=ceil(sqrt(N*objectHeight*screenWidth/(screenHeight*objectWidth))) </code></pre> <p>(the solution of Matthieu M.) but these have the disadvantage that the value of <code>n</code> may not be optimal.</p> <p>Border cases occur when <code>N=0</code>, and when <code>N=1</code> and the aspect ratio of the objects is such that <code>objectHeight/objectWidth &gt; screenHeight/screenWidth</code> - both of these are easy to deal with.</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. 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