| 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 : Floatx coordinate of vector.
Error on setter if:
Vec2 is immutable| y | property |
|
property y : Floaty coordinate of vector.
Error on setter if:
Vec2 is immutable| length | property |
|
property length : Floatlength of vector.
Error on setter if:
Vec2 is immutable| angle | property |
|
property angle : Floatangle of vector as measured counterclockwise from +x axis in radians.
Error on setter if:
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() : FloatComputes squared length of vector.
| set | method |
|
function set(p:const Vec2) : Vec2Copies values from given vector to this, returning the this vector.
Error if:
Vec2 is immutable| setxy | method |
|
function setxy(x:Float, y:Float) : Vec2Assigns values of this vector from arguments, returning the this vector.
Error if:
Vec2 is immutable| rotate | method |
|
function rotate(angle:Float) : Vec2Rotate the vector in-place returning the 'this' vector for chaining
Error if:
Vec2 is immutable| copy | method |
|
const function copy(?weak:Bool=false) : Vec2Produce a copy of this vector.
(Note: the copy of an immutable vector will now be mutable).
| dot | method |
|
const function dot(a:const Vec2) : FloatComputes dot product of the this vector and the given argument.
| cross | method |
|
const function cross(a:const Vec2) : FloatComputes 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) : Vec2Adds the given vector to this returning the result.
| sub | method |
|
const function sub(a:const Vec2, ?weak:Bool=false) : Vec2Subtracts the given vector to this returning the result.
| mul | method |
|
const function mul(s:Float, ?weak:Bool=false) : Vec2Multiplies the vector by given scalar returning the result.
| addeq | method |
|
function addeq(a:const Vec2) : Vec2Adds the given vector to this, modifying the this vector and returning the this vector. Equivalent to this.set(this.add(a))
Error if:
Vec2 is immutable| subeq | method |
|
function subeq(a:const Vec2) : Vec2Subtracts the given vector from this, modifying the this vector and returning the this vector. Equivalent to this.set(this.sub(a))
Error if:
Vec2 is immutable| muleq | method |
|
function muleq(s:Float) : Vec2Multiplies the vector by given scalar modifying the this vector and returning the this vector. Equivalent to this.set(this.mul(s))
Error if:
Vec2 is immutable
Static Method Detail
| get | static | method |
static function get(?x:Float=0, ?y:Float=0, ?weak:Bool=false) : Vec2Allocates 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) : Vec2Allocates 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) : Vec2Only available for flash9+ targets; construct Vec2 from Point object.
| fromPolar | static | method |
|
static function fromPolar(length:Float, angle:Float, ?weak:Bool=false) : Vec2Construct new vector from polar coordinates.
