Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Tuesday, March 17, 2009

Point methods... you were there all the time?

Tuesday, March 17, 2009    0 Comments

Today Jason Merrill open my eyes as well as my mind.

In the past, I had done some intersection type math... angle from center intersecting a square, intersecting a circle, etc. and it was all messy code glommed from Google and various sources. The stuff worked, but I never exactly knew why.

Today I had the need to do the "angle from center a certain distance from center of circle = what x,y" thing. And I started to look for my old code and decided to Google and blast a question to Flashcoders.

I got the simple response: In AS3, Point.polar()

Huh? I never would have thought to dig into Point to look for something like that. But there she was, sitting pretty at the end of the AS3 bar, waiting for me to buy her a drink. And I bought her many.

var coord:Point = Point.polar( radiusFromCenter, radians );
trace( coord.x, coord, y );

I'd like to buy Jason a few rounds too, after all it IS Saint Patrick's Day. He also posted a few methods from his extended Math class which are really quite useful too.
public static function polarToCartesian(distance:Number, degrees:Number):Point
{
var radians:Number = (degrees * Math.PI) / 180;
return Point.polar(distance, radians);
}

public static function degreesToRadians(degrees:Number):Number
{
return (degrees * Math.PI)/180;
}

public static function radiansToDegrees(radians:Number):Number
{
return (radians*180)/Math.PI;
}

public static function cartesianAngle(fromPoint:Point, toPoint:Point):Number
{
var radians:Number = Math.atan2(toPoint.y-fromPoint.y, toPoint.x-fromPoint.x);
var backAzimuthDegrees:Number = MathTranslation.radiansToDegrees(radians);
return MathTranslation.getBackAzimuth(backAzimuthDegrees);
}

public static function getBackAzimuth(angle:Number):Number
{
var backAzimuth:Number;
if(angle < 180)
{
var tempNeg:Number = angle-180;
backAzimuth = tempNeg + 360;
}
else
{
backAzimuth = angle-180;
}
return backAzimuth;
}

Labels: , ,