realbasic-nug
[Top] [All Lists]

Re: Need help with an image grid

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Need help with an image grid
From: "Joseph J. Strout" <joe at realsoftware dot com>
Date: Fri, 30 Sep 2005 08:54:26 -0600
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <GMail dot 1128091012 dot 86215318 dot 069330855622 at localhost>
OK, this will be a stream-of-consciousness reply. Hopefully the thought process will prove useful even if the final answer isn't. :)

So, you have a bunch of square images, which you need to fit into an output area that is not square. The key question is how many columns you should have, since when you know that, it's easy to calculate both the output image size (which is just output width / picture width), and the number of rows (which is just Ceil(qtyImages / qtyColumns).

If the output area were square, you'd have the same number of rows and columns. If the output is twice as high as wide, then you want twice as mane rows as columns. Basically, the aspect ratio of the output area should match the ratio of rows to columns. But we need to be careful to round in the right direction. Some examples may help.

Suppose we have 7 images (a nice indivisible number that will force us to deal with rounding). In a square output area, we'd want 3 rows and 3 columns, but the last column will have only 1 image in it. We can find this 3 as the square root of 7, rounded up.

Now what if our output were twice as high as wide? In this case we want 4 rows of 2 columns (with the last row again having only 1 image in it), so that the rows/columns ratio (2) equals the output aspect ratio. To get this result, we could write:

   rows/columns = outputAspect
   rows*columns = qtyImages

and now we have two equations with two unknowns. Solving the first equation for 'rows' gives us

   rows = columns * outputAspect

and then substituting this into the second one gives

  columns*columns*outputAspect = qtyImages

or

  columns = Sqrt(qtyImages/outputAspect)

In this example, that'd be Sqrt(7/2) = 1.87, which again we round up to 2, to find two columns.

So, I think that's your answer... use Ceil(Sqrt(qtyImages/outputAspect)) to get the number of columns, then from that, you can find the number of rows and the output image size.

HTH,
- Joe

P.S. See kids, algebra IS useful in the real world!  :)

--
Joe Strout                          REAL Software, Inc.

Vote for REALbasic (twice!) in the LinuxWorld Reader's Choice Awards:
http://linux.sys-con.com/general/readerschoice.htm
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>