Packagegraphics.geom
Classpublic class Vector2
InheritanceVector2 Inheritance flash.geom.Point
Implements Serializable

Represents a vector in a 2D world with the coordinates x and y.



Public Properties
 PropertyDefined By
  angle : Number
Indicates the angle value of the specified Vector2 object.
Vector2
  degrees : Boolean
Indicates if all calcul in this methods use angles in degrees or radians.
Vector2
  ZERO : Vector2
[static] Defines the Vector2 object with the x and y properties set to zero.
Vector2
Public Methods
 MethodDefined By
  
Vector2(x:Number = 0, y:Number = 0, degrees:Boolean = false)
Creates a new Vector2 instance.
Vector2
  
abs():void
Transforms the coordinates of the vector to used absolute value for the x and y properties.
Vector2
  
[static] Returns a new Vector2 reference with the absolute value of the coordinates of the specified Vector2 object.
Vector2
  
add(v1:Point, v2:Point):Vector2
[static] Computes the addition of two Vector2.
Vector2
  
angleBetween(vector:Vector2):Number
Returns the angle value between this Vector2 object and the specified Vector2 passed in arguments.
Vector2
  
clone():Point
[override] Returns a shallow copy of this instance.
Vector2
  
cross(vector:Vector2):Number
Returns the cross value of the two Vector2.
Vector2
  
Returns the direction of this Vector2 object.
Vector2
  
distance(v1:Vector2, v2:Vector2):Number
[static] Returns the distance between the 2 vectors.
Vector2
  
dot(vector:Vector2):Number
Returns the dot value of the passed-in Vector2 with the current Vector2 object.
Vector2
  
equals(toCompare:Point):Boolean
[override] Compares the specified object with this object for equality.
Vector2
  
interpolate(vector:Point, level:Number):Vector2
Determines a point between two specified points.
Vector2
  
invert():void
Invert the current Vector2 object.
Vector2
  
isPerpendicularTo(vector:Vector2):Boolean
Returns true if the Vector2 is perpendicular with the passed-in Vector2.
Vector2
  
Returns the new Vector2 with the maximum horizontal coordinate and the maximum vertical coordinate.
Vector2
  
Returns the middle Vector2 object between 2 Points.
Vector2
  
Returns a new Vector2 with the minimum horizontal coordinate and the minimize vertical coordinate.
Vector2
  
negate():void
Sets this Vector2 with negate coordinates.
Vector2
  
[static] Returns the new negate Vector2 of the specified Vector2 objet.
Vector2
  
Returns the normal value of this Vector2.
Vector2
  
polar(length:Number, angle:Number):Vector2
[static] Converts a pair of polar coordinates to a Cartesian point coordinate.
Vector2
  
pow(value:Number):void
Computes the power of the vector.
Vector2
  
pow(vector:Vector2, value:Number):Vector2
[static] Computes the power of the specified Vector2.
Vector2
  
Returns the projection of a Vector2 with the specified Vector2 passed in argument.
Vector2
  
Returns the size of the projection of this Point with an other Point.
Vector2
  
reflect(normal:Vector2):void
Reflects the current Vector2 object with the passed-in normal Vector2 argument.
Vector2
  
reflect(vector:Vector2, normal:Vector2):Vector2
[static] Creates a new Vector2 object, the reflect of the specific vector.
Vector2
  
rotate(angle:Number):void
Rotates the Vector2 object with the specified angle passed-in argument.
Vector2
  
rotate(vector:Vector2, angle:Number):Vector2
[static] Rotates the Vector2 with the specified angle in argument and creates a new Vector2.
Vector2
  
scale(value:Number):void
Scales the vector object with the input value.
Vector2
  
scale(v:Vector2, value:Number):Vector2
[static] Scales a new Vector2 object with the specified Vector2.
Vector2
  
set(vector:Vector2):void
Sets the current Vector2 with the passed-in Vector2 argument.
Vector2
  
setByObject(o:Object):void
Sets the current Vector2 object with the passed-in Object argument.
Vector2
  
setByPoint(p:Point):void
Sets the current Vector2 object with the passed-in flash.geom.Point argument.
Vector2
  
