Vector3d (com.robertpenner.geom.Vector3d)

com.robertpenner.geom.Vector
        |
        +--com.robertpenner.geom.Vector3d
class Vector3d
extends Vector

The Vector3d class is designed to represent vectors and points in three-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.

z:Number

The z property of the vector object.

length:Number

The length of the vector object.

Method summary

Method Description
toString() : String

Returns a string representation of the vector object.

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

Reinitializes the vector object.

getClone() : Vector3d

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

equals(v:Vector3d) : Object

Tests the equality of two vector objects.

plus(v:Vector3d) : Void

Adds a vector object to the current vector.

plusNew(v:Vector3d) : Vector3d

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

minus(v:Vector3d) : Void

Subtracts a vector object from the current vector.

minusNew(v:Vector3d) : Vector3d

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() : Vector3d

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) : Vector3d

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.

dot(v:Vector3d) : Number

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

cross(v:Vector3d) : Vector3d

Performs multiplication on two vector objects and returns a third vector object that is perpendicular to the first two vectors.

angleBetween(v:Vector) : Number

Returns the angle between the current vector and another vector.

getPerspective(viewDist:Number) : Number

Calculates and returns the perspective ratio needed to scale an object correctly.

persProject(p:Number) : Void

Performs a perspective projection on a 3d point. It converts (x, y, z) coordinates to a 2d location (x, y) on the screen.

persProjectNew(p:Number) : Vector3d

Performs a perspective projection on a 3d point and returns the result of the projection as a new vector. It converts (x, y, z) coordinates to a 2d location (x, y) on the screen.

rotateX(ang:Number) : Void

Rotates the current vector object around the x-axis by a certain amount of degrees.

rotateXTrig(ca:Number, sa:Number) : Void

Rotates the current vector object around the x-axis by the cosine and sine of an angle.

rotateY(ang:Number) : Void

Rotates the current vector object around the y-axis by a certain amount of degrees.

rotateYTrig(ca:Number, sa:Number) : Void

Rotates the current vector object around the y-axis by the cosine and sine of an angle.

rotateZ(ang:Number) : Void

Rotates the current vector object around the z-axis by a certain amount of degrees.

rotateZTrig(ca:Number, sa:Number) : Void

Rotates the current vector object around the z-axis by the cosine and sine of an angle.

rotateXY(a:Number, b:Number) : Void

Rotates the current vector object around the x and y axes by a certain amount of degrees.

rotateXYTrig(ca:Number, sa:Number, cb:Number, sb:Number) : Void

Rotates the current vector object around the x and y axes by the cosine and sine of an angle.

rotateXYZ(a:Number, b:Number, c:Number) : Void

Rotates the current vector object around the x, y and z axes by a certain amount of degrees.

rotateXYZTrig(ca:Number, sa:Number, cb:Number, sb:Number, cc:Number, sc:Number) : Void

Rotates the current vector object around the x, y and z axes by the cosine and sine of an angle.

Vector3d(x:Number, y:Number, z:Number)

Usage


var v:Vector3d = new Vector3d(4,0,1);
trace (v.x);
trace (v.y);
trace (v.z);

Parameters

x:Number - The x value of the vector.

y:Number - The y value of the vector.

z:Number - The z value of the vector.

Description

Creates an instance of the Vector3d class.


toString() : String

Usage


var position:Vector3d = new Vector3d(2,4,6.952013);
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, z:Number) : Void

Usage


var acceleration:Vector3d = new Vector3d(0,0,0);
acceleration.reset(10,20,30);
trace (acceleration);

Parameters

x:Number - The x value of the vector.

y:Number - The y value of the vector.

z:Number - The z value of the vector.

Returns

Nothing.

Description

Reinitializes the vector object.


getClone() : Vector3d

Usage


var force:Vector3d = new Vector3d(2,4,6);
var forceCopy:Vector3d = force.getClone();
trace (forceCopy);
trace (forceCopy == force);

Parameters

Returns

Returns a copy of the current vector object.

Description

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


equals(v:Vector3d) : Object

Usage


var force:Vector3d = new Vector3d(2,4,6);
var forceCopy:Vector3d = force.getClone();
trace (forceCopy.equals(force));

Parameters

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

Returns

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

Description

Tests the equality of two vector objects.


plus(v:Vector3d) : Void

Usage


var position:Vector3d = new Vector3d(1,1,1);
var velocity:Vector3d = new Vector3d(2,1,3);
position.plus(velocity);
trace (position);

Parameters

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

Returns

Nothing.

Description

Adds a vector object to the current vector.


plusNew(v:Vector3d) : Vector3d

Usage


var position:Vector3d = new Vector3d(1,1,1);
var velocity:Vector3d = new Vector3d(2,1,3);
var newPosition:Vector3d = position.plusNew(velocity);
trace (newPosition);

Parameters

