平方根を取得して、正方形を作る時のブロック数を調べる

by mmlemon_
♥0 | Line 31 | Modified 2009-09-28 21:22:28 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.events.*;
    public class FlashTest extends Sprite {
        
        // ブロックがいくつかあって、どのような
        // 形状にすればなるべくきれいに並ぶか計算したい時に
        // 試してください。
        // clickHandlerの計算ロジックだけみるといいと思います。
        private var tf:TextField;
        private var input:TextField;
        
        public function FlashTest() {
            // write as3 code here..
            init();
        }
        
        private function init():void
        {
            input = new TextField();
            input.type = "input";
            input.background=true;
            input.backgroundColor=0xeeeeee;
            input.height = 20;
            input.text = "3";
            addChild(input);
            tf = new TextField();
            addChild(tf);
            tf.y = 20;
            stage.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, false);
            
        }
        
        private function clickHandler(event:MouseEvent):void
        {
            // 平方根をとり、切り捨てる。
            var ans:Number = Math.sqrt(parseInt(input.text));
            tf.text = Math.ceil(ans).toString();
        }
    }
}