SINEWAVE
/**
* Copyright christian ( http://wonderfl.net/user/christian )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/xPmf
*/
package
{
import flash.utils.*;
import flash.display.*;
import flash.geom.Rectangle;
import com.bit101.components.*;
/** @author SPANVEGA // CHRISTIAN **/
// http://en.wikipedia.org/wiki/Sine_wave
[ SWF (width = '465', height = '465', backgroundColor = '0xFFFFFF', frameRate = '0')]
public class SINEWAVE extends Sprite
{
private var w : uint = 465, h : uint = 465;
private var r : Rectangle = new Rectangle (0, 0, w, h);
private var b : BRESENHAM = new BRESENHAM (w, h, true, 0);
private var amp : Number = 116.25, axe : int = h >> 1;
private var p : int = 0, t : Number = 0, i : Number;
private var rad : Number, phase : Number = 1.5;
private var sin : Number, sin_p : Number;
private var cos : Number, cos_p : Number;
private var update : Boolean = false;
private var fps : int = 50;
public function SINEWAVE ()
{
Wonderfl.disable_capture ();
stage.scaleMode = 'noScale';
addChild (new Bitmap (b));
with (Style) { BACKGROUND = 0xFFFFFF; LABEL_TEXT = DROPSHADOW = BUTTON_FACE = 0x404040; }
with (addChild (new Sprite()))
{ y = 410; graphics.beginFill (0xFFFFFF, 0.5); graphics.drawRect (0, 0, w, h - y); }
var k : HUISlider = new HUISlider (this, 10, 415, 'phase',
function () : void { phase = k.value; update = true; });
k.setSliderParams (-5, 5, phase); k.width = 471; k.labelPrecision = 2;
var a : HUISlider = new HUISlider (this, 10, 430, 'amplitude',
function () : void { amp = a.value; update = true; });
a.setSliderParams (0, h / 2, amp); a.width = 471; a.labelPrecision = 0; a.tick = 1;
var f : HUISlider = new HUISlider (this, 10, 445, 'framerate',
function () : void { clearInterval (i); i = setInterval (render, 1000 / f.value); });
f.setSliderParams (1, 100, fps); f.width = 471; f.labelPrecision = 0; f.tick = 1;
i = setInterval (render, 1000 / fps);
}
private function render () : void
{
if (update)
{
update = false;
b.fillRect (r, 0);
sin_p = cos_p = Number.NaN;
for (var i : int = 0; i < p; i++) draw (t - phase * (p - i), i);
}
if (p == 0) { b.fillRect (r, 0); }
draw (t, p);
with (graphics)
{
clear ();
beginFill (0xBEBEBE, 0.25);
drawRect (0, 0, p, h);
endFill ();
// rules
lineStyle (1, 0xFF808080, 1);
moveTo (0, axe);
lineTo (w, axe);
moveTo (0, axe - amp);
lineTo (w, axe - amp);
moveTo (0, axe + amp);
lineTo (w, axe + amp);
// amplitude
lineStyle (0, 0, 0);
beginFill (0xBEBEBE, 0.25);
drawCircle (p, axe, amp);
endFill ();
// sin
lineStyle (1, 0xFF0000, 0.75);
beginFill (0xFF0000, 0.15);
moveTo (p, axe);
lineTo (p, axe + sin);
lineTo (p + cos, axe + sin);
lineStyle (1, 0xBEBEBE, 0.75);
beginFill (0xBEBEBE, 0.25);
moveTo (p, axe);
lineStyle (1, 0x808080, 1);
lineTo (p + cos, axe + sin);
lineStyle (0, 0, 0);
lineTo (p + cos, axe);
endFill ();
// cos
lineStyle (1, 0x0000FF, 0.75);
moveTo (p, axe);
lineTo (p, axe + cos);
lineTo (p + cos, axe + sin);
// time
lineStyle (1, 0x808080, 1);
moveTo (p, axe);
lineTo (p + cos, axe + sin);
}
p += 1; p %= w; t += phase;
}
private function draw (t : Number, p : int) : void
{
rad = t * (Math.PI / 180);
sin = Math.sin (rad) * amp;
cos = Math.cos (rad) * amp;
b.line (p - 1, sin_p ? sin_p : axe + sin, p, sin_p = axe + sin, 0xFFFF0000);
b.line (p - 1, cos_p ? cos_p : axe + cos, p, cos_p = axe + cos, 0xFF0000FF);
}
}
}
//
import flash.display.BitmapData;
final class BRESENHAM extends BitmapData
{
// http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
public function BRESENHAM (width : uint, height : uint, transparent : Boolean = true, color : uint = 0)
{
super (width, height, transparent, color);
}
public function line (x0 : int, y0 : int, x1 : int, y1 : int, color : uint = 0) : void
{
var dx : int = x1 - x0;
var dy : int = y1 - y0;
var sx : int = dx >= 0 ? 1 : -1;
var sy : int = dy >= 0 ? 1 : -1;
dx = dx >= 0 ? dx : -dx;
dy = dy >= 0 ? dy : -dy;
var err : int = dx - dy, e2 : int;
while (true)
{
setPixel32 (x0, y0, color);
if (x0 == x1 && y0 == y1) break;
e2 = err << 1;
if (e2 > -dy) { err -= dy; x0 += sx; }
if (e2 < dx) { err += dx; y0 += sy; }
}
}
}