Packageshared.math
Classpublic class Vec2Const
InheritanceVec2Const Inheritance Object
Subclasses Vec2

A 2D Vector class to perform constant operations.

Use this class to make sure that objects stay const, e.g.: public function getPos():Vec2Const { return _pos; } // _pos is not allowed to change outside (cannot assing to its x and y)

Many methods have a postfix "XY" - this allows you to operate on the components directly i.e. instead of writing add(new Vec2(1, 2)) you can directly write addXY(1, 2);

For performance reasons I am not using an interface for read only specification since internally it should be possible to use direct access to x and y. Externally x and y are obtained via getters which are a bit slower than direct access of a public variable. I suggest you stick with this during development. If there is a bottleneck you can just remove the get accessors and directly expose _x and _y (rename it to x and replace all _x and _y to this.x, this.y internally).

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
[read-only] Vector angle in radians
Vec2Const
  length : Number
[read-only] Computes the length of the vector
Vec2Const
  x : Number
[read-only] X component (read-only)
Vec2Const
  y : Number
[read-only] Y component (read-only)
Vec2Const
Public Methods
 MethodDefined By
  
Vec2Const(x:Number = 0, y:Number = 0)
Vec2Const
  
Adds "pos" vector (returns a new Vec2)
Vec2Const
  
addXY(x:Number, y:Number):Vec2
Adds ("x", "y") (returns a new Vec2)
Vec2Const
  
clamp(maxLen:Number):Vec2
Returns a new Vec2 clamped to "maxLen" length
Vec2Const
  
clampInRect(rectangle:Rectangle):Vec2
Clamps this vector to fit in the specified "rectangle" (returns a new Vec2)
Vec2Const
  
clampXY(minX:Number, maxX:Number, minY:Number, maxY:Number):Vec2
Clamps this vector to specified components (returns a new Vec2)
Vec2Const
  
Returns a new Vec2, replica of this instance
Vec2Const
  
crossDet(vec:Vec2Const):Number
Computes the cross product (determinant) with "vec"
Vec2Const
  
crossDetXY(x:Number, y:Number):Number
Computes the cross product (determinant) with ("x", "y"))
Vec2Const
  
distance(vec:Vec2Const):Number
Computes the distance from "vec" vector
Vec2Const
  
Computes the squared distance from "vec" vector
Vec2Const
  
distanceXY(x:Number, y:Number):Number
Computes the distance from ("x", "y")
Vec2Const
  
distanceXYSqr(x:Number, y:Number):Number
Computes the distance from ("x", "y")
Vec2Const
  
Divides by "vec" vector (returns a new Vec2)
Vec2Const
  
divXY(x:Number, y:Number):Vec2
Divides by ("x", "y") (returns a new Vec2)
Vec2Const
  
dot(vec:Vec2Const):Number
Computes the dot product with "vec" vector
Vec2Const
  
dotXY(x:Number, y:Number):Number
Computes the dot product with "vec" vector
Vec2Const
  
equals(vec:Vec2Const):Boolean
Returns true if this vector's components equal the ones of "vec"
Vec2Const
  
equalsXY(x:Number, y:Number):Boolean
Returns true if this vector's components equal ("x", "y")
Vec2Const
  
Returns a new Vec2 with negated components
Vec2Const
  
getDegrees():Number
Vector angle in degrees
Vec2Const
  
Returns a new Vec2 which is the maximum between this vector and "vec" (component-wise)
Vec2Const
  
Returns a new Vec2 which is the minimum between this vector and "vec" (component-wise)
Vec2Const
  
getRads():Number
Vector angle in radians
Vec2Const
  
Angle between this vector and "vec" in radians
Vec2Const
  
isNear(vec2:Vec2Const):Boolean
Returns true if this vector is near "vec2"
Vec2Const
  
isNearXY(x:Number, y:Number):Boolean
Returns true if this vector is near ("x", "y")
Vec2Const
  
isNormalized():Boolean
Returns true if this vector is normalized (length == 1)
Vec2Const
  
isValid():Boolean
Returns true if is a valid vector (has finite components)
Vec2Const
  
isWithin(vec2:Vec2Const, epsilon:Number):Boolean
Returns true if the distance from "vec2" vector is lesser than "epsilon"
Vec2Const
  
isWithinXY(x:Number, y:Number, epsilon:Number):Boolean
Returns true if the distance from ("x", "y") vector is lesser than "epsilon"
Vec2Const
  
