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.
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. |
var v:Vector = new Vector(-9,4);
trace (v.x);
trace (v.y);
x:Number
- The x value of the vector.
y:Number
- The y value of the vector.
Creates an instance of the Vector class.
var position:Vector = new Vector(5,8);
trace (position.toString());
trace (position);
Returns the string representation of the vector object.
Returns a string representation of the vector object.
var velocity:Vector = new Vector(5,1);
velocity.reset(-9,7);
trace (velocity);
x:Number
- The x value of the vector.
y:Number
- The y value of the vector.
Nothing.
Reinitializes the vector object.
var forceA:Vector = new Vector(2,4);
var forceB:Vector = forceA.getClone();
trace (forceA);
trace (forceB);
trace (forceA == forceB);
Returns a copy of the current vector object.
Returns a new vector object containing the x and y values of the current vector.
var forceA:Vector = new Vector(2,4);
var forceB:Vector = forceA.getClone();
trace (forceB.equals(forceA));
forceA.reset(0,0);
trace (forceB.equals(forceA));
v:Vector
- The vector object to compare with the current vector.
Returns true if the values of the two x properties and the two y properties are the same.
Tests the equality of two vector objects.
var position:Vector = new Vector(1,3);
var velocity:Vector = new Vector(3,0);
position.plus(velocity);
trace (position);
v:Vector
- The vector object to add to the current vector.
Nothing.
Adds a vector object to the current vector.
var position:Vector = new Vector(4,1);
var velocity:Vector = new Vector(-2,0);
var newPosition:Vector = position.plusNew(velocity);
trace (newPosition);
v:Vector
- The vector object to add to the current vector.
Returns the result of the addition as a new vector.
Performs addition on two vector objects and returns the result of the addition as a new vector.
var forceA:Vector = new Vector(1,1);
var forceB:Vector = new Vector(-2,-1);
forceA.minus(forceB);
trace (forceA);
v:Vector
- The vector object to subtract from the current vector.
Nothing.
Subtracts a vector object from the current vector.
var pointA:Vector = new Vector(0,1);
var pointB:Vector = new Vector(-2,0);
var displacement:Vector = pointB.minusNew(pointA);
trace (displacement);
v:Vector
- The vector object to subtract from the current vector.
Returns the result of the subtraction as a new vector.
Performs subtraction on two vector objects and returns the result of the subtraction as a new vector.
var direction:Vector = new Vector(2,3);
direction.negate();
trace (direction);
Nothing.
Reverses the direction of the current vector.
var forward:Vector = new Vector(99,0);
var backward:Vector = forward.negateNew();
trace (backward);
Returns the reversed vector object as a new vector.
Reverses the direction of the current vector and returns the result as a new vector.
var windForce:Vector = new Vector(2,3);
windForce.scale(2);
trace (windForce);
s:Number
- The scale factor to multiply the current vector by.
Nothing.
Scales the length of the current vector object by a scale factor.
var windForce:Vector = new Vector(-2,1);
var galeForce:Vector = windForce.scaleNew(2); //strong wind
trace (galeForce);
s:Number
- The scale factor to multiply the current vector by.
Returns the scaled vector object as a new vector.
Scales the length of the current vector object by a scale factor and returns the result of the scale as a new vector.
var velocity:Vector = new Vector(3,4);
var speed:Number = velocity.getLength();
trace (speed);
Returns the size of the current vector.
Gets the size of the current vector.
var velocity:Vector = new Vector(3,4);
var newSpeed:Number = 10;
velocity.setLength(newSpeed);
trace (velocity);
trace (velocity.getLength());
len:Number
- The length that the current vector will be set.
Nothing.
Sets the size of the current vector.
var destination:Vector = new Vector(5,5);
var compassBearing:Number = destination.getAngle();
trace (compassBearing);
Returns the angle of the current vector.
Gets the angle of the current vector.
var velocity:Vector = new Vector(7,0);
var newBearing:Number = 180;
velocity.setAngle(newBearing);
trace (velocity);
trace (velocity.getAngle());
ang:Number
- The angle that the current vector will be set.
Nothing.
Sets the angle of the current vector.
var direction:Vector = new Vector(5,5);
trace (direction.getAngle());
direction.rotate(-90);
trace (direction);
trace (direction.getAngle());
ang:Number
- The amount of degrees that the current vector object will be rotated by.
Nothing.
Rotates the angle of the current vector object by a certain amount of degrees.
var direction:Vector = new Vector(5,5);
var newDirection:Vector = direction.rotateNew(10);
trace (newDirection.getAngle());
ang:Number
- The amount of degrees that the current vector object will be rotated by.
Returns the result of the rotation as a new 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.
var v:Vector = new Vector(2,3);
var w:Vector = new Vector(4,5);
trace (v.dot(w));
trace (w.dot(v));
v:Vector
- The vector object to multiply the current vector by.
Returns the dot product.
Performs multiplication on two vector objects and returns the dot product.
var wallDirection:Vector = new Vector(3,5);
var forceDirection:Vector = wallDirection.getNormal();
trace (forceDirection);
Returns the vector object that is the normal to the current vector.
Finds a normal for the current vector.
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));
v:Vector
- The vector object to check perpendicularity against.
Returns true if the dot product is zero (indicating perpendicularity).
Tests if the current vector is perpendicular to another vector.
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));
v:Vector
- The vector object to check perpendicularity against.
Returns true if the dot product is zero.
Tests if the current vector is the normal of another vector.
var pullForce:Vector = new Vector(4,0);
var frictionForce:Vector = new Vector(-1,0);
var theta:Number = pullForce.angleBetween(frictionForce);
trace (theta);
v:Vector
- The vector object to check the angle between.
Returns the angle between the current vector and another vector.
Returns the angle between the current vector and another vector.
v.x = 9;
The x property of the vector object.
v.y = -4;
The y property of the vector object.
var velocity:Vector = new Vector(3,4);
var newSpeed:Number = 10;
velocity.length = newSpeed;
trace (velocity);
trace (velocity.length);
The length of the vector object.
var destination:Vector = new Vector(5,5);
var compassBearing:Number = destination.angle;
trace (compassBearing);
The angle of the vector object.