MathUtil (com.robertpenner.utils.MathUtil)

com.robertpenner.utils.MathUtil
class MathUtil

The MathUtil class includes a collection of trigonometry and coordinate system utilities.

Availability:

ActionScript 2.0; Flash Player 6

Property summary

Property Description
DEG2RAD:Number

Change degrees to radians

Math.PI/180

RAD2DEG:Number

Change radians to degrees

180/Math.PI

PHI:Number

The golden mean (phi)

(1+Math.sqrt(5))/2

LAMBDA:Number

Euler-Mascheroni constant (lambda or C)


		( n )
	lim	( sigma 1/k - ln(n) )
	n->oo	( k=1 )

Method summary

Method Description
distance(x1:Number, y1:Number, x2:Number, y2:Number) : Number

Finds the distance between two points.

distancePts(p1:Object, p2:Object) : Number

Finds the distance between two points.

angleOfLine(x1:Number, y1:Number, x2:Number, y2:Number) : Number

Finds the angle of the line formed between two points.

angleOfLinePts(p1:Object, p2:Object) : Number

Finds the angle of the line formed between two points.

fixAngle(angle:Number) : Number

Takes an angle in degrees and returns the equivalent standardized angle between 0 and 360 degrees.

degreesToRadians(angle:Number) : Number

Changes degrees to radians.

radiansToDegrees(angle:Number) : Number

Changes radians to degrees.

randRangeFloat(low:Number, high:Number) : Number

Returns a random floating-point number between two numbers.

randRangeInt(low:Number, high:Number) : Number

Returns a random integer between two numbers.

distance(x1:Number, y1:Number, x2:Number, y2:Number) : Number

Usage


var x1:Number = 10;
var y1:Number = 10;
var x2:Number = 20;
var y2:Number = 20;
trace (MathUtil.distance(x1, y1, x2, y2));

Parameters

x1:Number - The x value of the first point.

y1:Number - The y value of the first point.

x2:Number - The x value of the second point.

y2:Number - The y value of the second point.

Returns

Returns the distance between the two points.

Description

Finds the distance between two points.


distancePts(p1:Object, p2:Object) : Number

Usage


var p1:Object = {x:10, y:10};
var p2:Object = {x:20, y:20};
trace (MathUtil.distancePts(p1, p2));

Parameters

p1:Object - The first point, with x and y properties.

p2:Object - The second point, with x and y properties.

Returns

Returns the distance between the two points.

Description

Finds the distance between two points.


angleOfLine(x1:Number, y1:Number, x2:Number, y2:Number) : Number

Usage


var x1:Number = 10;
var y1:Number = 10;
var x2:Number = 20;
var y2:Number = 20;
trace (MathUtil.angleOfLine(x1, y1, x2, y2));

Parameters

x1:Number - The x value of the first point.

y1:Number - The y value of the first point.

x2:Number - The x value of the second point.

y2:Number - The y value of the second point.

Returns

Returns the angle of the line formed between the two points.

Description

Finds the angle of the line formed between two points.


angleOfLinePts(p1:Object, p2:Object) : Number

Usage


var p1:Object = {x:10, y:10};
var p2:Object = {x:20, y:20};
trace (MathUtil.angleOfLinePts(p1, p2));

Parameters

p1:Object - The first point, with x and y properties.

p2:Object - The second point, with x and y properties.

Returns

Returns the angle of the line formed between the two points.

Description

Finds the angle of the line formed between two points.


fixAngle(angle:Number) : Number

Usage


trace (MathUtil.fixAngle(740));
trace (MathUtil.fixAngle(-340));
trace (MathUtil.fixAngle(20));

Parameters

angle:Number - The angle below 0 or above 360.

Returns

Returns the equivalent standardized angle between 0 and 360 degrees.

Description

Takes an angle in degrees and returns the equivalent standardized angle between 0 and 360 degrees.


degreesToRadians(angle:Number) : Number

Usage


var angleDegrees:Number = 180;
var angleRadians:Number = MathUtil.degreesToRadians(angleDegrees);
trace (angleRadians);

Parameters

angle:Number - The angle in degrees.

Returns

Returns the angle in radians.

Description

Changes degrees to radians.


radiansToDegrees(angle:Number) : Number

Usage


trace (MathUtil.radiansToDegrees(Math.PI));

Parameters

angle:Number - The angle in radians.

Returns

Returns the angle in degrees.

Description

Changes radians to degrees.


randRangeFloat(low:Number, high:Number) : Number

Usage


trace (MathUtil.randRangeFloat(-.1, .1));

Parameters

low:Number - The low number of the random range.

high:Number - The high number of the random range.

Returns

Returns a random floating-point number between two numbers.

Description

Returns a random floating-point number between two numbers.


randRangeInt(low:Number, high:Number) : Number

Usage


trace (MathUtil.randRangeInt(-10, 10));

Parameters

low:Number - The low number of the random range.

high:Number - The high number of the random range.

Returns

Returns a random integer between two numbers.

Description

Returns a random integer between two numbers.


DEG2RAD : Number

Usage

trace (Constant.DEG2RAD * 180);

Description

Change degrees to radians

Math.PI/180


RAD2DEG : Number

Usage

trace (Constant.RAD2DEG * Math.PI);

Description

Change radians to degrees

180/Math.PI


PHI : Number

Usage

trace (Constant.PHI);

Description

The golden mean (phi)

(1+Math.sqrt(5))/2


LAMBDA : Number

Usage

trace (Constant.LAMBDA);

Description

Euler-Mascheroni constant (lambda or C)


		( n )
	lim	( sigma 1/k - ln(n) )
	n->oo	( k=1 )