ActionScript: Trigonometry
I wanted to start experimenting, learning and understanding the basics of certain key principles that can be used in ActionScript to create animation and interactivity. The first in this series of experiments focuses on Trigonometry. I am hoping that by creating the basics from tutorials, books and other resources I can develop my skills and understanding and then start developing my own examples from this.
The first thing I wanted to get to grips with is using Sine, Cosine and Tangent in ActionScript as well as Arcsine, Arccosine and Arctangent. I also needed to practise converting values to and from degrees (for measuring rotation as in 360) and radians (used in computer programming for measuring rotation). Finally I used Pythagorean Theorem to calculate the distances between 2 points at any angle.
onEnterFrame = function()
{
var dx = _xmouse - arrow._x;
var dy = _ymouse - arrow._y;
var radians = Math.atan2(dy, dx);
arrow._rotation = radians * 180 / Math.PI;
}
var angle = 0;
var ycenter = 200;
var range = 50;
var xspeed = 2;
var yspeed = .05;
moveTo(0, ycenter);
lineStyle(2, 0x999999,100, true);
var x = 0;
onEnterFrame = function()
{
x += xspeed
var y = ycenter + Math.sin(angle) * range;
lineTo(x, y)
angle += yspeed;
if(x > 640)
{
clear();
moveTo(0, ycenter);
lineStyle(2, 0×999999,100, true);
x = 0;
}
}
var angle = 0;
var xcenter = 320;
var ycenter = 200;
var radius = 100;
var speed = .1;
onEnterFrame = function()
{
ball._x = xcenter + Math.cos(angle) * radius;
ball._y = ycenter + Math.sin(angle) * radius;
angle += speed;
}
var angle = 0;
var xcenter = 320;
var ycenter = 200;
var radiusX = 250;
var radiusY = 10;
var speed = .03;
onEnterFrame = function()
{
ball._x = xcenter + Math.cos(angle) * radiusX;
ball._y = ycenter + Math.sin(angle) * radiusY;
ball._xscale = ball._yscale = Math.sin(angle) * 30 + 70;
angle += speed;
}
function onEnterFrame()
{
var dx:Number = ball._x - _xmouse;
var dy:Number = ball._y - _ymouse;
var dist = Math.sqrt(dx * dx + dy * dy);
output.text = Math.round(dist) + ” pixels away”;
clear();
lineStyle(1, 0×999999, 100);
moveTo(ball._x, ball._y);
lineTo(_xmouse, _ymouse);
}