Finding Optimal Cell Size for Uniform Rectangle Packing

  • Context: Graduate 
  • Thread starter Thread starter ktoz
  • Start date Start date
  • Tags Tags
    Rectangle Uniform
Click For Summary
SUMMARY

The discussion focuses on optimizing the layout of uniform-sized pages within a resizable rectangular content area for a newspaper page management application. Key variables include the enclosing view dimensions (vw, vh), page dimensions (pw, ph), and the number of pages (pct). The optimal configuration is determined by calculating the number of columns (c) and rows (r) to minimize wasted space, using a specific formula to find the zero cross. A refined C function, CalculateCellSize, is provided to efficiently compute the optimal cell dimensions based on the given parameters.

PREREQUISITES
  • Understanding of C programming and functions
  • Knowledge of mathematical optimization techniques
  • Familiarity with basic geometry and area calculations
  • Experience with algorithmic problem-solving
NEXT STEPS
  • Explore advanced optimization algorithms for layout problems
  • Learn about dynamic programming techniques in C
  • Investigate graphical user interface (GUI) frameworks for dynamic resizing
  • Research performance profiling tools for C applications
USEFUL FOR

Software developers, particularly those working on UI/UX design, algorithm developers, and anyone involved in optimizing layout management for applications requiring dynamic content display.

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:

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 42 ·
2
Replies
42
Views
4K
  • · Replies 2 ·
Replies
2
Views
596
  • · Replies 98 ·
4
Replies
98
Views
16K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 125 ·
5
Replies
125
Views
20K
  • · Replies 61 ·
3
Replies
61
Views
13K