Packageshared.math
Classpublic class Vec2
InheritanceVec2 Inheritance Vec2Const Inheritance Object

A "mutable" 2D Vector class.

Due to the lack of AS3 operator overloading most methods exists with different names.

All methods that end with "Self" actually modify the object itself (including obvious ones like set, setXY and zero). For example v1 += v2; is written as v1.addSelf(v2);

Notes:

Uses Vec2.Epsilon as max error in comparisons.

Original work by playchilla, slightly reworked by azrafe7.

License: Use it as you wish and if you like it - link back!

See also

http://www.playchilla.com/vector-2d-for-as3
https://github.com/azrafe7/vec2


Public Properties
 PropertyDefined By
  angle : Number
Vector angle in radians (can be assigned to)
Vec2
  length : Number
Length of the vector (can be assigned to)
Vec2
  stringDecimals : int = 4
[static] Number of decimal places to show when using toString() (set this to -1 for default Number -> String conversion)
Vec2
  x : Number
X component
Vec2
  y : Number
X component
Vec2
Public Methods
 MethodDefined By
  
Vec2(x:Number = 0, y:Number = 0)
Vec2
 Inherited
Adds "pos" vector (returns a new Vec2)
Vec2Const
  
Adds "pos" vector
Vec2
 Inherited
addXY(x:Number, y:Number):Vec2
Adds ("x", "y") (returns a new Vec2)
Vec2Const
  
addXYSelf(x:Number, y:Number):Vec2
Adds ("x", "y")
Vec2
 Inherited
clamp(maxLen:Number):Vec2
Returns a new Vec2 clamped to "maxLen" length
Vec2Const
 Inherited
clampInRect(rectangle:Rectangle):Vec2
Clamps this vector to fit in the specified "rectangle" (returns a new Vec2)
Vec2Const
  
clampInRectSelf(rectangle:Rectangle):Vec2
Clamps this vector to fit in the specified "rectangle"
Vec2
  
clampSelf(maxLen:Number):Vec2
Clamps this vector to "maxLen" length
Vec2
 Inherited
clampXY(minX:Number, maxX:Number, minY:Number, maxY:Number):Vec2
Clamps this vector to specified components (returns a new Vec2)
Vec2Const
  
clampXYSelf(minX:Number, maxX:Number, minY:Number, maxY:Number):Vec2
Clamps this vector to specified components
Vec2
 Inherited
Returns a new Vec2, replica of this instance
Vec2Const
 Inherited
crossDet(vec:Vec2Const):Number
Computes the cross product (determinant) with "vec"
Vec2Const
 Inherited
crossDetXY(x:Number, y:Number):Number
Computes the cross product (determinant) with ("x", "y"))
Vec2Const
 Inherited
distance(vec:Vec2Const):Number
Computes the distance from "vec" vector
Vec2Const
 Inherited
Computes the squared distance from "vec" vector
Vec2Const
 Inherited
distanceXY(x:Number, y:Number):Number
Computes the distance from ("x", "y")
Vec2Const
 Inherited
distanceXYSqr(x:Number, y:Number):Number
Computes the distance from ("x", "y")
Vec2Const
 Inherited
Divides by "vec" vector (returns a new Vec2)
Vec2Const
  
Divides by "vec" vector
Vec2
 Inherited
divXY(x:Number, y:Number):Vec2
Divides by ("x", "y") (returns a new Vec2)
Vec2Const
  
divXYSelf(x:Number, y:Number):Vec2
Divides by ("x", "y")
Vec2
 Inherited
dot(vec:Vec2Const):Number
Computes the dot product with "vec" vector
Vec2Const
 Inherited
dotXY(x:Number, y:Number):Number
Computes the dot product with "vec" vector
Vec2Const
 Inherited
equals(vec:Vec2Const):Boolean
Returns true if this vector's components equal the ones of "vec"
Vec2Const
 Inherited
equalsXY(x:Number, y:Number):Boolean
Returns true if this vector's components equal ("x", "y")
Vec2Const
 Inherited
Returns a new Vec2 with negated components
Vec2Const
  
Negates components
Vec2
  
[static] Creates a Vec2 from an AS3 Point (or any "p" object exposing x, y properties)
Vec2
  
fromPolar(rads:Number, length:Number):Vec2
[static] Sets components converting from polar coords (returns a new Vec2)
Vec2
  
fromPolarSelf(rads:Number, length:Number):Vec2
Sets components converting from polar coords
Vec2
 Inherited
getDegrees():Number
Vector angle in degrees
Vec2Const
 Inherited
Returns a new Vec2 which is the maximum between this vector and "vec" (component-wise)
Vec2Const
 Inherited
Returns a new Vec2 which is the minimum between this vector and "vec" (component-wise)
Vec2Const
 Inherited
getRads():Number
Vector angle in radians
Vec2Const
 Inherited
Angle between this vector and "vec" in radians
Vec2Const
 Inherited
isNear(vec2:Vec2Const):Boolean
Returns true if this vector is near "vec2"
Vec2Const
 Inherited
