In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

fastest bitmap line algorithm?

Try this one!
( needs some actual benchmark, if anyone's interested )
Get Adobe Flash player
by FLASHMAFIA 06 Oct 2015
/**
 * Copyright FLASHMAFIA ( http://wonderfl.net/user/FLASHMAFIA )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/l0vb
 */

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [SWF(frameRate=60)]
    public class WFLBresenham extends Sprite
    {
        private var _canvas : BitmapData;
        private var _pressed : int;
        private var _mx0 : int;
        private var _my0 : int;
        private var _mx1 : int;
        private var _my1 : int;


        function WFLBresenham()
        {
            addChild(new Bitmap(_canvas = new BitmapData(465, 465, false, 0xDCDCDC)));

            addEventListener(Event.ADDED_TO_STAGE, onStage);
        }

        private function onStage(e : Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, onStage);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

            addEventListener(Event.ENTER_FRAME, oef);
        }

        private function onMouseDown(event : MouseEvent) : void
        {
            _pressed = 1;

            _mx0 = stage.mouseX;
            _my0 = stage.mouseY;
            _mx1 = 0;
            _my1 = 0;
        }

        private function onMouseUp(event : MouseEvent) : void
        {
            _pressed = 0;
        }

        private function oef(e : Event) : void
        {
            _mx1 = _mx0;
            _my1 = _my0;
            _mx0 = stage.mouseX;
            _my0 = stage.mouseY;

            if (_pressed)
            {
                _canvas.lock();

                // drawLine_A(_canvas, _mx1, _my1, _mx0, _my0, 0x000000);
                drawLine_B(_canvas, _mx1, _my1, _mx0, _my0, 0x000000);

                _canvas.unlock();
            }
        }

        [Inline]
        internal function drawLine_A(dst : BitmapData, x0 : int, y0 : int, x1 : int, y1 : int, color : uint) : void
        {
            var dy : int = y1 - y0;
            var dx : int = x1 - x0;

            if ((dy ^ (dy >> 31)) - (dy >> 31) > (dx ^ (dx >> 31)) - (dx >> 31))
            {
                dy ^= dx;
                dx ^= dy;
                dy ^= dx;

                var s : int = 1;
            }
            else
            {
                s = 0;
            }

            if (dx < 0) var i : int = (~1 + 1);
            else i = 1;

            if (dx == 0) var m : Number = dy;
            else m = (dy / dx);

            if (s)
            {
                for (var n : int = 0; n != dx; n += i)
                {
                    dst.setPixel((x0 + n * m), (y0 + n), color);
                }
            }
            else
            {
                for (n = 0; n != dx; n += i)
                {
                    dst.setPixel((x0 + n), (y0 + n * m), color);
                }
            }
        }

        [Inline]
        internal function drawLine_B(dst : BitmapData, x0 : int, y0 : int, x1 : int, y1 : int, color : uint) : void//
        {
            var shortLen : int = y1 - y0;
            var longLen : int = x1 - x0;

            if (!longLen) if (!shortLen) return;

            var i : int;

            // TODO: check for this above, swap x0/y0/len and optimize loops to ++ and -- (operators twice as fast, still only 2 loops)

            if ((shortLen ^ (shortLen >> 31)) - (shortLen >> 31) > (longLen ^ (longLen >> 31)) - (longLen >> 31))//
            {
                if (shortLen < 0)//
                {
                    // var inc : int = -1;
                    var id : int = -shortLen & 3;

                    if (!shortLen) var muldif : Number = longLen;
                    else muldif = longLen / shortLen;

                    if (id)//
                    {
                        dst.setPixel(x0, y0, color);

                        i--;

                        if (--id)//
                        {
                            dst.setPixel(x0 + i * muldif, y0 + i, color);

                            i--;

                            if (--id)//
                            {
                                dst.setPixel(x0 + i * muldif, y0 + i, color);
                                i--;
                            }
                        }
                    }

                    while (i != shortLen)//
                    {
                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i--;

                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i--;

                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i--;

                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i--;
                    }
                }//
                else//
                {
                    // inc = 1;
                    id = shortLen & 3;

                    if (!shortLen) muldif = longLen;
                    else muldif = longLen / shortLen;

                    if (id)//
                    {
                        dst.setPixel(x0, y0, color);

                        i++;

                        if (--id)//
                        {
                            dst.setPixel(x0 + i * muldif, y0 + i, color);

                            i++;

                            if (--id)//
                            {
                                dst.setPixel(x0 + i * muldif, y0 + i, color);
                                i++;
                            }
                        }
                    }

                    while (i != shortLen)//
                    {
                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i++;

                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i++;

                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i++;

                        dst.setPixel(x0 + i * muldif, y0 + i, color);

                        i++;
                    }
                }
            }//
            else//
            {
                if (longLen < 0)//
                {
                    // inc = -1;
                    id = -longLen & 3;

                    if (!longLen) muldif = shortLen;
                    else muldif = shortLen / longLen;

                    if (id)//
                    {
                        dst.setPixel(x0, y0, color);

                        i--;

                        if (--id)//
                        {
                            dst.setPixel(x0 + i, y0 + i * muldif, color);

                            i--;

                            if (--id)//
                            {
                                dst.setPixel(x0 + i, y0 + i * muldif, color);

                                i--;
                            }
                        }
                    }

                    while (i != longLen)//
                    {
                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i--;

                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i--;

                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i--;

                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i--;
                    }
                }//
                else//
                {
                    // inc = 1;
                    id = longLen & 3;

                    if (!longLen) muldif = shortLen;
                    else muldif = shortLen / longLen;

                    if (id)//
                    {
                        dst.setPixel(x0, y0, color);

                        i++;

                        if (--id)//
                        {
                            dst.setPixel(x0 + i, y0 + i * muldif, color);

                            i++;

                            if (--id)//
                            {
                                dst.setPixel(x0 + i, y0 + i * muldif, color);

                                i++;
                            }
                        }
                    }

                    while (i != longLen)//
                    {
                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i++;

                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i++;

                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i++;

                        dst.setPixel(x0 + i, y0 + i * muldif, color);

                        i++;
                    }
                }
            }
        }
    }
}