דוד מצא לי את התשובה פה.
http://math.stackexchange.com/a/466248
ועשיתי דוגמא לעניין הזה פה.
http://liveweave.com/zzk2wR
זה הפונקציה.
function calculateSquareSizeInArea(widthArea ,heightArea, countSquare ){
var x=widthArea, y=heightArea, n=countSquare;
var px=Math.ceil(Math.sqrt(n*x/y));
var sx,sy;
if(Math.floor(px*y/x)*px<n) //does not fit, y/(x/px)=px*y/x
sx = y/Math.ceil(px*y/x);
else
sx = x/px;
var py= Math.ceil(Math.sqrt(n*y/x));
if(Math.floor(py*x/y)*py<n) //does not fit
sy=x/Math.ceil(x*py/y);
else
sy=y/py;
return Math.max(sx,sy);
}
פורסם במקור בפורום CODE613 ב15/12/2014 19:12 (+02:00)