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

radian角度をπ範囲に丸める

@see http://tail.s288.xrea.com/open/index.php?%A5%E1%A5%E2%BD%F1%A4%AD%2Fradian%B3%D1%C5%D9%A4%F2%A6%D0%C8%CF%B0%CF%A4%CB%B4%DD%A4%E1%A4%EB
Get Adobe Flash player
by uwi 01 Sep 2009
/**
 * Copyright uwi ( http://wonderfl.net/user/uwi )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tJim
 */

package {

import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;

// @see http://tail.s288.xrea.com/open/index.php?%A5%E1%A5%E2%BD%F1%A4%AD%2Fradian%B3%D1%C5%D9%A4%F2%A6%D0%C8%CF%B0%CF%A4%CB%B4%DD%A4%E1%A4%EB
public class Main extends Sprite
{
    static private const _NUM_TIMES:uint = 500000;
    
    private function _init():void
    {
        _debug(
            "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
            "(誤差は多少生じます)\n"
        );
        
        _measure("からっぽ", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var angle : Number = Math.random() * 100000 - 50000;
                var res : Number = 3.14;
            }
        });
        
        _measure("tail_y", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var angle : Number = Math.random() * 100000 - 50000;
                var res : Number = adjustRadian(angle);
            }
        });
        
        _measure("keno42", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var rad : Number = Math.random() * 100000 - 50000;
                var res : Number = Math.atan2(Math.sin(rad),Math.cos(rad));
            }
        });
        
        _measure("chibitami", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var rad : Number = Math.random() * 100000 - 50000;
                var res : Number = rad - 2.0 * Math.PI * Math.floor( 0.5 + rad / ( 2.0 * Math.PI ) ) ;
            }
        });
               
        _measure("AtuyL", function ():void
        {
            var PI2 : Number = Math.PI * 2; 
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var rad : Number = Math.random() * 100000 - 50000;
                rad %= PI2;
                var res : Number = rad - Math.abs(rad) / rad * PI2 * Math.floor(Math.abs(rad) / Math.PI) || 0;
            }
        });
        
        _measure("uwitenpen", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var angle : Number = Math.random() * 100000 - 50000;
                var res : Number = angle > 0 ? (((angle + Math.PI) % (Math.PI * 2)) - Math.PI) : (Math.PI - ((-angle + Math.PI) % (Math.PI * 2)));
            }
        });
        
        _measure("ma1on", function ():void
        {
            for (var i:uint = 0; i < _NUM_TIMES; i++) {
                var angle : Number = Math.random() * 100000 - 50000;
                var res : Number = adjustRadianM(angle);
            }
        });
        
        _measure("undo", function ():void
        {
            for (var i:uint = 0; i < 10; i++) {
                var angle : Number = Math.random() * 1000;
                while(Math.abs(angle)>180){ angle -= 360; _debug("ちんこ"); }
            }
        });
        
        _debug("\n結果については言及しませんので, 各自ご判断ください.");
    }
    
    private function adjustRadian(angle:Number):Number{
	var pi:Number = Math.PI;
	if (-pi < angle && angle <= pi) return angle;
	angle %= pi*2;
	if (angle <= -pi) return angle + pi*2;
	if (pi < angle) return angle - pi*2;
	return angle;
    }
    
    private function adjustRadianM(angle:Number):Number
    {
	var tmp:int;
	var pi:Number = Math.PI;
	if (-pi < angle && angle <= pi) return angle;
	tmp = -angle / pi;
	return angle + pi*tmp;
    }
    
    private var _field:TextField;
    private var _time:uint;
    
    public function Main():void
    {
        _setup();
        _init();
    }
    
    private function _measure(title:String, func:Function, ...params):void
    {
        _time = getTimer();
        func.apply(null, params);
        _time = getTimer() - _time;
        
        _debug("[ " + title + " ] --> " + _time + " ms");
    }
    
    private function _debug(log:String):void
    {
        _field.appendText(log + "\n");
    }
    
    private function _setup():void
    {
        _field = new TextField();
        _field.width = stage.stageWidth - 40;
        _field.height = stage.stageHeight - 60;
        _field.x = 20;
        _field.y = 60;
        _field.multiline = true;
        _field.wordWrap = true;
        
        var format:TextFormat = _field.defaultTextFormat;
        format.font = "_sans";
        _field.defaultTextFormat = format;
        
        addChild(_field);
        
        var button:Sprite = new Sprite();
        button.graphics.lineStyle(1, 0xBBBBBB);
        button.graphics.beginFill(0xEEEEEE);
        button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
        button.graphics.endFill();
        
        addChild(button);
        
        button.x = 20;
        button.y = 20;
        button.mouseChildren = false;
        button.buttonMode = true;
        
        var field:TextField = new TextField();
        field.width = 100;
        field.height = 20;
        field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
        
        button.addChild(field);
        
        button.addEventListener(MouseEvent.CLICK, function ():void
        {
            _field.text = "";
            _init();
        });
    }
}

}