v:Vector3d - 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:Vector3d) : Void

Usage


var farPoint:Vector3d = new Vector3d(10,-6,5);
var nearPoint:Vector3d = new Vector3d(6,-2,0);
farPoint.minus(nearPoint);
trace (farPoint);

Parameters

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

Returns

Nothing.

Description

Subtracts a vector object from the current vector.


minusNew(v:Vector3d) : Vector3d

Usage


var farPoint:Vector3d = new Vector3d(10,-6,5);
var nearPoint:Vector3d = new Vector3d(6,-2,0);
var displacement:Vector3d = farPoint.minusNew(nearPoint);
trace (displacement);

Parameters

v:Vector3d - 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:Vector3d = new Vector3d(3,-6,-9);
direction.negate();
trace (direction);

Parameters

Returns

Nothing.

Description

Reverses the direction of the current vector.


negateNew() : Vector3d

Usage


var towards:Vector3d = new Vector3d(0,0,-20);
var away:Vector3d = towards.negateNew();
trace (away);

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 tensionForce:Vector3d = new Vector3d(3,-1,0);
tensionForce.scale(2);
trace (tensionForce);

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) : Vector3d

Usage


var tensionForce:Vector3d = new Vector3d(3,-1,0);
var strongerForce:Vector3d = tensionForce.scaleNew(2);
trace (strongerForce);

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:Vector3d = new Vector3d(3,4,0);
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:Vector3d = new Vector3d(3,4,0);
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.


dot(v:Vector3d) : Number

Usage


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

Parameters

v:Vector3d - 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.


cross(v:Vector3d) : Vector3d

Usage


var a:Vector3d = new Vector3d(2,0,0);
var b:Vector3d = new Vector3d(0,2,0);
var c:Vector3d = a.cross(b);
trace (c);
var d:Vector3d = b.cross(a);
trace (d);

Parameters

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

Returns

Returns a normal vector to the other two vector objects.

Description

Performs multiplication on two vector objects and returns a third vector object that is perpendicular to the first two vectors.


angleBetween(v:Vector) : Number

Usage


var pullForce:Vector3d = new Vector3d(4,0,0);
var frictionForce:Vector3d = new Vector3d(-1,0,1);
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.


getPerspective(viewDist:Number) : Number

Usage


var pointA:Vector3d = new Vector3d(50,20,40);
var pers:Number = pointA.getPerspective();
trace (pers);

Parameters

viewDist:Number - The viewing distance of the projection.
Large values produce an image in which there is little size difference between close and distant objects.
Small values produce more of a "fisheye" effect, distorting the image like a peephole.
If no value is specified, a default value of 300 is chosen, which gives a natural-looking perspective.

Returns

Returns the perspective ratio needed to scale an object correctly.

Description

Calculates and returns the perspective ratio needed to scale an object correctly.


persProject(p:Number) : Void

Usage


var pointA:Vector3d = new Vector3d(50,20,40);
pointA.persProject();
trace (pointA);

Parameters

p:Number - The perspective ratio. If no value is specified, it is calculated automatically by calling the getPerspective() method.

Returns

Nothing.

Description

Performs a perspective projection on a 3d point. It converts (x, y, z) coordinates to a 2d location (x, y) on the screen.


persProjectNew(p:Number) : Vector3d

Usage


var pointA:Vector3d = new Vector3d(50,20,40);
pointA.persProject();
trace (pointA);

Parameters

p:Number - The perspective ratio. If no value is specified, it is calculated automatically by calling the getPerspective() method.

Returns

Returns the result of the projection as a new vector.

Description

Performs a perspective projection on a 3d point and returns the result of the projection as a new vector. It converts (x, y, z) coordinates to a 2d location (x, y) on the screen.


rotateX(ang:Number) : Void

Usage


var p:Vector3d = new Vector3d(1,4,7);
p.rotateX(180);
trace (p);

Parameters

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

Returns

Nothing.

Description

Rotates the current vector object around the x-axis by a certain amount of degrees.


rotateXTrig(ca:Number, sa:Number) : Void

Usage


var p:Vector3d = new Vector3d(1,4,7);
var cosAngle:Number = Degree.cosD(180);
var sinAngle:Number = Degree.sinD(180);
p.rotateXTrig(cosAngle, sinAngle);
trace (p);

Parameters

ca:Number - The cosine of the angle to rotate the current vector object by.

sa:Number - The sine of the angle to rotate the current vector object by.

Returns

Nothing.

Description

Rotates the current vector object around the x-axis by the cosine and sine of an angle.


rotateY(ang:Number) : Void

Usage


var p:Vector3d = new Vector3d(1,4,7);
p.rotateY(180);
trace (p);

Parameters

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

Returns

Nothing.

Description

Rotates the current vector object around the y-axis by a certain amount of degrees.


rotateYTrig(ca:Number, sa:Number) : Void

