Donate Donating to nape helps push me to keep on making it better!
Index | Donators | Forums | Project Page
Loading
nape.geom.Vec2 class
Top | Properties | Constructor | Methods | Statics

Package nape.geom
Class Vec2

2D Vector class.

Note: In many cases a Vec2 will be returned from a function internal to nape and will be marked internally as an 'immutable' Vec2. This is documented in those cases; trying to modify (mutate) such a Vec2 will result in an error.

Trying to use a disposed Vec2 will also result in an error, the pool works in a FILO order so as to make catching the use of a disposed Vec2 bug easier

Weak Vec2's are so-named because when given to any nape function, they will be automaticaly disposed of afterwards (Makes re-using Vec2's very easy, but be careful :P). Occasionaly it is the case that this will not occur, in which case it will be noted. The main exception is with lists, pushing a weak Vec2 to a list does not dispose it, but instead adds it as usual to the list. Then for example the Polygon constructor which takes a list of Vec2, any weak Vec2's in this list will be disposed of and removed from the list.



Public Properties
Property Defined By
x : Float
x value
Vec2
y : Float
y value
Vec2
length : Float
length
Vec2
angle : Float
angle
Vec2

Constructor
Constructor
new(?x:Float=0, ?y:Float=0)
Construct a new vector. Use of 'get' is preferred.

Public Methods
Method Defined By
dispose()
Release vector to the object pool.
Vec2
const lsq() : Float
Squared length of vector.
Vec2

 
set(p:const Vec2) : Vec2
Set values from vector
Vec2
setxy(x:Float, y:Float) : Vec2
Set values from coordinate pair
Vec2
rotate(angle:Float) : Vec2
Rotate vector in-place
Vec2

 
const copy(?weak:Bool=false) : Vec2
Produce a copy of this vector.
Vec2

 
const dot(a:const Vec2) : Float
Compute dot product.
Vec2
const cross(a:const Vec2) : Float
Compute cross product.
Vec2

 
const add(a:const Vec2, ?weak:Bool=false) : Vec2
Add vectors together
Vec2
const sub(a:const Vec2, ?weak:Bool=false) : Vec2
Subtract vectors together
Vec2
const mul(s:Float, ?weak:Bool=false) : Vec2
Multiply vector by scalar
Vec2
addeq(a:const Vec2) : Vec2
Add vector accumulatively
Vec2
subeq(a:const Vec2) : Vec2
Subtract vector accumulatively
Vec2
muleq(s:Float) : Vec2
Multiply vector by scalar
Vec2

Public Static Methods
Method Defined By
static get(?x:Float=0, ?y:Float=0, ?weak:Bool=false) : Vec2
Allocate a vector from the object pool; or failing that, construct a new one.
Vec2
static weak(?x:Float=0, ?y:Float=0) : Vec2
Allocate a 'weak' vector from the object pool; or failing that, construct a new one.
Vec2
static fromPoint(point:const flash.geom.Point, ?weak:Bool=false) : Vec2
(flash9+) Constructor vector from Point.
Vec2
static fromPolar(length:Float, angle:Float, ?weak:Bool=false) : Vec2
Construct Vec2 from polar coordinates.
Vec2

Property Detail
x property
property x : Float

x coordinate of vector.

Error on setter if:
X   Vec2 is immutable


y property
property y : Float

y coordinate of vector.

Error on setter if:
X   Vec2 is immutable


length property
property length : Float

length of vector.

Error on setter if:
X   Vec2 is immutable


angle property
property angle : Float

angle of vector as measured counterclockwise from +x axis in radians.

Error on setter if:
X   Vec2 is immutable


Constructor Detail
function new(?x:Float=0, ?y:Float=0)

Construct a new vector from given cartesian values. Using the static get method is preferred as it will make use of any released Vec2's in the object pool whereas the constructor will allocate a new Vec2 from the heap whether there is anything in the object pool or not.


Method Detail
dispose method
function dispose()

Releases the vector to the object pool leaving it available for re-use by the static 'get' method and nape internals. when disposing a Vec2 you should treat it the same as if you were wanting it to be collected by the garbage collector and have no remaining references to it; this isn't necessary but if you accidentaly use a released Vec2 and it is not caught (by debug mode errors) then incorrect behaviour may result


lsq method
const function lsq() : Float

Computes squared length of vector.


set method
function set(p:const Vec2) : Vec2

Copies values from given vector to this, returning the this vector.

Error if:
X   Vec2 is immutable


setxy method
function setxy(x:Float, y:Float) : Vec2

Assigns values of this vector from arguments, returning the this vector.

Error if:
X   Vec2 is immutable


rotate method
function rotate(angle:Float) : Vec2

Rotate the vector in-place returning the 'this' vector for chaining

Error if:
X   Vec2 is immutable


copy method
const function copy(?weak:Bool=false) : Vec2

Produce a copy of this vector.
(Note: the copy of an immutable vector will now be mutable).


dot method
const function dot(a:const Vec2) : Float

Computes dot product of the this vector and the given argument.


cross method
const function cross(a:const Vec2) : Float

Computes the cross product between the this vector and given argument. Strictly speaking the cross product is not defined on 2D vectors; this is defined as the perp-dot product chosen to be the z-coordinate in the 3D cross product between the equivalent 3D vectors, or equivalently the determinant of the direct product. This satifies all the normal rules of the cross product that apply.


add method
const function add(a:const Vec2, ?weak:Bool=false) : Vec2

Adds the given vector to this returning the result.


sub method
const function sub(a:const Vec2, ?weak:Bool=false) : Vec2

Subtracts the given vector to this returning the result.


mul method
const function mul(s:Float, ?weak:Bool=false) : Vec2

Multiplies the vector by given scalar returning the result.


addeq method
function addeq(a:const Vec2) : Vec2

Adds the given vector to this, modifying the this vector and returning the this vector. Equivalent to this.set(this.add(a))

Error if:
X   Vec2 is immutable


subeq method
function subeq(a:const Vec2) : Vec2

Subtracts the given vector from this, modifying the this vector and returning the this vector. Equivalent to this.set(this.sub(a))

Error if:
X   Vec2 is immutable


muleq method
function muleq(s:Float) : Vec2

Multiplies the vector by given scalar modifying the this vector and returning the this vector. Equivalent to this.set(this.mul(s))

Error if:
X   Vec2 is immutable


Static Method Detail
get static method
static function get(?x:Float=0, ?y:Float=0, ?weak:Bool=false) : Vec2

Allocates a vector with given cartesian values from the object pool. If the object pool is empty then a new vector will be constructed instead. Use of this method is preferable to using the constructor.


weak static method
static function weak(?x:Float=0, ?y:Float=0) : Vec2

Allocates a 'weak' vector with given cartesian values from the object pool. If the object pool is empty then a new vector will be constructed instead. Use of this method is preferable to using the constructor.


fromPoint static method
static function fromPoint(point:const flash.geom.Point, ?weak:Bool=false) : Vec2

Only available for flash9+ targets; construct Vec2 from Point object.


fromPolar static method
static function fromPolar(length:Float, angle:Float, ?weak:Bool=false) : Vec2

Construct new vector from polar coordinates.