[static] Returns the squared distance between 2 vectors.
Vector2
  
Computes the substraction of the current vector object with an other.
Vector2
  
[static] Computes the substraction of two Vector2.
Vector2
  
swap(vector:Vector2):void
Swap the horizontal and vertical coordinates of two Vector2 objects.
Vector2
  
toObject():Object
Returns the Object representation of this object.
Vector2
  
toSource(indent:int = 0):String
Returns the source code string representation of the object.
Vector2
  
toString():String
[override] Returns the string representation of the object.
Vector2
Public Constants
 ConstantDefined By
  DOWN : Vector2
[static] Indicates the relative "down" direction.
Vector2
  LEFT : Vector2
[static] Indicates the relative "left" direction.
Vector2
  RIGHT : Vector2
[static] Indicates the relative "right" direction.
Vector2
  UP : Vector2
[static] Indicates the relative "up" direction.
Vector2
Property Detail
angleproperty
angle:Number

Indicates the angle value of the specified Vector2 object.


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

See also

boolean property to set if the value is in degrees or radians.
degreesproperty 
public var degrees:Boolean

Indicates if all calcul in this methods use angles in degrees or radians.

ZEROproperty 
public static var ZERO:Vector2

Defines the Vector2 object with the x and y properties set to zero.

Constructor Detail
Vector2()Constructor
public function Vector2(x:Number = 0, y:Number = 0, degrees:Boolean = false)

Creates a new Vector2 instance.

Parameters
x:Number (default = 0) — the x coordinate.
 
y:Number (default = 0) — the y coordinate.
 
degrees:Boolean (default = false) — Optional flag who indicates if all methods use angles in degrees or radians.
Method Detail
abs()method
public function abs():void

Transforms the coordinates of the vector to used absolute value for the x and y properties.

Example :

         import graphics.geom.Vector2 ;
         
         var v:Vector2 = new Vector2(-10, -20) ;
         
         v.abs() ;
         
         trace( v ) ; // [Vector2 x:10 y:20]
         

abs()method 
public static function abs(vector:Vector2):Vector2

Returns a new Vector2 reference with the absolute value of the coordinates of the specified Vector2 object.

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2(-10, -20) ;
         var v2:Vector2 = Vector2.abs( v1 ) ;
         
         trace( v1 + " / " + v2 ) ; // [Vector2 x:-10 y:-20] / [Vector2 x:10 y:20]
         

Parameters

vector:Vector2 — The vector reference to create the new absolute Vector2 object.

Returns
Vector2 — a new Vector2 reference with the absolute value of the coordinates of the passed-in Vector2 object.
add()method 
public static function add(v1:Point, v2:Point):Vector2

Computes the addition of two Vector2.

Parameters

v1:Point — a Vector2 to concat.
 
v2:Point — a Vector2 to concat.

Returns
Vector2 — the addition result of two Vector2.
angleBetween()method 
public function angleBetween(vector:Vector2):Number

Returns the angle value between this Vector2 object and the specified Vector2 passed in arguments.

Example :

         import graphics.geom.Vector2 ;
         
         var angle:Number ;
         
         var v1:Vector2 = new Vector2(10, 20) ;
         var v2:Vector2 = new Vector2(50, 200) ;
         
         v1.degrees = false ;
         angle      = v1.angleBetween(v2) ;
         
         trace( angle ) ;
         
         v1.degrees = true ;
         angle      = v1.angleBetween(v2) ;
         
         trace( angle ) ;
         

Parameters

vector:Vector2

Returns
Number — the angle value between this Point object and the specified Point passed in arguments.
clone()method 
override public function clone():Point

Returns a shallow copy of this instance.

Returns
Point — a shallow copy of this instance.
cross()method 
public function cross(vector:Vector2):Number

Returns the cross value of the two Vector2.

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2( 10 , 20 ) ;
         var v2:Vector2 = new Vector2( 40 , 60 ) ;
         
         trace( v1.cross( v2 ) ) ; // -200
         

Parameters

vector:Vector2 — The Vector2 object use to calculate the cross value.