Usage


var p:Vector3d = new Vector3d(3,-8,5);
var cosAngle:Number = Degree.cosD(90);
var sinAngle:Number = Degree.sinD(90);
p.rotateYTrig(cosAngle, sinAngle);
trace (p);

Parameters

ca:Number - The cosine of the angle to rotate the current vector object by.

sa:Number - The sine of the angle to rotate the current vector object by.

Returns

Nothing.

Description

Rotates the current vector object around the y-axis by the cosine and sine of an angle.


rotateZ(ang:Number) : Void

Usage


var p:Vector3d = new Vector3d(1,4,7);
p.rotateZ(180);
trace (p);

Parameters

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

Returns

Nothing.

Description

Rotates the current vector object around the z-axis by a certain amount of degrees.


rotateZTrig(ca:Number, sa:Number) : Void

Usage


var p:Vector3d = new Vector3d(6,1,4);
var cosAngle:Number = Degree.cosD(45);
var sinAngle:Number = Degree.sinD(45);
p.rotateZTrig(cosAngle, sinAngle);
trace (p);

Parameters

ca:Number - The cosine of the angle to rotate the current vector object by.

sa:Number - The sine of the angle to rotate the current vector object by.

Returns

Nothing.

Description

Rotates the current vector object around the z-axis by the cosine and sine of an angle.


rotateXY(a:Number, b:Number) : Void

Usage


var p:Vector3d = new Vector3d(8,0,0);
p.rotateXY(45,45);
trace (p);

Parameters

a:Number - The amount of degrees that the current vector object will be rotated around the x-axis by.

b:Number - The amount of degrees that the current vector object will be rotated around the y-axis by.

Returns

Nothing.

Description

Rotates the current vector object around the x and y axes by a certain amount of degrees.


rotateXYTrig(ca:Number, sa:Number, cb:Number, sb:Number) : Void

Usage


var p:Vector3d = new Vector3d(6,1,4);
var cosAngleA:Number = Degree.cosD(45);
var sinAngleA:Number = Degree.sinD(45);
var cosAngleB:Number = Degree.cosD(90);
var sinAngleB:Number = Degree.sinD(90);
p.rotateXYTrig(cosAngleA, sinAngleA, cosAngleB, sinAngleB);
trace (p);

Parameters

ca:Number - The cosine of the angle to rotate the current vector object around the x-axis by.

sa:Number - The sine of the angle to rotate the current vector object around the x-axis by.

cb:Number - The cosine of the angle to rotate the current vector object around the y-axis by.

sb:Number - The sine of the angle to rotate the current vector object around the y-axis by.

Returns

Nothing.

Description

Rotates the current vector object around the x and y axes by the cosine and sine of an angle.


rotateXYZ(a:Number, b:Number, c:Number) : Void

Usage


var p:Vector3d = new Vector3d(8,0,0);
p.rotateXYZ(45,45,45);
trace (p);

Parameters

a:Number - The amount of degrees that the current vector object will be rotated around the x-axis by.

b:Number - The amount of degrees that the current vector object will be rotated around the y-axis by.

c:Number - The amount of degrees that the current vector object will be rotated around the z-axis by.

Returns

Nothing.

Description

Rotates the current vector object around the x, y and z axes by a certain amount of degrees.


rotateXYZTrig(ca:Number, sa:Number, cb:Number, sb:Number, cc:Number, sc:Number) : Void

Usage


var p:Vector3d = new Vector3d(6,1,4);
var cosAngleA:Number = Degree.cosD(45);
var sinAngleA:Number = Degree.sinD(45);
var cosAngleB:Number = Degree.cosD(90);
var sinAngleB:Number = Degree.sinD(90);
var cosAngleC:Number = Degree.cosD(135);
var sinAngleC:Number = Degree.sinD(135);
p.rotateXYZTrig(cosAngleA, sinAngleA, cosAngleB, sinAngleB, cosAngleC, sinAngleC);
trace (p);

Parameters

ca:Number - The cosine of the angle to rotate the current vector object around the x-axis by.

sa:Number - The sine of the angle to rotate the current vector object around the x-axis by.

cb:Number - The cosine of the angle to rotate the current vector object around the y-axis by.

sb:Number - The sine of the angle to rotate the current vector object around the y-axis by.

cc:Number - The cosine of the angle to rotate the current vector object around the z-axis by.

sc:Number - The sine of the angle to rotate the current vector object around the z-axis by.

Returns

Nothing.

Description

Rotates the current vector object around the x, y and z axes by the cosine and sine of an angle.


x : Number

Usage


v.x = -4;

Description

The x property of the vector object.


y : Number

Usage


v.y = 0;

Description

The y property of the vector object.


z : Number

Usage


v.z = -1;

Description

The z property of the vector object.


length : Number

Usage


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

Description

The length of the vector object.