One last addition:
[as]
/**
* computes the largest N square size (and layout) that can fit an area
(width,height).
*
* @return an Object containing 'squareSize' and the number of 'cols'
and 'rows'
* and a layout array for drawing the squares on screen.
*
* 96% of the credits goes to Danny Kodicek, 3% to Bernard Poulin ;)
*/
function computeLargestSquareSizeAndLayout(width:Number, height:Number,
Nsquares:Number):Object {
if (width<height) {
var cols:Number = 1;
var rows:Number = Math.floor(height/width);
} else {
var rows:Number = 1;
var cols:Number = Math.floor(width/height);
}
//var cols:Number = 1;
//var rows:Number = 1;
var swidth:Number = width;
var sheight:Number = height;
var next_swidth:Number = width/(cols+1);
var next_sheight:Number = height/(rows+1);
while (true) {
if (cols*rows>=Nsquares) {
var squareSize:Number = Math.min(swidth,
sheight);
var posArray = [];
var count = 0;
for (var j = 0; j<rows; j++) {
count++;
ypos = j*squareSize;
for (var k = 0; k<cols; k++) {
xpos = k*squareSize;
if (count<Nsquares) {
posArray.push({x:xpos,
y:ypos});
}
}
}
return {squareSize:squareSize, cols:cols,
rows:rows, number:Nsquares, layout:posArray};
}
if (next_swidth>next_sheight) {
cols++;
swidth = next_swidth;
next_swidth = width/(cols+1);
} else {
rows++;
sheight = next_sheight;
next_sheight = height/(rows+1);
}
}
}
drawLayout = computeLargestSquareSizeAndLayout(500, 460, 10);
[/as]
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com