Sine-function

Code Actionscript 2.0

Frame action

// initialize drawing
createEmptyMovieClip("drawing", 1);
drawing.lineStyle(0, 0x000000, 100);
drawing.moveTo(0, 200);

// go from left to right
// and calculate function values
for (px = 0; px < 400; px = px + 1) {
py = graph(px);
drawing.lineTo(px, py);
}
function graph(px) {
py = sine(px);
// set center and height of curves:
py = stretch(py, 200, 100);
return (py);
}
function sine(x) {
// sine conversion for values
//between 0 and 400 (stage width)
s = Math.sin(x * 2 * Math.PI / 400);
return s;
}
function stretch(value, center, amplitude) {
// scales and shifts value
st = int(value * amplitude + center);
return st;
}

Code Actionscript 3.0

// initialize drawing var drawing:MovieClip = new MovieClip(); drawing.graphics.lineStyle(0, 0x000000, 1); drawing.graphics.moveTo(0, 200); this.addChild(drawing); // go from left to right // and calculate function values for (var px = 0; px < 400; px = px + 1) { var py = graph(px); drawing.graphics.lineTo(px, py); } function graph(px:int) { py = sine(px); // set center and height of curves: py = stretch(py, 200, 100); return py; } function sine(X:int) { // sine conversion for values //between 0 and 400 (stage width) var s = Math.sin(X * 2 * Math.PI / 400); return s; } function stretch(value:Number, center:int, amplitude:int) { // scales and shifts value var st = value * amplitude + center; return st; }

Description

This script draws a sine-function. We haven written two auxiliary functions sine() and stretch(), to make the code easier to read. The function sine() converts angles, so the stage dimensions contain a nice curve. The stretch() function scales and shifts the sine values, that are originally between -1 and 1. If you change the parameters in stretch(), you will achieve different scalings and shifts of the graph function. The frequency of the sine(x) function depends on the factor, with which x is multiplied.

related to: Sine grid, Line