isNearXY(x:Number, y:Number):Boolean
Returns true if this vector is near ("x", "y")
Vec2Const
 Inherited
isNormalized():Boolean
Returns true if this vector is normalized (length == 1)
Vec2Const
 Inherited
isValid():Boolean
Returns true if is a valid vector (has finite components)
Vec2Const
 Inherited
isWithin(vec2:Vec2Const, epsilon:Number):Boolean
Returns true if the distance from "vec2" vector is lesser than "epsilon"
Vec2Const
 Inherited
isWithinXY(x:Number, y:Number, epsilon:Number):Boolean
Returns true if the distance from ("x", "y") vector is lesser than "epsilon"
Vec2Const
 Inherited
isZero():Boolean
Returns true if this vector's components are 0
Vec2Const
 Inherited
lengthSqr():Number
Computes the squared length of the vector
Vec2Const
 Inherited
lerp(to:Vec2Const, t:Number):Vec2
Linear interpolation from this vector to "to" vector (returns a new Vec2)
Vec2Const
  
lerpSelf(to:Vec2Const, t:Number):Vec2
Linear interpolation from this vector to "to" vector
Vec2
 Inherited
Multiplies by "vec" vector (returns a new Vec2)
Vec2Const
  
Multiplies by "vec" vector
Vec2
 Inherited
mulXY(x:Number, y:Number):Vec2
Multiplies by ("x", "y") (returns a new Vec2)
Vec2Const
  
mulXYSelf(x:Number, y:Number):Vec2
Multiplies by ("x", "y")
Vec2
 Inherited
normalize(length:Number = 1):Vec2
Normalizes the vector (returns a new Vec2)
Vec2Const
  
normalizeSelf(length:Number = 1):Vec2
Normalizes the vector
Vec2
 Inherited
Returns a new Vec2 left-perpendicular to this vector
Vec2Const
  
Sets components to be left-perpendicular to this vector
Vec2
 Inherited
Returns a new Vec2 right-perpendicular to this vector
Vec2Const
  
Sets components to be right-perpendicular to this vector
Vec2
 Inherited
Reflect this vector in plane whose normal is "normal" (returns a new Vec2)
Vec2Const
 Inherited
rotate(rads:Number):Vec2
Rotates by "rads" radians (returns a new Vec2)
Vec2Const
  
rotateSelf(rads:Number):Vec2
Rotates by "rads" radians
Vec2
 Inherited
Rotates using spinor "vec" (returns a new Vec2)
Vec2Const
  
Rotates using spinor "vec"
Vec2
 Inherited
scale(s:Number):Vec2
Scales by the scalar "s" (returns a new Vec2)
Vec2Const
  
scaleSelf(s:Number):Vec2
Scales by the scalar "s"
Vec2
  
set(p:*):Vec2
Copies x and y components from "p" (where "p" is any object exposing x, y properties)
Vec2
  
setXY(x:Number, y:Number):Vec2
Copies x and y components from passed arguments
Vec2
 Inherited
slerp(vec:Vec2Const, t:Number):Vec2
Spherical linear interpolation from this vector to "to" vector (returns a new Vec2) - not thoroughly tested Note: this vector and "vec" MUST be orthogonal for it to work properly
Vec2Const
 Inherited
Gets spinor between this vector and "vec" (returns a new Vec2)
Vec2Const
 Inherited
Subtracts "pos" vector (returns a new Vec2)
Vec2Const
  
Subtracts "pos" vector
Vec2
 Inherited
subXY(x:Number, y:Number):Vec2
Subtracts ("x", "y") (returns a new Vec2)
Vec2Const
  
subXYSelf(x:Number, y:Number):Vec2
Subtracts ("x", "y")
Vec2
  
swap(a:Vec2, b:Vec2):void
[static] Swaps vectors
Vec2
 Inherited
toPoint():Point
Creates an AS3 Point from this vector
Vec2Const
 Inherited
toString():String
String representation of this vector (uses Vec2Const.stringDecimals decimal positions)
Vec2Const
 Inherited
Returns a new Vec2 which is a unit for this vector (same as normalize(1))
Vec2Const
  
Sets x and y components to 0
Vec2
Public Constants
 ConstantDefined By
  Epsilon : Number = 0.0000001
[static] Epsilon (maximum error for comparisons)
Vec2
  EpsilonSqr : Number = 9.999999999999998E-15
[static] Epsilon squared
Vec2
  RAD2DEG : Number = 57.29577951308232
[static] Constant to convert from radians to degrees
Vec2
  Zero : Vec2Const
[static] Zero vector
Vec2
Property Detail
angleproperty
angle:Number

Vector angle in radians (can be assigned to)


Implementation
    public function get angle():Number
    public function set angle(value:Number):void
lengthproperty 
length:Number

Length of the vector (can be assigned to)


Implementation
    public function get length():Number
    public function set length(value:Number):void
stringDecimalsproperty 
public static var stringDecimals:int = 4

Number of decimal places to show when using toString() (set this to -1 for default Number -> String conversion)

xproperty 
x:Number

