トップページ > Flash入門 > 矩形波フーリエ級数flash(1)
※画像ファイル“equation.png”が必要です。
//************************************************************************ //2012/03/22 //矩形波フーリエ級数表示flash //Proggramed by Koten-Kairoya //************************************************************************ package { import flash.display.Sprite; import flash.events.Event; import flash.display.Bitmap; import flash.text.*; //画面サイズと背景色,フレームレートを設定 [SWF(width = "550", height = "450", frameRate = "30", backgroundColor = "0xffffcc")] public class Main extends Sprite { //数式の画像ファイルを読み込む [Embed(source = "./equation.png")] public var eq_Img:Class; public var eq_img:Bitmap = new eq_Img(); //数式表示用スプライト public var eq_sp:Sprite = new Sprite(); //グラフ表示用スプライト public var canvas:RectCanvas = new RectCanvas(500, 300); //テキスト表示用スプライト public var num_text:TextField = new TextField(); //テキストフォーマット public var format:TextFormat = new TextFormat("Arial", 24, 0xFF0000); //スライドバー public var slidebar:vSlideBar = new vSlideBar(50,425,1); //main関数 public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } //コンストラクタ private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point //グラフ領域を表示 addChild(canvas); //スライドバーを表示 addChild(slidebar); slidebar.x = 500; //テキストを表示 num_text.defaultTextFormat = format; num_text.text = "1"; num_text.autoSize = TextFieldAutoSize.CENTER num_text.selectable = false; addChild(num_text); num_text.x = 100; num_text.y = 300; //数式を表示 eq_sp.addChild(eq_img); addChild(eq_sp); eq_sp.x = 5; eq_sp.y = 305; //イベントリスナ登録 addEventListener(Event.ENTER_FRAME, onEnterFrame); } //フレームごとに,slidebarの値を呼んで,ボールを適当に拡大・縮小 private function onEnterFrame(e:Event):void { //スライドバーの値をcanvasの値に移す canvas.num = slidebar.value / 10 + 1; //グラフを消す canvas.clear(); //フーリエ級数を計算し,再描画 canvas.makegraph(); //画面上の数式テキストを更新 num_text.text = canvas.num.toString(); } } }
フーリエ級数のグラフを表示するスプライトです。
package { import flash.display.Sprite; public class RectCanvas extends Sprite { //寸法 public var Width:int; public var Height:int; //背景色 public var backgroundColor:int = 0xffffcc; //級数の項数 public var num:int = 1; //データ public var data:Array = new Array(); //コンストラクタ public function RectCanvas(width:int, height:int ) { //寸法を保存 Width = width; Height = height; //クリア clear(); //グラフ描画しておく makegraph(); } public function clear():void { //画面クリア this.graphics.clear(); //背景を色付け this.graphics.beginFill(backgroundColor, 1); this.graphics.drawRect(0, 0, Width, Height); this.graphics.endFill(); //軸を描く this.graphics.lineStyle(2, 0x000000); this.graphics.moveTo(0, Height/2); this.graphics.lineTo(Width, Height / 2); this.graphics.moveTo(Width / 2, 0); this.graphics.lineTo(Width / 2, Height); //データをクリア for (var i:int = 0; i < Width; i++ ) { data[i] = 0; } } public function makegraph():void { //矩形波のフーリエ級数を作る for (var i:int = 0; i < Width; i++ ) { for (var j:int = 1; j <= num; j++ ) { data[i] += 1 / (2 * j - 1) * Math.sin((2*j-1)*4*Math.PI/Width*i + Math.PI); } } //グラフ描画 this.graphics.lineStyle(2,0x0000ff); for (i = 0; i < Width-1; i++ ) { this.graphics.moveTo(i, -150*data[i] + Height / 2); this.graphics.lineTo(i + 1, -150*data[i + 1] + Height / 2); } } } }
※画像ファイル“OrangeBar.png”が必要です。
//************************************************************************ //2012/03/22 //縦方向スライドバークラス //Proggramed by Koten-Kairoya //************************************************************************ package { import flash.display.Graphics; import flash.display.Sprite; import flash.display.Bitmap; import flash.events.Event; import flash.events.MouseEvent; import flash.text.*; public class vSlideBar extends Sprite { //画像ファイルを読み込む [Embed(source = "./OrangeBar.png")] public var bar_Img:Class; public var bar_img:Bitmap = new bar_Img(); //寸法 public var Width:int; public var Height:int; //バー描画用スプライト public var bar:Sprite = new Sprite(); //スライドバーの値 public var value:int = 0; //ドラッグ中フラグ public var click_flag:Boolean = false; //座標保存用 public var pre_y:int = 0; public var delta_y:int = 0; //背景色 public var backgroundColor:int = 0xffe4b5; //軸の色 public var axisColor:int = 0x3f3f3f; //================================================================ //コンストラクタ。引数は,横方向寸法,スライドバーの最大値初期位置。 public function vSlideBar(width:int, height:int, ini_pos:int) { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point //バーを用意 bar.addChild(bar_img); //寸法を保存。縦の寸法はバーの寸法を加えた値 this.Width = width; this.Height = bar.height + height; //矩形描画 this.graphics.beginFill(backgroundColor, 1); this.graphics.drawRect(0, 0, Width, Height); this.graphics.endFill(); //スライドの軸を描画 this.graphics.lineStyle(2, axisColor); this.graphics.moveTo(Width / 2, 0); this.graphics.lineTo(Width/2, Height); //バーを表示 addChild(bar); //バーの初期位置を設定 bar.x = Width/2 - bar.width/2 bar.y = Height - bar.height - ini_pos; //初期値を設定 value = ini_pos; //イベントリスナを登録 bar.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); bar.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); bar.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); bar.addEventListener(MouseEvent.ROLL_OUT, onMouseRollout); this.addEventListener(MouseEvent.ROLL_OUT, onMouseRollout ); } //バーの上でマウス左ボタンが押されると呼ばれる関数 private function onMouseDown(e:MouseEvent):void { //フラグを立てて,クリック中の状態にする click_flag = true; //ドラッグ処理開始 bar.startDrag(); } //バーの上でマウス左ボタンが離された時に呼ばれる関数 private function onMouseUp(e:MouseEvent):void { //フラグを下げて,クリック中の状態をやめる click_flag = false; //ドラッグ処理終了 bar.stopDrag(); } //マウスが動いたときに呼ばれる関数 private function onMouseMove(e:MouseEvent):void { //クリック中の場合は処理を行う if (click_flag == true) { //ドラッグを一度やめる bar.stopDrag(); //横方向には動かさない。常に中心。 bar.x = width / 2 - bar.width / 2; //バーがスライドバーエリア外に出た場合は,もとに戻す if(bar.y+delta_y <=0 ) { bar.y = 0; } if (bar.y >= Height - bar.height) { bar.y = Height - bar.height; } //ドラッグ処理再開 bar.startDrag(); //valueの値を更新 value = Height - bar.height - bar.y; } } //スライドバーの領域からマウスが離れた場合 private function onMouseRollout(e:MouseEvent):void { //クリック中フラグを下げる click_flag = false; //ドラッグ終了 bar.stopDrag(); //バーの横ズレを修正 bar.x = Width/2 - bar.width/2; } } }