Finding Optimal Cell Size for Uniform Rectangle Packing

ktoz
Messages
170
Reaction score
12
Hi

I'm writing a newspaper page management application and am having trouble trying to directly calculate a "best fit" column/row breakdown for displaying pages into arbitrary rectangles.

The arbitrary rectangle in question is the content area of a window and can be resized at will by the user. The pages are a uniform fixed size and the problem is to calculate the best scale and column/row breakdown to display all the pages in the given rectangle with the least wasted space.

Here are the knowns:
vw = width of enclosing view
vh = height of enclosing view
pw = width of the page
ph = height of the page
pct = number of pages to be displayed in the view

Unknowns:
c = columns
r = rows
s = scaling factor
cw = cell width
ch = cell height

After a bit of trial and error, I found that the optimal solution exists when

1 - (c * pw * vh) / (r * ph * vw) = 0;

Where r equals

r = floor(pct / c);
r += (c * r - pct < 0) ? 1 : 0 ;I wrote a C function that is fairly efficient, but was wondering if there is a more direct way to find the zero.

Here's the C function
Code:
for (c = 1; c < pct + 1; c++)
{
	// calculate the number of rows 
	r	= floor(pct / c);
	r	+= (c * r - pct < 0) ? 1 : 0 ;
	
	// test for zero cross
	if (1 - (c * pw * vh) / (vr * ph * vw) < 0)
	{
		// found zero, decrement c and calculate scale
		c--;
		
		// calculate the scale
		s	= vw / (c * pw);
		
		// set the cell dimensions
		cw	= s * pw;
		ch	= s * ph;
		
		// since we've found the zero, exit the loop
		break;
	}
}

Thanks for any help
 
Last edited:
Mathematics news on Phys.org
Came up with a much more accurate function. The last one didn't work as well as I thought at first

Code:
bool CalculateCellSize(float vw, float vh, float pw, float ph, int pct, float *outWidth, float *outHeight)
{
	if (vw > 1)
	{
		bool	exeOK	= false;

		int	lim	= sqrt(vw - 1),
			c,
			r;
				
		float	va	= vw * vh,
			pr	= ph / pw;
	
		for (c = 1; c < lim; c++)
		{
			r	= pct / c + ((pct % c) > 0);
			cw	= vw / c;
			ch	= cw * pr;
			
			if (r * ch ≤ vh)
			{
				*outWidth	= cw;
				*outHeight	= ch;
				exeOK		= true;
				break;
			}
		}
		
		return exeOK;
	}
	
	return false;
}
 
Last edited:
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Fermat's Last Theorem has long been one of the most famous mathematical problems, and is now one of the most famous theorems. It simply states that the equation $$ a^n+b^n=c^n $$ has no solutions with positive integers if ##n>2.## It was named after Pierre de Fermat (1607-1665). The problem itself stems from the book Arithmetica by Diophantus of Alexandria. It gained popularity because Fermat noted in his copy "Cubum autem in duos cubos, aut quadratoquadratum in duos quadratoquadratos, et...
Thread 'Imaginary Pythagorus'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...
Back
Top