>> have the items align into a circle at the nearest point, *keeping the
   same angle*

I'm not sure what that means exactly, but check out Point.polar()


Jason Merrill 

Instructional Technology Architect
Bank of America   Global Learning 

Join the Bank of America Flash Platform Community  and visit our
Instructional Technology Design Blog
(Note: these resources are only available for Bank of America
associates)






-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of jared
stanley
Sent: Wednesday, August 11, 2010 3:51 PM
To: Flash Coders List
Subject: [Flashcoders] Trigonometry Problem

hey all,

working on getting a grid to expand into a circle.

I would like to:

   - place items in a grid
   - calculate angle from centerpoint(or any desired point)
   - have the items align into a circle at the nearest point, *keeping
the
   same angle*


here's my progress:
http://lab.freestyleinteractive.com/jared/maths/radius.html

the start and end is pretty close to what i want, but i'd like the items
to rearrange keeping the same angle, so an item that was on the top left
would stay on the top left area and an item to the right in the grid
would stay to the right in a circle.

hope this makes sense.

code is below, thanks for any help!

private function init():void{
            //create and center container clip
            mainClip = new MovieClip(); //container clip for all items
            addChild(mainClip);
            mainClip.x = stage.stageWidth / 2;
            mainClip.y = stage.stageHeight / 2;

            createParticles();

            //draw circle around centerpoint for visual reference
            mainClip.graphics.lineStyle(2, 0xffffff, .5);
            mainClip.graphics.drawCircle(0, 0, 100);
            //
            stage.addEventListener(MouseEvent.CLICK, expand);

}

private function createParticles():void{
            for(var i:int = 0; i<maxParticles; i++){

                var particle:Particle = new Particle();//just a sprite
with some properties
               //create grid, calculate original X & Y positons;
                particle.i = i;
                particle.x = (particle.width + 5) * (i % 20);
                particle.y = (particle.height + 5) * Math.floor(i / 20);
                particle.ox = particle.x;
                particle.oy = particle.y;
                //calculate angle from the 0 point;
                var rad:Number = Math.atan2(mainClip.x-particle.y,
mainClip.y-particle.x);
                var angle:Number = rad * 180 / Math.PI;
                particle.angle = angle;
                mainClip.addChild(particle);
                //put all particles in an array to be referenced below
                particlesArr.push(particle);
            }

            //code below is to recenter the grid on the centerpoint
instead of top left
            var tmpw:Number = mainClip.width;
            var tmph:Number = mainClip.height;
            //loop through to offset the grid so it's even within the
page
            for(i = 0; i<particlesArr.length; i++){
                var p:Particle = particlesArr[i];
                p.x -= tmpw* 0.5;
                p.y -= tmph* 0.5;
                p.ox = p.x;
                p.oy = p.y
            }

                addEventListener(Event.ENTER_FRAME, moveAll);
        }


        private function expand(e:MouseEvent):void{
            isGrid = !isGrid;

            for each (var p in particlesArr) {
                p.tgtx = Math.cos(p.angle)*mag;
                p.tgty = Math.sin(p.angle)*mag;
            }

        }


        private function moveAll(e:Event):void{
            var mainTgtX:Number;
            var mainTgtY:Number;

            if(!isGrid){
                for each (var p in particlesArr) {
                    p.x += (p.tgtx - p.x) /20;
                    p.y += (p.tgty - p.y) /20;
                }

            }else {

               for each (p in particlesArr) {
                    p.x += (p.ox - p.x) /20;
                    p.y += (p.oy- p.y) /10;
                }

            }
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to