Some more faster (forked from: More speed (forked from: Diffusion-Limited Aggregation))

by smirnov48 forked from More speed (forked from: Diffusion-Limited Aggregation) (diff: 1770)
♥0 | Line 877 | Modified 2014-08-04 09:53:58 | MIT License
play

ActionScript3 source code

/**
 * Copyright smirnov48 ( http://wonderfl.net/user/smirnov48 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/4k1O
 */

// forked from smirnov48's More speed (forked from: Diffusion-Limited Aggregation)
// forked from greentec's Diffusion-Limited Aggregation
package {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    import net.hires.debug.Stats;

    public class FlashTest extends Sprite {
        
        private var centerX:Number = 465 / 2;
        private var centerY:Number = 465 / 2;
        private var _bitmap:Bitmap;
        private var _bitmapData:BitmapData;
        private var _baseBitmap:Bitmap;
        private var _baseBitmapData:BitmapData;
        private var hitBitmapData:BitmapData;
        private var particleNum:int = 5000;
        private var particles:Array = [];
        private var tx:Number = 0;
        private var ty:Number = 0;
        private var i:int = 0;
        private var n:int = 0;
        private var p:Particle;
        private var frameCount:int = 0;
        private var base:Vector.<Vector.<Boolean>> = new Vector.<Vector.<Boolean>>(468, true);
        private var time:uint = 0;
        private var iq:int = 0;

        public function FlashTest() {
            var i:int;
            for (i = 0; i < 468; i++) {
                base[i] = new Vector.<Boolean>(468, true);
            }

            for (i = 0; i < 4; i++) {
                base[232 + i][232 + 0] = true;
                base[232 + i][232 + 1] = true;
                base[232 + i][232 + 2] = true;
                base[232 + i][232 + 3] = true;
            }

            _bitmapData = new BitmapData(465, 465, true, 0xff292929);
            _bitmap = new Bitmap(_bitmapData);
            addChild(_bitmap);

            _baseBitmapData = new BitmapData(465, 465, true, 0x00ffffff);
            _baseBitmap = new Bitmap(_baseBitmapData);
            addChild(_baseBitmap);
            _baseBitmap.alpha = 1;

            hitBitmapData = new BitmapData(2, 2, true, 0xffffffff);

            initParticles();

            var stats:Stats = new Stats();
            addChild(stats);
            
            addEventListener(Event.ENTER_FRAME, onLoop);
        }

        public function onLoop(e:Event):void
        {
            i = 0;
            while (i < particleNum) {
            {

                    p = particles[i];
                    n = p.x;
                    tx = Math.round(p.x);
                    n = p.y;
                    ty = Math.round(p.y);
                    if (
                        base[tx + 0][ty + 0] || 
                        base[tx + 0][ty + 1] ||
                        base[tx + 0][ty + 2] ||
                        base[tx + 1][ty + 0] ||
                        base[tx + 1][ty + 1] ||
                        base[tx + 1][ty + 2] ||
                        base[tx + 2][ty + 0] ||
                        base[tx + 2][ty + 1] ||
                        base[tx + 2][ty + 2]
                        )
                    {

                        base[tx + 1][ty + 1] = true;

                        //respawn
                        if (Math.random() < .5)
                        {
                            p.x = Math.random() * 465;
                            p.y = Math.random() < .5 ? 0 : 465;
                        }
                        else
                        {
                            p.x = Math.random() < .5 ? 0 : 465;
                            p.y = Math.random() * 465;
                        }
                        //end respawn
                    }
                    else
                    {
                        p.vx += Math.random() * .1 - .05;
                        p.vy += Math.random() * .1 - .05;
                        p.x += p.vx;
                        p.y += p.vy;

                        p.vx *= .99;
                        p.vy *= .99;

                        if (p.x > 465)
                        {
                            p.x -= 465;
                        }
                        else if (p.x < 0)
                        {
                            p.x += 465;
                        }

                        if (p.y > 465)
                        {
                            p.y -= 465;
                        }
                        else if (p.y < 0)
                        {
                            p.y += 465;
                        }
                    }
                }    
                i++;
            }
//copies

            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }
//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }
//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//

            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }

//
            i = 0;

            while (i < particleNum) {

            {



                    p = particles[i];

                    n = p.x;

                    tx = Math.round(p.x);

                    n = p.y;

                    ty = Math.round(p.y);

                    if (

                        base[tx + 0][ty + 0] || 

                        base[tx + 0][ty + 1] ||

                        base[tx + 0][ty + 2] ||

                        base[tx + 1][ty + 0] ||

                        base[tx + 1][ty + 1] ||

                        base[tx + 1][ty + 2] ||

                        base[tx + 2][ty + 0] ||

                        base[tx + 2][ty + 1] ||

                        base[tx + 2][ty + 2]

                        )

                    {



                        base[tx + 1][ty + 1] = true;



                        //respawn

                        if (Math.random() < .5)

                        {

                            p.x = Math.random() * 465;

                            p.y = Math.random() < .5 ? 0 : 465;

                        }

                        else

                        {

                            p.x = Math.random() < .5 ? 0 : 465;

                            p.y = Math.random() * 465;

                        }

                        //end respawn

                    }

                    else

                    {

                        p.vx += Math.random() * .1 - .05;

                        p.vy += Math.random() * .1 - .05;

                        p.x += p.vx;

                        p.y += p.vy;



                        p.vx *= .99;

                        p.vy *= .99;



                        if (p.x > 465)

                        {

                            p.x -= 465;

                        }

                        else if (p.x < 0)

                        {

                            p.x += 465;

                        }



                        if (p.y > 465)

                        {

                            p.y -= 465;

                        }

                        else if (p.y < 0)

                        {

                            p.y += 465;

                        }

                    }

                }    

                i++;

            }


//
            if (frameCount++ > 35) {
                frameCount = 0;
                drawBase();
            }
        }

        public function drawBase():void
        {
            for (var i:int = 1; i < 466; i++) {
                for (var j:int = 1; j < 466; j++) {
                    if (base[i][j]) {
                        _baseBitmapData.setPixel32(i - 1, j - 1, 0xff00ffff);
                    }
                }
            }
        }

        public function initParticles():void
        {
            var i:int;
            var p:Particle;
            for (i = 0; i < particleNum; i += 1)
            {
                p = new Particle(Math.random() * 465, Math.random() * 465);
                particles.push(p);
            }
        }
    }
}

Class
{
    class Particle 
    {
        public var x:Number;
        public var y:Number;
        public var vx:Number;
        public var vy:Number;

        public function Particle(x:Number, y:Number) 
        {
            this.x = x;
            this.y = y;
            this.vx = Math.random() * .1 - .05;
            this.vy = Math.random() * .1 - .05;
        }
    }
}