X component


Implementation
    public function get x():Number
    public function set x(value:Number):void
yproperty 
y:Number

X component


Implementation
    public function get y():Number
    public function set y(value:Number):void
Constructor Detail
Vec2()Constructor
public function Vec2(x:Number = 0, y:Number = 0)



Parameters
x:Number (default = 0)
 
y:Number (default = 0)
Method Detail
addSelf()method
public function addSelf(pos:Vec2Const):Vec2

Adds "pos" vector

Parameters

pos:Vec2Const

Returns
Vec2
addXYSelf()method 
public function addXYSelf(x:Number, y:Number):Vec2

Adds ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Vec2
clampInRectSelf()method 
public function clampInRectSelf(rectangle:Rectangle):Vec2

Clamps this vector to fit in the specified "rectangle"

Parameters

rectangle:Rectangle

Returns
Vec2
clampSelf()method 
public function clampSelf(maxLen:Number):Vec2

Clamps this vector to "maxLen" length

Parameters

maxLen:Number

Returns
Vec2
clampXYSelf()method 
public function clampXYSelf(minX:Number, maxX:Number, minY:Number, maxY:Number):Vec2

Clamps this vector to specified components

Parameters

minX:Number
 
maxX:Number
 
minY:Number
 
maxY:Number

Returns
Vec2
divSelf()method 
public function divSelf(vec:Vec2Const):Vec2

Divides by "vec" vector

Parameters

vec:Vec2Const

Returns
Vec2
divXYSelf()method 
public function divXYSelf(x:Number, y:Number):Vec2

Divides by ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Vec2
flipSelf()method 
public function flipSelf():Vec2

Negates components

Returns
Vec2
fromPoint()method 
public static function fromPoint(p:*):Vec2

Creates a Vec2 from an AS3 Point (or any "p" object exposing x, y properties)

Parameters

p:*

Returns
Vec2
fromPolar()method 
public static function fromPolar(rads:Number, length:Number):Vec2

Sets components converting from polar coords (returns a new Vec2)

Parameters

rads:Number
 
length:Number

Returns
Vec2
fromPolarSelf()method 
public function fromPolarSelf(rads:Number, length:Number):Vec2

Sets components converting from polar coords

Parameters

rads:Number
 
length:Number

Returns
Vec2
lerpSelf()method 
public function lerpSelf(to:Vec2Const, t:Number):Vec2

Linear interpolation from this vector to "to" vector

Parameters

to:Vec2Const
 
t:Number

Returns
Vec2
mulSelf()method 
public function mulSelf(vec:Vec2Const):Vec2

Multiplies by "vec" vector

Parameters

vec:Vec2Const

Returns
Vec2
mulXYSelf()method 
public function mulXYSelf(x:Number, y:Number):Vec2

Multiplies by ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Vec2
normalizeSelf()method 
public function normalizeSelf(length:Number = 1):Vec2

Normalizes the vector

Parameters

length:Number (default = 1)

Returns
Vec2
perpLeftSelf()method 
public function perpLeftSelf():Vec2

Sets components to be left-perpendicular to this vector

Returns
Vec2
perpRightSelf()method 
public function perpRightSelf():Vec2

Sets components to be right-perpendicular to this vector

Returns
Vec2
rotateSelf()method 
public function rotateSelf(rads:Number):Vec2

Rotates by "rads" radians

Parameters

rads:Number

Returns
Vec2
rotateSpinorSelf()method 
public function rotateSpinorSelf(vec:Vec2Const):Vec2

Rotates using spinor "vec"

Parameters

vec:Vec2Const

Returns
Vec2
scaleSelf()method 
public function scaleSelf(s:Number):Vec2

Scales by the scalar "s"

Parameters

s:Number

Returns
Vec2
set()method 
public function set(p:*):Vec2

Copies x and y components from "p" (where "p" is any object exposing x, y properties)

Parameters

p:*

Returns
Vec2
setXY()method 
public function setXY(x:Number, y:Number):Vec2

Copies x and y components from passed arguments

Parameters

x:Number
 
y:Number

Returns
Vec2
subSelf()method 
public function subSelf(pos:Vec2Const):Vec2

Subtracts "pos" vector

Parameters

pos:Vec2Const

Returns
Vec2
subXYSelf()method 
public function subXYSelf(x:Number, y:Number):Vec2

Subtracts ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Vec2
swap()method 
public static function swap(a:Vec2, b:Vec2):void

Swaps vectors

Parameters

a:Vec2
 
b:Vec2

zero()method 
public function zero():Vec2

Sets x and y components to 0

Returns
Vec2
Constant Detail
EpsilonConstant
public static const Epsilon:Number = 0.0000001

Epsilon (maximum error for comparisons)

EpsilonSqrConstant 
public static const EpsilonSqr:Number = 9.999999999999998E-15

Epsilon squared

RAD2DEGConstant 
public static const RAD2DEG:Number = 57.29577951308232

Constant to convert from radians to degrees

ZeroConstant 
public static const Zero:Vec2Const

Zero vector