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

BitmapDataに直接描画する直線とBezier曲線

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

package {
    import flash.display.*;
    import flash.text.*;
    
    public class FlashTest extends Sprite {
        private var _bmd : BitmapData;
        private var _tf : TextField;
        
        public function FlashTest() {
            _bmd = new BitmapData(465, 465, false, 0xffffff);
            addChild(new Bitmap(_bmd));
            
            _tf = new TextField();
            _tf.height = 465;
            addChild(_tf);
            
            drawLine(_bmd, 100, 100, 200, 300, 0x000000);
            drawLine(_bmd, 200, 300, 300, 300, 0x000000);
            drawCurve(_bmd, 100, 100, 200, 300, 300, 300, 0x000000);
        }
        
        private static function drawLine(bmd : BitmapData, x1 : Number, y1 : Number, x2 : Number, y2 : Number, color : uint, fill : Number = 1.0) : void
        {
            var nDiv : int = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) * fill + 1;
            for(var i : int = 0;i < nDiv;i++){
                bmd.setPixel(x1 + i / nDiv * (x2 - x1), y1 + i / nDiv * (y2 - y1), color);
            }
        }
        
        private static function drawCurve(bmd : BitmapData, x1 : Number, y1 : Number, xc : Number, yc : Number, x2 : Number, y2 : Number, color : uint, fill : Number = 1.0) : void
        {
            var nDiv : int = lenBezier(x1, y1, xc, yc, x2, y2) * fill + 1;
            /*
            var nDiv : int = (
                Math.sqrt((xc - x1) * (xc - x1) + (yc - y1) * (yc - y1)) + 
                Math.sqrt((x2 - xc) * (x2 - xc) + (y2 - yc) * (y2 - yc))
                ) * fill + 1;
                */
            for(var i : int = 0;i < nDiv;i++){
                var t : Number = i / nDiv;
                var x1c : Number = x1 + t * (xc - x1);
                var y1c : Number = y1 + t * (yc - y1);
                var xc2 : Number = xc + t * (x2 - xc);
                var yc2 : Number = yc + t * (y2 - yc);
                bmd.setPixel(x1c + t * (xc2 - x1c), y1c + t * (yc2 - y1c), color);
            }
        }
        
        private static function lenBezier(x1 : Number, y1 : Number, xc : Number, yc : Number, x2 : Number, y2 : Number, t : Number = 1.0) : Number
        {
            var A : Array = [2 * (xc - x1), 2 * (yc - y1)];
            var B : Array = [2 * ((x2 - xc) - (xc - x1)), 2 * ((y2 - yc) - (yc - y1))];
            var U : Number = Math.sqrt(B[0] * B[0] + B[1] * B[1]);
            if(U < 0.001){
                return Math.sqrt(A[0] * A[0] + A[1] * A[1]) * t;
            }
            var V : Number = (A[0] * B[0] + A[1] * B[1]) / U;
            var W : Number = (A[0] * A[0] + A[1] * A[1]) - V * V;
            if(W < 0.001){
                var q : Number = (A[0] * A[0] + A[1] * A[1]) / (B[0] * B[0] + B[1] * B[1]);
                return (t * t * 0.5 + q * t) * U;
            }
            
            var u : Number;
            u = (U * t + V) / Math.sqrt(W);
            var sup : Number = Math.log(u + Math.sqrt(1 + u * u)) + u * Math.sqrt(1 + u * u);
            u = (V) / Math.sqrt(W);
            var inf : Number = Math.log(u + Math.sqrt(1 + u * u)) + u * Math.sqrt(1 + u * u);
            
            return W / (2 * U) * (sup - inf);
        }
    }
}