Please note that ExpatTech no longer operates regular office hours, meeting is by appointment only. Please call +36306031104 for details.

ExpatTech Techblog

Peter Todd 2010.01.29. 15:26

Flash Cookbook - Geometry - Fine tuning Point class Part 3. Angle.

"Dear Master,

if I can create a Point using the Point.polar() method, why on earth can't I reuqest the angle of a Point? Please answer me, you are my only hope.

The Little Grasshopper"

Good question, I could have thought about it myselft. I guess the actual answer is that a Point is not a vector therefore there is no need to calculate any angle as there is no angle for one point. But, come on, why not to think about a point as a vector originated from (0, 0)? We are using a graphical program in the end, so a point is a vector.

public function get angle():Number
{
     var angle:Number = x >= 0 ? Math.atan(y / x) : Math.atan(y / x) - Math.PI;
     if(x == 0 && y == 0) return 0;

     return (angle + PI_2) % PI_2;
}

public function set angle(angle:Number):void
{
     var length:Number = Math.sqrt(x * x + y * y);
           
     x = Math.cos(angle) * length;
     y = Math.sin(angle) * length;
}

AND! It is also good to know in which quarter (of the unit-radius circle) is our point. Also to set this.

public function get quarter():int
{
     if(angle >= 0 && angle < PI_1_2) return 1;
     if(angle >= PI_1_2 && angle < PI) return 2;
     if(angle >= PI && angle < PI + PI_1_2) return 3;
     if(angle >= PI + PI_1_2 && angle < PI_2) return 4;

     return 0;
}
       
public function set quarter(newquarter:int):void
{
     angle = angle + (newquarter - quarter) * PI_1_2;
}