GONIOMETRY                                                                                   as2


    In this tutorial I will show you how to generate basic goniometric functions in flash using actionscript 2.0. This might not be the most useful project ever but its nice looking feature. At the end we should get something like this:




    Open new actionscript 2.0 document set stage size to 720 x 400 px. First create x,y axis on the stage with 0,0 point at X:360,Y:200. Now insert new layer on the timeline. Select this layer and insert two numeric stepper componens and one combo box component and one button. Call the steppers stepper1 and stepper2, combo box funkcia_btn and button generate_btn. Select stepper1 and open component inspector (press shift+F7), set maximum value to 3, minimum to 1 and stepSize to 1. Now open component inspector for stepper2 and set first three values the same way as for stepper1. Now open inspector for combo box. Insert these values to data parameter, sin, cos, tan, asin, acos, atan in this order so when you leave adding window data line should look like this [sin,cos,tan,asin,acos,atan]. Set editable parameter to false. Lables parameter should look exactly like data parameter and rowCount parameter should be set to 5. Now draw small yellow circle ,convert it to movieclip and on symbol properties page check export for actionscript and write dot to identifier: line. Insert new layer, select frame one and insert following actionscript code.


var WAVESTART:Number = 360;
var WAVEMP:Number = 200;
function generateFunction(n,k,p){
	if(p == 'sin'){
		for (var i=-360; i<=360; i++) {
			angDeg = i; // v stupnoch
			angRad = angDeg * Math.PI / 180 ; //prevod na radiany
			a = n*Math.sin(k*angRad);
			attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP+(a*60)});
		}
	}else if(p == 'cos'){
		for (var i=-360; i<=360; i++) {
			angDeg = i; // v stupnoch
			angRad = angDeg * Math.PI / 180 ; //prevod na radiany
			a = n*Math.cos(k*angRad);
			attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP+(a*60)});
		}
	}else if(p == 'tan'){
		for (var i=-360; i<=360; i++) {
			angDeg = i; // v stupnoch
			angRad = angDeg * Math.PI / 180 ; //prevod na radiany
			a = n*Math.tan(k*angRad);
			attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP+(a*60)});
		}
	}else if(p == 'asin'){
		for (var i=-360; i<=360; i++) {
			angDeg = i; // v stupnoch
			angRad = angDeg * Math.PI / 180 ; //prevod na radiany
			a = n*Math.asin(k*angRad);
			attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP+(a*60)});
		}
	}else if(p == 'acos'){
		for (var i=-360; i<=360; i++) {
			angDeg = i; // v stupnoch
			angRad = angDeg * Math.PI / 180 ; //prevod na radiany
			a = n*Math.acos(k*angRad);
			attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP+(a*60)});
		}
	}else if(p == 'atan'){
		for (var i=-360; i<=360; i++) {
			angDeg = i; // v stupnoch
			angRad = angDeg * Math.PI / 180 ; //prevod na radiany
			a = n*Math.atan(k*angRad);
			attachMovie("dot", "dot"+i, i, {_x:WAVESTART+i, _y:WAVEMP+(a*60)});
		}
	}
}
generate_btn.onRelease = function(){
	n = stepper1.value;
	k = stepper2.value;
	p = funkcia_btn.value;
	generateFunction(n,k,p);
}


    First two lines are used variables definition. On line 3 starts the main function. The if condition selects goniometric function based on value sent by comboBox component. Each if condition contains for loop that draws actuall function by duplicating the dot movieclip and placing them on stage using coordinates generated on line 6-8 (in case of first loop). Lines 43-58 represents the function for button that send all the variables from steppers and combobox to the generateFunction.



DOWNLOAD SOURCE FILE