Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  • ceil( sqrt(2) ) == 2 so there is no need for ? :.
  • sqrt() is an expensive function and not needed twice.
  • be careful with operator order better use brackets or unambiguous order

Your should (not tested) get the same with:

int w = cvCeil( sqrt( ( double )( images.size() ) ) );
int h = cvCeil( ( double )( images.size() ) / w );
int size = images.size() <= 2 ? 300 : 2 * 300 / w;

But you will not get the same result as your original code e.g. images.size() == 7 results in w == 3 not w == 4. Having such a limited number of cases (12) the fastest and most flexible method are lookup tables:

static const int wl[] = { 0,1,2,2,2,3,3,3,3,3,4,4,4 };
static conat int hl[] = { 0,1,1,2,2,2,2,3,3,3,3,3,3 };
static const int sl[] = { 0,300,300,200,150 };
int w = wl[n];
int h = hl[n];
int size = sl[w];

But take care of being consistent. Lookup tables do also offer many ways to create faults.