> Hi, this problem was raised in 2002 and sort of solved by Jon
> Bradley (code
> below) but not quite the way I need.
>
> What I¹m after is a function that divides a defined area into n
> equal parts
> and places a clip in each part to achieve even distribution throughout the
> area. Jon Bradley¹s solution below does this for only the x, y or
> diagonally
> across both ­ not throughout the whole area...
>
> The math involved is beyond me I¹m afraid. Any ideas? Any help much
> appreciated. Thanks!

This came up a while ago. If you're happy with a solution that includes some
blank space (otherwise your regions can't be rectangular), try this code (it
includes some test code so you can just throw it into a movie with a
movieclip linked as 'square' and run to see the effect). I don't recall who
else it was that refined this code with me, so apologies for not giving due
credit:

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
iterate=function(){
drawLayout(computeLargestSquareSizeAndLayout(640, 480, n++));
}
onEnterFrame=iterate

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