On Apr 26, 2007, at 10:29 AM, Jiri Heitlager | dadata.org wrote:

Can somebody help me out with dynamically building a matrix of clips.
Well it is not really a matrix, see example:

1   2  3  4
12         5
11          6
10  9  8  7

Here's a solution for a square spiral matrix. Never converted it to NxM format ... if you do that, please send me the adjusted code.

I'll let you figure out only using the values you want (full outer ring on matrix).

cheers,

Jon

Jon Bradley
VFX Artist / Animator
Post Central, Inc.
[EMAIL PROTECTED]
www.cherrycrushthemovie.com


/**
*       SpiralMatrix class
*/

class com.jbradley.geom.SpiralMatrix {

    private var size:Number;
    private var mat:Array;
    private var isInnerSpiral:Boolean;

    function SpiralMatrix(s) {
        size = s;
        mat = [];
        calcMatrix();
    }

    /**
    *   calcMatrix loops over the matrix and calculates the final
    *   value based on the base and delta values.
    *
    *   If "inner" is set to true, values increase from the
    *   center. Otherwise, they increase toward the center
    *   starting from the [0][0] position in matrix
    */
    private function calcMatrix() {
        var offset = isInnerSpiral ? 0:size*size;
        for (var i=0; i<size; i++)
        {
            mat[i] = [];
            for (var j=1; j<size+1; j++)
            {
mat[i][j-1] = Math.abs( Math.pow(base(i,j),2) + delta (i,j) - offset);
            }
        }
    }

    /**
    *   Base returns a ring position in the matrix
    */
    private function base(x:Number,y:Number):Number
    {
return size - 2*Math.min(Math.min(x,y),Math.min(size-x,size- y));
    }

    /**
    *   Delta returns an offset used to determine the final value
    *   added to the current position in the matrix
     */
    private function delta(x:Number,y:Number):Number
    {
        var d = Math.min(x,y);
        if (x + y > size ) { d = size - Math.max(x,y); }
        if (y > x) { return -1 * (x + y - 2*d); }
        return x + y - 2*d;
    }

    public function setSize(s:Number) {
        size = s;
        mat = [];
        calcMatrix();
    }

    public function toString():String {
        var str = "";
        for (var i = 0; i < size; i++)
        {
            str += mat[i] + newline;
        }
        return str;
    }

    public function get matrix():Array { return mat; }
public function set inner(b:Boolean) { isInnerSpiral = b; calcMatrix(); }
    public function get inner():Boolean { return isInnerSpiral; }
}

/**
*       SpiralMatrix usage
*/

import com.jbradley.geom.SpiralMatrix;

var spiral = new SpiralMatrix(5);
trace(spiral);

spiral.inner = true;
trace(spiral);

_______________________________________________
Flashcoders@chattyfig.figleaf.com
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