/* ----------------------------------------------------------------------------
Points Class v1.2
Copyright © 2003, 2004 David Stiller
History:
v1.2 - Added getPoint() method.
v1.1 - Renamed class Points; converted class members to static; added
optional rounding to returned values.
v1.0 - Released December 4, 2003.
Description:
Provides numerical relationships between two points.
Usage:
Points.methodName(args);
When an argument calls for a point, the point must be and object with
x and y properties.
Methods:
Points.getDistance(pointA:Object, pointB:Object, bRound:Boolean):Number
Distance between two points, optionally rounded.
Points.getAngle(pointA:Object, pointB:Object, bRound:Boolean):Number
Angle of hypotenuse defined by two points, optionally rounded.
eg. _/_ = 45°, _|_ = 90°, _\_ = 135°
Points.getRotation(point1:Object, point2:Object, bRound:Boolean):Number
Rotation of getAngle()'s hypotenuse in terms of a MovieClip's _rotation
property, where zero is north; optionally rounded.
Points.getPoint(point:Object, distance:Number, angle:Number):Object
Point in relation to given point at distance and angle provided.
---------------------------------------------------------------------------- */
//
import net.quip.MathAmp;
class net.quip.Points {
// PROPERTIES
//
// CONSTRUCTOR
public function Points () {
//
}
// METHODS
// Get Distance
public static function getDistance (pointA:Object, pointB:Object, bRound:Boolean):Number {
// Right angle point
var pointC:Object = new Object ();
pointC.x = pointA.x;
pointC.y = pointB.y;
// Pythagorean theorem (a^2 + b^2 = c^2)
var a:Number = Math.abs (pointC.y - pointA.y);
var b:Number = Math.abs (pointC.x - pointB.x);
// Hypotenuse
var c:Number = Math.sqrt (Math.pow (a, 2) + Math.pow (b, 2));
// Return length
if (bRound) {
return Math.round (c);
}
return c;
}
// Get Angle
public static function getAngle (pointA:Object, pointB:Object, bRound:Boolean):Number {
var radians:Number = Math.atan2 (pointB.y - pointA.y, pointB.x - pointA.x);
var degrees:Number = radians / (Math.PI / -180);
if (bRound) {
return Math.round (degrees);
}
return degrees;
}
// Get Rotation
public static function getRotation (pointA:Object, pointB:Object, bRound:Boolean):Number {
var rotation:Number;
var degrees:Number = getAngle (pointA, pointB);
if (degrees >= 0) {
rotation = (90 - degrees);
} else {
rotation = (Math.abs (degrees) + 90);
}
if (rotation < 0) {
rotation += 360;
}
if (bRound) {
return Math.round (rotation);
}
return rotation;
}
// Get Point
// http://community.borland.com/article/0,1410,17915,00.html
public static function getPoint (pointA:Object, distance:Number, angle:Number):Object {
var pointB:Object = new Object ();
var radians:Number = MathAmp.degreesToRadians (angle);
pointB.x = distance * Math.cos (radians) + pointA.x;
pointB.y = -(distance * Math.sin (radians) - pointA.y);
return pointB;
}
}