isZero():Boolean
Returns true if this vector's components are 0
Vec2Const
  
lengthSqr():Number
Computes the squared length of the vector
Vec2Const
  
lerp(to:Vec2Const, t:Number):Vec2
Linear interpolation from this vector to "to" vector (returns a new Vec2)
Vec2Const
  
Multiplies by "vec" vector (returns a new Vec2)
Vec2Const
  
mulXY(x:Number, y:Number):Vec2
Multiplies by ("x", "y") (returns a new Vec2)
Vec2Const
  
normalize(length:Number = 1):Vec2
Normalizes the vector (returns a new Vec2)
Vec2Const
  
Returns a new Vec2 left-perpendicular to this vector
Vec2Const
  
Returns a new Vec2 right-perpendicular to this vector
Vec2Const
  
Reflect this vector in plane whose normal is "normal" (returns a new Vec2)
Vec2Const
  
rotate(rads:Number):Vec2
Rotates by "rads" radians (returns a new Vec2)
Vec2Const
  
Rotates using spinor "vec" (returns a new Vec2)
Vec2Const
  
scale(s:Number):Vec2
Scales by the scalar "s" (returns a new Vec2)
Vec2Const
  
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
  
Gets spinor between this vector and "vec" (returns a new Vec2)
Vec2Const
  
Subtracts "pos" vector (returns a new Vec2)
Vec2Const
  
subXY(x:Number, y:Number):Vec2
Subtracts ("x", "y") (returns a new Vec2)
Vec2Const
  
toPoint():Point
Creates an AS3 Point from this vector
Vec2Const
  
toString():String
String representation of this vector (uses Vec2Const.stringDecimals decimal positions)
Vec2Const
  
Returns a new Vec2 which is a unit for this vector (same as normalize(1))
Vec2Const
Property Detail
angleproperty
angle:Number  [read-only]

Vector angle in radians


Implementation
    public function get angle():Number
lengthproperty 
length:Number  [read-only]

Computes the length of the vector


Implementation
    public function get length():Number
xproperty 
x:Number  [read-only]

X component (read-only)


Implementation
    public function get x():Number
yproperty 
y:Number  [read-only]

Y component (read-only)


Implementation
    public function get y():Number
Constructor Detail
Vec2Const()Constructor
public function Vec2Const(x:Number = 0, y:Number = 0)



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

Adds "pos" vector (returns a new Vec2)

Parameters

pos:Vec2Const

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

Adds ("x", "y") (returns a new Vec2)

Parameters

x:Number
 
y:Number

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

Returns a new Vec2 clamped to "maxLen" length

Parameters

maxLen:Number

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

Clamps this vector to fit in the specified "rectangle" (returns a new Vec2)

Parameters

rectangle:Rectangle

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

Clamps this vector to specified components (returns a new Vec2)

Parameters

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

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

Returns a new Vec2, replica of this instance

Returns
Vec2
crossDet()method 
public function crossDet(vec:Vec2Const):Number

Computes the cross product (determinant) with "vec"

Parameters

vec:Vec2Const

Returns
Number
crossDetXY()method 
public function crossDetXY(x:Number, y:Number):Number

Computes the cross product (determinant) with ("x", "y"))

Parameters

x:Number
 
y:Number

Returns
Number
distance()method 
public function distance(vec:Vec2Const):Number

Computes the distance from "vec" vector

Parameters

vec:Vec2Const

Returns
Number
distanceSqr()method 
public function distanceSqr(vec:Vec2Const):Number

Computes the squared distance from "vec" vector

Parameters

vec:Vec2Const

Returns
Number
distanceXY()method 
public function distanceXY(x:Number, y:Number):Number

Computes the distance from ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Number
distanceXYSqr()method 
public function distanceXYSqr(x:Number, y:Number):Number

Computes the distance from ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Number
div()method 
public function div(vec:Vec2Const):Vec2

Divides by "vec" vector (returns a new Vec2)

Parameters

vec:Vec2Const

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

Divides by ("x", "y") (returns a new Vec2)

Parameters

x:Number
 
y:Number

Returns
Vec2
dot()method 
public function dot(vec:Vec2Const):Number

Computes the dot product with "vec" vector

Parameters

vec:Vec2Const

Returns
Number
dotXY()method 
public function dotXY(x:Number, y:Number):Number

