if have multiples object, how to arrange it so that each number of object in row x column will form a near square?
exp:14 objects arrange to something like this:
0 0 0 0
0 0 0 0
0 0 0 0
0 0
if have multiples object, how to arrange it so that each number of object in row x column will form a near square?
exp:14 objects arrange to something like this:
0 0 0 0
0 0 0 0
0 0 0 0
0 0
Take the ceil of the square root?
in python:
import math
ceil(14**(.5))
Which returns:
>>> from math import ceil
>>> ceil(14**(.5))
4.0
Get the square root of the number of items:
n = 14 ^ 0.5 ~ 3.7417
Round up to the nearest integer:
n = ceil(14 ^ 0.5) = 4
Now simply arrange the items in rows of n items until you run out.
Given n
objects, the size of the resulting "square" is ceil(sqrt(n))
, with special shortcut cases for n = 0 (don't draw anything) or n = 1 (just a 1x1 square).
Also, the question title is a little misleading. You can never arrange N objects in an NxN square, unless N is 1.
That depends. Assuming that the number of columns should be equal to or greater than the number of rows then the following calculates the number of columns (in pseudo-code):
Ceiling(Sqrt(n))
where n
is the number of items.