让你的微小离子随机分布更均匀–布朗运动

1、布朗运动揭秘http://baike.baidu.com/view/17875.htm
    2、在动画制作过程中到处都能看到随机的应用,然而我们直接通过随机数生成的随机数并不是很均匀,当我们想让物体感觉没有外力、任何意识均匀分布时(如:浩瀚的星空,盒子里乱飞的蜜蜂苍蝇,太阳光照射的灰尘等等),布朗运动就相当管用了。
    3、基本原理:
         3.1方形分布(舞台中心上下方各100像素):
  
[codes=java]for(var i:uint=0;i    {  
        circle = new UCircle();  
        circle.x = stage.StageWidth/2 + Math.random() * 100 – 50;  
        circle.y = stage.StageHeight/2 +Math.random() * 100 – 50;  
        myCirclePanel.addChild(circle);  
        circles.push(circle);  
    }

         3.2:圆形分布:
[codes=java]for(var i:uint=0;i    {  
        circle = new UCircle();  
        var radius:Number = Math.sqrt(Math.radom()) * maxRadius;  
        var angle:Number = Math.random() * (Math.PI * 2);  
        circle.x = stage.StageWidth/2 + Math.cos(angle) * radius;  
        circle.y = stage.StageHeight/2 + Math.sin(angle) * radius;  
        myCirclePanel.addChild(circle);  
        circles.push(circle);  
    }  

    3.3:偏向分布(趋于中心分布,越靠近中心越多):
[codes=java]for(var i:uint=0;i    {  
        circle = new UCircle();  
        xpos = 0;  
        ypos = 0;  
        for(var j:uint = 0;j        {  
            xpos += Math.random() * stage.stageWidth;  
            ypos += Math.random() * stage.stageHeight;  
        }  
        circle.x = xpos / iterations;  
        circle.y = ypos / iterations;  
        myCirclePanel.addChild(circle);  
        circles.push(circle);  
    }

    4、综合应用(绘制了300个闪烁的小萤火虫子在盒子中飞舞):
[codes=java]package  
    {  
        import flash.display.Sprite;  
        import flash.events.Event;  
        public class BrownClass extends Sprite  
        {  
            //myMask  
            //myMolecule  
            private var numCircle:uint = 300;  
            private var iterations:uint = 6;  
            private var circles:Array;  
            public function BrownClass()  
            {  
                init();  
            }  
            public function init():void  
            {  
                var circle:UCircle;  
                circles = new Array();  
                var xpos:Number ;  
                var ypos:Number;  
                for(var i:uint=0;i                {  
                    circle = new UCircle();  
                    xpos = 0;  
                    ypos = 0;  
                    for(var j:uint = 0;j                    {  
                        xpos += Math.random() * stage.stageWidth;  
                        ypos += Math.random() * stage.stageHeight;  
                    }  
                    circle.x = xpos / iterations;  
                    circle.y = ypos / iterations;  
                    myCirclePanel.addChild(circle);  
                    circles.push(circle);  
                }  
                this.addEventListener(Event.ENTER_FRAME,onEnterframe);  
            }  
            function onEnterframe(e:Event):void  
            {  
                var circle:UCircle;  
                
                for(var i:uint=0;i                {  
                    circle = circles[i];  
                    circle.x += Math.random() * 0.8 – 0.4;  
                    circle.y += Math.random() * 0.8 – 0.4;  
                    //将圆点局限在盒子内运动  
                    if(circle.x                    {  
                        circle.x=myMask.x  
                    }  
                    else if(circle.x>(myMask.x + myMask.width – circle.width))  
                    {  
                        circle.x = myMask.x + myMask.width – circle.width;  
                    }  
                    
                    if(circle.y                    {  
                        circle.y=myMask.y  
                    }  
                    else if (circle.y>(myMask.y + myMask.height – circle.height))  
                    {  
                        circle.y= myMask.y + myMask.height – circle.height;  
                    }  
                }  
            }  
        }  
    }  

5、DEMO演示及源代码:http://www.flash8.net/fla/10071.shtml http://www.flash8.net/fla/10071.shtml

“让你的微小离子随机分布更均匀–布朗运动”的一个回复

发表评论