Infinity loop function simulation
This is a blueprint made for one of the projects im working on right now
Mouse click - toogle slow motion effect
Link to plotted graph:
http://graphsketch.com/?eqn1_color=1&eqn1_eqn=sin%28%20x%20*%20pi%20*%202%20%29%20*%205&eqn2_color=2&eqn2_eqn=sin%28%20x%20*%20pi%20*%204%20%29%20*%202&eqn3_color=3&eqn3_eqn=&eqn4_color=4&eqn4_eqn=&eqn5_color=5&eqn5_eqn=&eqn6_color=6&eqn6_eqn=&x_min=-.25&x_max=1.25&y_min=-5&y_max=5&x_tick=.25&y_tick=1&x_label_freq=1&y_label_freq=1&do_grid=0&do_grid=1&bold_labeled_lines=0&bold_labeled_lines=1&line_width=4&image_w=850&image_h=525
/**
* Copyright WLAD ( http://wonderfl.net/user/WLAD )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2PSp
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BitmapFilter;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var xAxisGraph:Bitmap;
private var yAxisGraph:Bitmap;
private var data:BitmapData;
private var cont:Sprite;
private var circleShape:Shape;
private var stats:Shape;
private var sw:int;
private var sh:int;
private var stageRect:Rectangle;
private var origin:Point;
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
sw = stage.stageWidth;
sh = stage.stageHeight;
stageRect = new Rectangle(0, 0, sw, sh);
data = new BitmapData(sw, sh, true, 0xFF000000);
var bitmap:Bitmap = new Bitmap(data);
addChild(bitmap);
cont = new Sprite();
addChild(cont);
px = sw / 2;
py = sh / 2 + 20;
origin = new Point( px , py );
circleShape = new Shape();
cont.addChild(circleShape);
xAxisGraph = new Bitmap(new BitmapData( 200, 80, false, 0xFF000000));
cont.addChild(xAxisGraph);
xAxisGraph.x = 10;
xAxisGraph.y = 0;
yAxisGraph = new Bitmap(new BitmapData( 200, 80, false, 0xFF000000));
cont.addChild(yAxisGraph);
yAxisGraph.x = 250;
yAxisGraph.y = 0;
stats = new Shape();
stats.x = origin.x;
stats.y = origin.y;
cont.addChild( stats );
addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(MouseEvent.CLICK, function(e:*):void
{
slow = !slow;
stage.frameRate = slow ? 5 : 60;
});
}
private var slow:Boolean = false;
private var time:Number = 0;
private var cycle:Number = 12.5;
private var ms:Number = 60 / 1000;
private var px:Number;
private var py:Number;
private var xAMP:Number = 200;
private var yAMP:Number = 150;
private var colorTransform:ColorTransform = new ColorTransform(.9, .9, .9);
private var filter:BitmapFilter = new BlurFilter(4, 4);
private function loop(e:Event):void
{
var c:int = slow ? 1 : 3;
while ( c-- > 0 ) {
moveCircle();
drawCircle();
drawBitmap();
}
drawStats();
}
private function moveCircle():void
{
time += ms;
if (time > cycle)
time -= cycle;
px = Math.sin(2 * Math.PI * time / cycle) * xAMP;
py = Math.sin(4 * Math.PI * time / cycle) * yAMP;
text( "X:\t" + px.toFixed(1) + "\nY:\t" + py.toFixed(1), cont.graphics,
{
x: 10,
y: sh - 40,
clear: true
});
}
private function drawCircle():void
{
circleShape.graphics.clear();
circleShape.graphics.beginFill(0xFFFFFF);
circleShape.graphics.drawCircle(px + origin.x, py + origin.y, 4);
circleShape.graphics.endFill();
}
private function drawBitmap():void
{
var d:Number = .4;
data.lock();
circleShape.visible = true;
//colorTransform.redMultiplier += ( .5 + Math.random() ) * d;
//colorTransform.blueMultiplier += ( .5 + Math.random() ) * d;
//colorTransform.greenMultiplier += ( .5 + Math.random() ) * d;
//
//colorTransform.redMultiplier = Math.min( Math.max( colorTransform.redMultiplier, .8), 1);
//colorTransform.blueMultiplier = Math.min( Math.max( colorTransform.redMultiplier, .8), 1);
//colorTransform.greenMultiplier = Math.min( Math.max( colorTransform.redMultiplier, .8), 1);
//
//data.colorTransform( stageRect );
data.applyFilter(data, stageRect, new Point(0, 0), filter);
data.draw(circleShape);
circleShape.visible = false;
data.unlock();
}
private function drawStats():void
{
var g:Graphics = stats.graphics;
g.clear();
// Draw Y axis line
g.lineStyle(.1, 0x1885A9);
g.moveTo(-origin.x, py);
g.lineTo(origin.x, py);
// Draw X axis line
g.lineStyle(.1, 0x17AA8C);
g.moveTo(px, -origin.y);
g.lineTo(px, origin.y );
// Draw origin axis
g.lineStyle(.1, 0x606060);
g.moveTo(0, -origin.y);
g.lineTo(0, origin.y);
g.moveTo(-origin.x, 0);
g.lineTo(origin.x, 0);
// Draw origin
g.lineStyle(2, 0xFFFFFF);
g.drawCircle(0, 0, 4);
var x:Number, y:Number;
// Draw size
var s:int = slow ? 2 : 5;
// Draw X axis graph
x = xAxisGraph.x -origin.x; y = xAxisGraph.y -origin.y;
g.lineStyle(.1, 0x17AA8C);
g.moveTo( x, y );
g.lineTo( x, y + 80 );
g.lineTo( x, y + 40 );
g.lineTo( x + 200, y + 40 );
xAxisGraph.bitmapData.fillRect( new Rectangle( 0, 0, s, xAxisGraph.height), 0xFF000000 );
xAxisGraph.bitmapData.fillRect( new Rectangle( 0, 40 * (px / xAMP) + xAxisGraph.height/2, s, s), 0xFFFF0000 );
xAxisGraph.bitmapData.scroll(s, 0);
text( "f(x) = sin( x * pi * 2 ) * xAMP", g,
{
x: x - 5,
y: y + 85,
color: 0x3AE4C2,
size: 14,
font: 'Verdana'
});
// Draw Y axis graph
x = yAxisGraph.x -origin.x; y = yAxisGraph.y -origin.y;
g.lineStyle(.1, 0x1885A9);
g.moveTo( x, y );
g.lineTo( x, y + 80 );
g.lineTo( x, y + 40 );
g.lineTo( x + 200, y + 40 );
yAxisGraph.bitmapData.fillRect( new Rectangle( 0, 0, s, yAxisGraph.height), 0xFF000000 );
yAxisGraph.bitmapData.fillRect( new Rectangle( 0, 40 * (py / yAMP) + yAxisGraph.height/2, s, s), 0xFFFF0000 );
yAxisGraph.bitmapData.scroll(s, 0);
text( "f(y) = sin( y * pi * 4 ) * yAMP", g,
{
x: x - 5,
y: y + 85,
color: 0x40BBE3,
size: 14,
font: 'Verdana'
});
}
private var tempTF:TextField;
private function text( text:String, target:Graphics, params:Object = null ):void
{
var color:uint = uint( getParam('color', params, 0xFFFFFF) );
var size:uint = uint( getParam('size', params, 14) );
var x:Number = Number( getParam('x', params, 0) );
var y:Number = Number( getParam('y', params, 0) );
var clear:Boolean = Boolean( getParam('clear', params, false) );
var bold:Boolean = Boolean( getParam('bold', params, false) );
var font:String = String( getParam('font', params, 'Courier New') );
if ( !tempTF )
{
tempTF = new TextField();
tempTF.autoSize = "left";
}
tempTF.defaultTextFormat = new TextFormat(font, size, color, bold);
tempTF.textColor = color;
tempTF.text = text;
var b:BitmapData = new BitmapData( tempTF.width, tempTF.height, true, 0 );
b.draw( tempTF );
if ( clear ) target.clear();
target.lineStyle();
target.beginBitmapFill( b , new Matrix(1, 0, 0, 1, x, y), false );
target.drawRect( x, y, tempTF.width, tempTF.height );
target.endFill();
}
private function getParam( param:String, fromObject:Object, defaultValue:* = null ):*
{
if ( fromObject == null ) return defaultValue;
return fromObject.hasOwnProperty( param ) ? fromObject[param] : defaultValue;
}
}
}