Returns
Number — The cross value of the current Vector2 object with the Vector2 passed in argument.
direction()method 
public function direction():Vector2

Returns the direction of this Vector2 object.

Example :

         import graphics.geom.Vector2 ;
         var v1 : Vector2 = new Point(10,2);
         var v2 : Vector2 = p1.direction();
         trace( v2 ) ; 
         

Returns
Vector2 — the direction of this Point.

See also

Vector2.normalize
distance()method 
public static function distance(v1:Vector2, v2:Vector2):Number

Returns the distance between the 2 vectors.

Parameters

v1:Vector2 — the first vector.
 
v2:Vector2 — the second vector.

Returns
Number — the distance between the 2 vectors.
dot()method 
public function dot(vector:Vector2):Number

Returns the dot value of the passed-in Vector2 with the current Vector2 object.

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2(10,20) ;
         var v2:Vector2 = new Vector2(40,60) ;
         
         trace( v1.dot( v2 ) ) ; // 1600
         

Parameters

vector:Vector2 — the Vector2 object to calculate the dot value.

Returns
Number — the dot value.
equals()method 
override public function equals(toCompare:Point):Boolean

Compares the specified object with this object for equality.

Parameters

toCompare:Point

Returns
Booleantrue if the the specified object is equal with this object.
interpolate()method 
public function interpolate(vector:Point, level:Number):Vector2

Determines a point between two specified points. The parameter 'level' determines where the new interpolated point is located relative to the two end points. The closer the value of the parameter level is to 1.0, the closer the interpolated point is to the first vector (current Vector2). The closer the value of the parameter level is to 0, the closer the interpolated point is to the second vector (parameter vector).

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2(10,10) ;
         var v2:Vector2 = new Vector2(40,40) ;
         
         var v3:Vector2 ;
         
         v3 = v1.interpolate( v2 , 0 ) ;
         trace(v3) ; // [Vector2 x:40 y:40]
         
         v3 = v1.interpolate( v2 , 1 ) ;
         trace(v3) ; // [Vector2 x:10 y:10]
         
         v3 = v1.interpolate( v2 , 0.5 ) ;
         trace(v3) ; // [Vector2 x:25 y:25]
         

Parameters

vector:Point — The first point.
 
level:Number — The second Point.

Returns
Vector2 — The new interpolated Vector2 object.
invert()method 
public function invert():void

Invert the current Vector2 object.

Example :

         import graphics.geom.Vector2 ;
         
         var vector:Vector2 = new Vector2(10,20) ;
         
         vector.invert() ;
         
         trace( vector ) ;
         

isPerpendicularTo()method 
public function isPerpendicularTo(vector:Vector2):Boolean

Returns true if the Vector2 is perpendicular with the passed-in Vector2.

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Point = new Vector2(  0 , 10 ) ;
         var v2:Point = new Vector2( 10 , 10 ) ;
         var v3:Point = new Vector2( 10 ,  0 ) ;
         
         trace( v1.isPerpendicularTo( v2 ) ) ; // false
         trace( v1.isPerpendicularTo( v3 ) ) ; // true
         

Parameters

vector:Vector2 — the Point use to determinate if this Point object is perpendicular.

Returns
Booleantrue if the Point is perpendicular with the passed-in Point.
max()method 
public function max(vector:Vector2):Vector2

Returns the new Vector2 with the maximum horizontal coordinate and the maximum vertical coordinate.

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2(10,100) ;
         var v2:Vector2 = new Vector2(100,10) ;
         
         var v3:Vector2 = v1.max( v2 ) ;
         trace( v3 ) ; // [Vector2 x:100 y:100]
         

Parameters

vector:Vector2 — The Vector2 passed in this method

Returns
Vector2 — The new Vector2 with the maximum horizontal coordinate and the maximum vertical coordinate.
middle()method 
public function middle(vector:Vector2):Vector2

Returns the middle Vector2 object between 2 Points.

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2(10,10) ;
         var v2:Vector2 = new Vector2(20,20) ;
         
         var middle:Vector2 = v1.middle( v2 ) ;
         
         trace(middle) ;
         

Parameters

