OK this is cool, create an MC in the library 100px  w & h, linkage
"square", create a smaller different colour square inside it just so you
can see what's going on - Then run this:

[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.
 *
 * 97% of the credits goes to Danny Kodicek  */
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++) {
                                ypos = j*squareSize;
                                for (var k = 0; k<cols; k++) {
                                        xpos = k*squareSize;
                                        if (count<Nsquares) {
                                                posArray.push({x:xpos,
y:ypos});
                                                count++;
                                        }
                                }
                        }
                        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);
                }
        }
}
function drawLayout(lobj) {
        var holder = this.createEmptyMovieClip("holder", 1);
        var squareSize = lobj.squareSize;
        var cols = lobj.cols;
        var rows = lobj.rows;
        var number = lobj.number;
        var layout = lobj.layout;
        for (var j = 0; j<number; j++) {
                var sqr = holder.attachMovie("square", "square"+j, j+1);
                sqr._x = layout[j].x;
                sqr._y = layout[j].y;
                sqr._xscale = sqr._yscale=squareSize;
        }
}
n=0
onEnterFrame=function(){
drawLayout(computeLargestSquareSizeAndLayout(400, 400, n++));
}
[/as]


Nice!
_______________________________________________
[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

Reply via email to