Drawing A Sine Wave In Actionscript
August 7th, 2009
10 comments
Just a simple way to draw a sine wave with a specific amplitude and frequency. Use the sliders to adjust the values.
Here’s the main function:
function drawWave(amp:int, freq:int):void{ newSine.graphics.clear(); newSine.graphics.lineStyle(2, 0xff0000, 0.4); newSine.graphics.moveTo(0, yaxis); for(var i:int=0; i<=stage.stageWidth; i++){ var ang:Number = 2 * Math.PI * freq * i/stage.stageWidth; newSine.graphics.lineTo(i, yaxis - amp*Math.sin(ang)); } }
(EDIT 8/14/09: In the SWF above, the sine wave is upside down. That’s because I had a typo when I wrote it. On line 7 of the code, I had typed “…yaxis + amp…” but it should be minus. The code above is correct, but the SWF hasn’t been updated)