Sine waves are great and all, but for us non-mathematicians they are difficult to visualize and tweak. So, lets dissect and analyze this ultra-common trig function.
Scalpel, please
A sine wave calculation has one core parameter, an angle. In AS3 this angle needs to be in radians. But we can expand this into five parameters and two modifiers for a total of seven parts. That’s right, seven different parts:
- Amplitude: Deviation from center (aka. height of the wave)
- Frequency: Radians per second (aka. speed of the wave)
- Offset: Shifting the wave’s position from center (usually a y-offset)
- Phase: Shifting the wave by time
- Wavelength: Distance between waves
- Position: Position at which to calculate the state of the wave (usually an x-position)
- Time: A point of time at which to calculate the wave
Each of these parts can be combined into a single line of code:
amplitude * Math.sin(((2 * Math.PI) / wavelength) * position - (frequency * time) + phase) + offset;
That makes it really easy to wrap it in a function or a class, like so:
public class Sine { // ------------------ // // --- properties --- // // ------------------ // public var amplitude:Number; public var frequency:Number; public var offset:Number; public var phase:Number; public var wavelength:Number; // ---------------------- // // --- public methods --- // // ---------------------- // // constructor public function Sine(amplitude:Number=0, frequency:Number=0, offset:Number=0, phase:Number=0, wavelength:Number=0) { this.amplitude = amplitude; this.frequency = frequency; this.offset = offset; this.phase = phase; this.wavelength = wavelength; } public function calculate(time:Number, position:Number):Number { return this.amplitude * Math.sin(((2 * Math.PI) / this.wavelength) * position - (this.frequency * time) + this.phase) + this.offset; } }
Rock n Roll
Now that you have added this sweet-ass tool to your display programming toolbox, you can use this little Flex app to help tweak your sine waves:
That’s it for now
Download the source for this app