vector:Vector2

Returns
Vector2 — the middle Point between 2 Points.
min()method 
public function min(vector:Vector2):Vector2

Returns a new Vector2 with the minimum horizontal coordinate and the minimize vertical coordinate.

Example :

         var v1:Vector2 = new Vector2(  10 , 100 ) ;
         var v2:Vector2 = new Vector2( 100 ,  10 ) ;
         
         var v3:Vector2 = v1.min( v2 ) ;
         
         trace( v3 ) ; // [Vector2 x:10 y:10]
         

Parameters

vector:Vector2 — The Vector2 passed in this method

Returns
Vector2 — The new Vector2 with the min horizontal coordinate and the minimize vertical coordinate.
negate()method 
public function negate():void

Sets this Vector2 with negate coordinates.

Example :

         import graphics.geom.Vector2 ;
         
         var v:Vector2 = new Vector2(10,20) ;
         
         trace(v) ; // [Vector2 x:10 y:20]
         
         v.negate() ;
         
         trace(v) ; // [Vector2 x:-10 y:-20]
         
         v.negate() ;
         
         trace(v) ; // [Vector2 x:10 y:20]
         

negate()method 
public static function negate(vector:Vector2):Vector2

Returns the new negate Vector2 of the specified Vector2 objet.

Example :

         import graphics.geom.Vector2 ;
         
         var v:Vector2 = new Point(10,20) ;
         var n:Vector2 = Vector2.negate( v ) ;
         
         trace(n) ; // [Vector2 x:-10 y:-20]
         

Parameters

vector:Vector2

Returns
Vector2 — The new negate Vector2 of the specified Vector2 objet.
normal()method 
public function normal():Vector2

Returns the normal value of this Vector2.

Example :

         import graphics.geom.Vector2 ;
         
         var v:Vector2 = new Vector2( 10 , 10 ) ;
         var n:Vector2 = p.normal() ;
          
         trace(n) ; // [Vector2 x:-10 y:10]
         

Returns
Vector2 — the normal value of this vector.
polar()method 
public static function polar(length:Number, angle:Number):Vector2

Converts a pair of polar coordinates to a Cartesian point coordinate.

Example :

         var polar:Vector2 = Vector2.polar( 5, Math.atan(3/4) ) ;
         trace(polar) ; // [Vector2 x:4 y:3]
         

Parameters

length:Number — The length coordinate of the polar pair.
 
angle:Number — The angle, in radians, of the polar pair.

Returns
Vector2 — The new Cartesian vector.
pow()method 
public function pow(value:Number):void

Computes the power of the vector.

Parameters

value:Number — the value of the pow..

pow()method 
public static function pow(vector:Vector2, value:Number):Vector2

Computes the power of the specified Vector2.

Parameters

vector:Vector2 — the Vector2 reference.
 
value:Number — the value of the pow..

Returns
Vector2 — A new Vector2 powered by the method.
project()method 
public function project(vector:Vector2):Vector2

Returns the projection of a Vector2 with the specified Vector2 passed in argument.

Parameters

vector:Vector2 — the Vector2 to project with this current Vector2.

Returns
Vector2 — the new project Vector2 object.
projectionLength()method 
public function projectionLength(v:Vector2):Number

Returns the size of the projection of this Point with an other Point.

Example :

          var p1:Point = new Point(10,10) ;
         var p2:Point = new Point(100,200) ;
         var size:Number = p1.getProjectionLength(p2) ;
         trace(size) ; // 0.06
         

Parameters

v:Vector2 — the Point use to calculate the length of the projection.

Returns
Number — the size of the projection of this Point with an other Point.
reflect()method 
public function reflect(normal:Vector2):void

Reflects the current Vector2 object with the passed-in normal Vector2 argument.

Parameters

normal:Vector2

reflect()method 
public static function reflect(vector:Vector2, normal:Vector2):Vector2

Creates a new Vector2 object, the reflect of the specific vector.

Parameters

vector:Vector2
 
normal:Vector2

Returns
Vector2 — A new reflect Vector2.
rotate()method 
public function rotate(angle:Number):void