Computes the dot product with "vec" vector

Parameters

x:Number
 
y:Number

Returns
Number
equals()method 
public function equals(vec:Vec2Const):Boolean

Returns true if this vector's components equal the ones of "vec"

Parameters

vec:Vec2Const

Returns
Boolean
equalsXY()method 
public function equalsXY(x:Number, y:Number):Boolean

Returns true if this vector's components equal ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Boolean
flip()method 
public function flip():Vec2

Returns a new Vec2 with negated components

Returns
Vec2
getDegrees()method 
public function getDegrees():Number

Vector angle in degrees

Returns
Number
getMax()method 
public function getMax(vec:Vec2Const):Vec2

Returns a new Vec2 which is the maximum between this vector and "vec" (component-wise)

Parameters

vec:Vec2Const

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

Returns a new Vec2 which is the minimum between this vector and "vec" (component-wise)

Parameters

vec:Vec2Const

Returns
Vec2
getRads()method 
public function getRads():Number

Vector angle in radians

Returns
Number
getRadsBetween()method 
public function getRadsBetween(vec:Vec2Const):Number

Angle between this vector and "vec" in radians

Parameters

vec:Vec2Const

Returns
Number
isNear()method 
public function isNear(vec2:Vec2Const):Boolean

Returns true if this vector is near "vec2"

Parameters

vec2:Vec2Const

Returns
Boolean
isNearXY()method 
public function isNearXY(x:Number, y:Number):Boolean

Returns true if this vector is near ("x", "y")

Parameters

x:Number
 
y:Number

Returns
Boolean
isNormalized()method 
public function isNormalized():Boolean

Returns true if this vector is normalized (length == 1)

Returns
Boolean
isValid()method 
public function isValid():Boolean

Returns true if is a valid vector (has finite components)

Returns
Boolean
isWithin()method 
public function isWithin(vec2:Vec2Const, epsilon:Number):Boolean

Returns true if the distance from "vec2" vector is lesser than "epsilon"

Parameters

vec2:Vec2Const
 
epsilon:Number

Returns
Boolean
isWithinXY()method 
public function isWithinXY(x:Number, y:Number, epsilon:Number):Boolean

Returns true if the distance from ("x", "y") vector is lesser than "epsilon"

Parameters

x:Number
 
y:Number
 
epsilon:Number

Returns
Boolean
isZero()method 
public function isZero():Boolean

Returns true if this vector's components are 0

Returns
Boolean
lengthSqr()method 
public function lengthSqr():Number

Computes the squared length of the vector

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

Linear interpolation from this vector to "to" vector (returns a new Vec2)

Parameters

to:Vec2Const
 
t:Number

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

Multiplies by "vec" vector (returns a new Vec2)

Parameters

vec:Vec2Const

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

Multiplies by ("x", "y") (returns a new Vec2)

Parameters

x:Number
 
y:Number

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

Normalizes the vector (returns a new Vec2)

Parameters

length:Number (default = 1)

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

Returns a new Vec2 left-perpendicular to this vector

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

Returns a new Vec2 right-perpendicular to this vector

Returns
Vec2
reflect()method 
public function reflect(normal:Vec2Const):Vec2

Reflect this vector in plane whose normal is "normal" (returns a new Vec2)

Parameters

normal:Vec2Const

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

Rotates by "rads" radians (returns a new Vec2)

Parameters

rads:Number

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

Rotates using spinor "vec" (returns a new Vec2)

Parameters

vec:Vec2Const

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

Scales by the scalar "s" (returns a new Vec2)

Parameters

s:Number

Returns
Vec2
slerp()method 
public function 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

Parameters

vec:Vec2Const
 
t:Number

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

Gets spinor between this vector and "vec" (returns a new Vec2)

Parameters

vec:Vec2Const

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

Subtracts "pos" vector (returns a new Vec2)

Parameters

pos:Vec2Const

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

Subtracts ("x", "y") (returns a new Vec2)

Parameters

x:Number
 
y:Number

Returns
Vec2
toPoint()method 
public function toPoint():Point

Creates an AS3 Point from this vector

Returns
Point
toString()method 
public function toString():String

String representation of this vector (uses Vec2Const.stringDecimals decimal positions)

Returns
String
unit()method 
public function unit():Vec2

Returns a new Vec2 which is a unit for this vector (same as normalize(1))

Returns
Vec2