Posts tagged ‘Math’

ActionScript based clock

a simple analog clock based on shapes.

download

all the ActionScript is all contained in a single file: Clock.as
this is added into the movie with #include “Clock.as” on the frame level

if there is interest, I’ll rewrite this as an object, add a few more options, and improve the script a bit.

it can be made small
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

or large
(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Method to draw an ellipse

Notes:
as this method uses a series of lines to construct the curve, a low angular change will result in a smoother curve. however, if a higher value is used, this will construct what appear to be “warped” polygons. for example, an angular change of 45 degrees (Pi/4 rad) will give an eight sided polygon. changing the dx and dy will still result in an elliptical shape.

ActionScript:

with( MovieClip_Object ) {

	//center of ellipse
	var x0 = Stage.width/2;
	var y0 = Stage.height/2;
	//ellipse bounds
	var dx = 300;
	var dy = 100;
	//angular change
	var dn = 1 * (Math.PI/180);

	var r,x,y;
	lineStyle(1, 0x000000);
	for( var n=0; n<2*Math.PI; n+=dn ) {
		x = dx*Math.cos(n);
		y = dy*Math.sin(n);
		moveTo(x0+x,y0+y);
		x = dx*Math.cos(n+dn);
		y = dy*Math.sin(n+dn);
		lineTo(x0+x,y0+y);
	}

}

Simple Trig With ActionScript

angle conversion:

var angle_deg = angle_rad * 180/Math.PI;
var angle_rad = angle_deg * Math.PI/180;

find (x,y) from angle and radius:

var x = radius * cos( angle_rad );
var y = radius * sin( angle_rad );

a minor issue you will come across in ActionScript is that all of trigonometry functions in Math are based on the angle in radians, and a MovieClip’s _rotation property is based on the angle in degrees (this is not a bug or a flaw, but rather a design decision).

so a bit of simple algebra…

using a ratio:

angle_in_degrees/degrees_in_a_rotation = angle_in_radians/radians_in_a_rotation

one full rotation from origin to origin is 360 degrees or 2π radians:

plugged into the ratio:

angle_in_degrees/360 = angle_in_radians/2π

and solving for either variable:

angle_in_degrees/360 = angle_in_radians/2π

( angle_in_degrees/360 ) * 360 = ( angle_in_radians/2π ) * 360

angle_in_degrees = ( angle_in_radians * 360 ) /2π

angle_in_degrees = ( angle_in_radians * 180 ) /π

angle_in_degrees = angle_in_radians*180/π

and

angle_in_radians/2π = angle_in_degrees/360

( angle_in_radians/2π ) * 2π = ( angle_in_degrees/360 ) * 2π

angle_in_radians = ( angle_in_degrees/360 ) * 2π

angle_in_radians = ( angle_in_degrees/180 ) * π

angle_in_radians = angle_in_degrees*π/180

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives