Vector (com.robertpenner.geom.Vector)

com.robertpenner.geom.Vector
class Vector

The Vector class is designed to represent vectors and points in two-dimensional space. Vectors can be added together, scaled, rotated, and otherwise manipulated with these methods.

Availability:

ActionScript 2.0; Flash Player 6

Property summary

Property Description
x:Number

The x property of the vector object.

y:Number

The y property of the vector object.

length:Number

The length of the vector object.

angle:Number

The angle of the vector object.

Method summary

Method Description
toString() : String

Returns a string representation of the vector object.

reset(x:Number, y:Number) : Void

Reinitializes the vector object.

getClone() : Vector

Returns a new vector object containing the x and y values of the current vector.

equals(v:Vector) : Boolean

Tests the equality of two vector objects.

plus(v:Vector) : Void

Adds a vector object to the current vector.

plusNew(v:Vector) : Vector

Performs addition on two vector objects and returns the result of the addition as a new vector.

minus(v:Vector) : Void

Subtracts a vector object from the current vector.

minusNew(v:Vector) : Vector

Performs subtraction on two vector objects and returns the result of the subtraction as a new vector.

negate() : Void

Reverses the direction of the current vector.

negateNew() : Vector

Reverses the direction of the current vector and returns the result as a new vector.

scale(s:Number) : Void

Scales the length of the current vector object by a scale factor.

scaleNew(s:Number) : Vector

Scales the length of the current vector object by a scale factor and returns the result of the scale as a new vector.

getLength() : Number

Gets the size of the current vector.

setLength(len:Number) : Void

Sets the size of the current vector.

getAngle() : Number

Gets the angle of the current vector.

setAngle(ang:Number) : Void

Sets the angle of the current vector.

rotate(ang:Number) : Void

Rotates the angle of the current vector object by a certain amount of degrees.

rotateNew(ang:Number) : Vector

Rotates the angle of the current vector object by a certain amount of degrees and returns the result of the rotation as a new vector.

dot(v:Vector) : Number

Performs multiplication on two vector objects and returns the dot product.

getNormal() : Vector

Finds a normal for the current vector.

isPerpTo(v:Vector) : Boolean

Tests if the current vector is perpendicular to another vector.

isNormalTo(v:Vector) : Boolean

Tests if the current vector is the normal of another vector.

angleBetween(v:Vector) : Number

Returns the angle between the current vector and another vector.

Vector(x:Number, y:Number)

Usage


var v:Vector = new Vector(-9,4);
trace (v.x);
trace (v.y);

Parameters

x:Number - The x value of the vector.

y:Number - The y value of the vector.

Description

Creates an instance of the Vector class.


toString() : String

Usage


var position:Vector = new Vector(5,8);
trace (position.toString());
trace (position);

Parameters

Returns

Returns the string representation of the vector object.

Description

Returns a string representation of the vector object.


reset(x:Number, y:Number) : Void

Usage


var velocity:Vector = new Vector(5,1);
velocity.reset(-9,7);
trace (velocity);

Parameters

x:Number - The x value of the vector.

y:Number - The y value of the vector.

Returns

Nothing.

Description

Reinitializes the vector object.


getClone() : Vector

Usage


var forceA:Vector = new Vector(2,4);
var forceB:Vector = forceA.getClone();
trace (forceA);
trace (forceB);
trace (forceA == forceB);

Parameters

Returns

Returns a copy of the current vector object.

Description

Returns a new vector object containing the x and y values of the current vector.


equals(v:Vector) : Boolean

Usage


var forceA:Vector = new Vector(2,4);
var forceB:Vector = forceA.getClone();
trace (forceB.equals(forceA));
forceA.reset(0,0);
trace (forceB.equals(forceA));

Parameters

v:Vector - The vector object to compare with the current vector.

Returns

Returns true if the values of the two x properties and the two y properties are the same.

Description

Tests the equality of two vector objects.


plus(v:Vector) : Void

Usage


var position:Vector = new Vector(1,3);
var velocity:Vector = new Vector(3,0);
position.plus(velocity);
trace (position);

Parameters

v:Vector - The vector object to add to the current vector.

Returns

Nothing.

Description

Adds a vector object to the current vector.


plusNew(v:Vector) : Vector

Usage


var position:Vector = new Vector(4,1);
var velocity:Vector = new Vector(-2,0);
var newPosition:Vector = position.plusNew(velocity);
trace (newPosition);

Parameters

v:Vector - The vector object to add to the current vector.

Returns

Returns the result of the addition as a new vector.

Description

Performs addition on two vector objects and returns the result of the addition as a new vector.


minus(v:Vector) : Void

Usage


var forceA:Vector = new Vector(1,1);
var forceB:Vector = new Vector(-2,-1);
forceA.minus(forceB);
trace (forceA);

Parameters

v:Vector - The vector object to subtract from the current vector.

Returns

Nothing.

Description

Subtracts a vector object from the current vector.


minusNew(v:Vector) : Vector

Usage


var pointA:Vector = new Vector(0,1);
var pointB:Vector = new Vector(-2,0);
var displacement:Vector = pointB.minusNew(pointA);
trace (displacement);

Parameters

v:Vector - The vector object to subtract from the current vector.

Returns

Returns the result of the subtraction as a new vector.

Description

Performs subtraction on two vector objects and returns the result of the subtraction as a new vector.


negate() : Void

Usage


var direction:Vector = new Vector(2,3);
direction.negate();
trace (direction);

Parameters

Returns

Nothing.

Description

Reverses the direction of the current vector.


negateNew() : Vector

Usage


var forward:Vector = new Vector(99,0);
var backward:Vector = forward.negateNew();
trace (backward);

Parameters

Returns

Returns the reversed vector object as a new vector.

Description

Reverses the direction of the current vector and returns the result as a new vector.


scale(s:Number) : Void

Usage


var windForce:Vector = new Vector(2,3);
windForce.scale(2);
trace (windForce);

Parameters

s:Number - The scale factor to multiply the current vector by.

Returns

Nothing.

Description

Scales the length of the current vector object by a scale factor.


scaleNew(s:Number) : Vector

Usage


var windForce:Vector = new Vector(-2,1);
var galeForce:Vector = windForce.scaleNew(2); //strong wind
trace (galeForce);

Parameters

s:Number - The scale factor to multiply the current vector by.

Returns

Returns the scaled vector object as a new vector.

Description

Scales the length of the current vector object by a scale factor and returns the result of the scale as a new vector.


getLength() : Number

Usage


var velocity:Vector = new Vector(3,4);
var speed:Number = velocity.getLength();
trace (speed);

Parameters

Returns

Returns the size of the current vector.

Description

Gets the size of the current vector.


setLength(len:Number) : Void

Usage


var velocity:Vector = new Vector(3,4);
var newSpeed:Number = 10;
velocity.setLength(newSpeed);
trace (velocity);
trace (velocity.getLength());

Parameters

len:Number - The length that the current vector will be set.

Returns

Nothing.

Description

Sets the size of the current vector.


getAngle() : Number

Usage


var destination:Vector = new Vector(5,5);
var compassBearing:Number = destination.getAngle();
trace (compassBearing);

Parameters

Returns

Returns the angle of the current vector.

Description

Gets the angle of the current vector.


setAngle(ang:Number) : Void

Usage


var velocity:Vector = new Vector(7,0);
var newBearing:Number = 180;
velocity.setAngle(newBearing);
trace (velocity);
trace (velocity.getAngle());

Parameters

ang:Number - The angle that the current vector will be set.

Returns

Nothing.

Description

Sets the angle of the current vector.


rotate(ang:Number) : Void

Usage


var direction:Vector = new Vector(5,5);
trace (direction.getAngle());
direction.rotate(-90);
trace (direction);
trace (direction.getAngle());

Parameters

ang:Number - The amount of degrees that the current vector object will be rotated by.

Returns

Nothing.

Description

Rotates the angle of the current vector object by a certain amount of degrees.


rotateNew(ang:Number) : Vector

Usage


var direction:Vector = new Vector(5,5);
var newDirection:Vector = direction.rotateNew(10);
trace (newDirection.getAngle());

Parameters

ang:Number - The amount of degrees that the current vector object will be rotated by.

Returns

Returns the result of the rotation as a new vector.

Description

Rotates the angle of the current vector object by a certain amount of degrees and returns the result of the rotation as a new vector.


dot(v:Vector) : Number

Usage


var v:Vector = new Vector(2,3);
var w:Vector = new Vector(4,5);
trace (v.dot(w));
trace (w.dot(v));

Parameters

v:Vector - The vector object to multiply the current vector by.

Returns

Returns the dot product.

Description

Performs multiplication on two vector objects and returns the dot product.


getNormal() : Vector

Usage


var wallDirection:Vector = new Vector(3,5);
var forceDirection:Vector = wallDirection.getNormal();
trace (forceDirection);

Parameters

Returns

Returns the vector object that is the normal to the current vector.

Description

Finds a normal for the current vector.


isPerpTo(v:Vector) : Boolean

Usage


var goingLeft:Vector = new Vector(-3,0);
var goingRight:Vector = new Vector(5,0);
trace (goingLeft.isPerpTo(goingRight));
var goingUp:Vector = new Vector(0,8);
trace (goingUp.isPerpTo(goingLeft));

Parameters

v:Vector - The vector object to check perpendicularity against.

Returns

Returns true if the dot product is zero (indicating perpendicularity).

Description

Tests if the current vector is perpendicular to another vector.


isNormalTo(v:Vector) : Boolean

Usage


var goingLeft:Vector = new Vector(-3,0);
var goingRight:Vector = new Vector(5,0);
trace (goingLeft.isNormalTo(goingRight));
var goingUp:Vector = new Vector(0,8);
trace (goingUp.isNormalTo(goingLeft));

Parameters

v:Vector - The vector object to check perpendicularity against.

Returns

Returns true if the dot product is zero.

Description

Tests if the current vector is the normal of another vector.


angleBetween(v:Vector) : Number

Usage


var pullForce:Vector = new Vector(4,0);
var frictionForce:Vector = new Vector(-1,0);
var theta:Number = pullForce.angleBetween(frictionForce);
trace (theta);

Parameters

v:Vector - The vector object to check the angle between.

Returns

Returns the angle between the current vector and another vector.

Description

Returns the angle between the current vector and another vector.


x : Number

Usage


v.x = 9;

Description

The x property of the vector object.


y : Number

Usage


v.y = -4;

Description

The y property of the vector object.


length : Number

Usage


var velocity:Vector = new Vector(3,4);
var newSpeed:Number = 10;
velocity.length = newSpeed;
trace (velocity);
trace (velocity.length);

Description

The length of the vector object.


angle : Number

Usage


var destination:Vector = new Vector(5,5);
var compassBearing:Number = destination.angle;
trace (compassBearing);

Description

The angle of the vector object.