Rotates the Vector2 object with the specified angle passed-in argument.

Example :

         import graphics.geom.Vector2 ;
         import graphics.geom.Vectors2 ;
         
         var v:Vector2 = new Vector2(100,100) ;
         
         Vectors2.rotate(v, Math.PI) ;
         
         trace(v) ;
         

Parameters

angle:Number — the Angle to rotate the specified Vector2 object (in degrees if the "degrees" property is true else in radians).

See also

degrees
rotate()method 
public static function rotate(vector:Vector2, angle:Number):Vector2

Rotates the Vector2 with the specified angle in argument and creates a new Vector2.

Parameters

vector:Vector2 — the Angle to rotate this Vector2.
 
angle:Number

Returns
Vector2 — The rotate new Vector2.
scale()method 
public function scale(value:Number):void

Scales the vector object with the input value.

Parameters

value:Number — a real number to scale the current vector object.

scale()method 
public static function scale(v:Vector2, value:Number):Vector2

Scales a new Vector2 object with the specified Vector2.

Parameters

v:Vector2 — the Vector2 reference to transform.
 
value:Number — a real number to scale the current Vector2.

Returns
Vector2 — A new Vector2 scaled by the value passed in in this method.
set()method 
public function set(vector:Vector2):void

Sets the current Vector2 with the passed-in Vector2 argument.

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2( 10 , 20 ) ;
         var v2:Vector2 = new Vector2( 40 , 60 ) ;
         
         v1.set( v2 ) ;
         
         trace( v1 ) ; 
         

Parameters

vector:Vector2 — The Vector2 object use to calculate the cross value.

setByObject()method 
public function setByObject(o:Object):void

Sets the current Vector2 object with the passed-in Object argument.

Parameters

o:Object — The Object to set the vector with this x and y properties.

setByPoint()method 
public function setByPoint(p:Point):void

Sets the current Vector2 object with the passed-in flash.geom.Point argument.

Parameters

p:Point — The Point to set the vector.

squaredDistance()method 
public static function squaredDistance(v1:Vector2, v2:Vector2):Number

Returns the squared distance between 2 vectors.

Parameters

v1:Vector2 — the first vector.
 
v2:Vector2 — the second vector.

Returns
Number — the squared distance between 2 vectors.
substract()method 
public function substract(v:Vector2):void

Computes the substraction of the current vector object with an other.

Parameters

v:Vector2 — the vector to substract.

substract()method 
public static function substract(v1:Vector2, v2:Vector2):Vector2

Computes the substraction of two Vector2.

Parameters

v1:Vector2 — a Vector2 to substract.
 
v2:Vector2 — a Vector2.

Returns
Vector2 — the substraction result of two Vector2.
swap()method 
public function swap(vector:Vector2):void

Swap the horizontal and vertical coordinates of two Vector2 objects.

Example :

         import graphics.geom.Vector2 ;
         
         var v1:Vector2 = new Vector2( 10 , 20 ) ;
         var v2:Vector2 = new Vector2( 30 , 40 ) ;
         
         trace( v1 + " / " + v2 ) ; // [Vector2 x:10 y:20] / [Vector2 x:30 y:40]
         
         v1.swap( v2 ) ;
         
         trace( v1 + " / " + v2 ) ; // [Vector2 x:30 y:40] / [Vector2 x:10 y:20]
         

Parameters

vector:Vector2

toObject()method 
public function toObject():Object

Returns the Object representation of this object.

Returns
Object — the Object representation of this object.
toSource()method 
public function toSource(indent:int = 0):String

Returns the source code string representation of the object.

Parameters

indent:int (default = 0)

Returns
String — the source code string representation of the object.
toString()method 
override public function toString():String

Returns the string representation of the object.

Returns
String — the string representation of the object.
Constant Detail
DOWNConstant
public static const DOWN:Vector2

Indicates the relative "down" direction.

LEFTConstant 
public static const LEFT:Vector2

Indicates the relative "left" direction.

RIGHTConstant 
public static const RIGHT:Vector2

Indicates the relative "right" direction.

UPConstant 
public static const UP:Vector2

Indicates the relative "up" direction.