# Class: Vector4

## RS.Math.Vector4

Class representing a 4D Vector with components x, y, z and w.

#### new RS.Math.Vector4 (initial)

Creates a new Vector4. Accepts any arguments that RS.Math.Vector4#set accepts.

Name Type Description
`initial` RS.Math.Vector4 | Array | Object | Number optional

initial value.

##### Example
``````let v = new RS.Math.Vector4();
v = new RS.Math.Vector4(1,2,3);
v = new RS.Math.Vector4([0.2,-0.3,0.5,0.4]);
v = new RS.Math.Vector4({x: 0.1, y: 0.53, z: -0.2});``````

### Members

#### wNumber

The W component of the vector

#### xNumber

The X component of the vector

#### yNumber

The Y component of the vector

#### zNumber

The Z component of the vector

### Methods

Adds another vector to this vector.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector

##### Returns:
Type Description
RS.Math.Vector4 this
##### Example
``````const vec = new RS.Math.Vector4(1,2,3);
// vec is now {x:8,y:-1,z:3,w:1}``````

#### clone ()RS.Math.Vector4

returns a copy of this vector.

Type Description
RS.Math.Vector4

#### cross (rhs)RS.Math.Vector4

Returns the cross product between this vector and another.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector.

##### Returns:
Type Description
RS.Math.Vector4 the cross product

#### distance (rhs)Number

Returns the distance between the point specified by this vector and another

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector.

##### Returns:
Type Description
Number the distance

#### divide (rhs)RS.Math.Vector4

Divides another vector into this vector per-component.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector

##### Returns:
Type Description
RS.Math.Vector4 this
##### Example
``````const vec = new RS.Math.Vector4(1,2,3);
vec.divide(new RS.Math.Vector4(7,-3,1));
// vec is now {x:0.1428,y:-0.6667,z:3,w:1}``````

#### dot (rhs)Number

Returns the dot product between this vector and another.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector.

##### Returns:
Type Description
Number the dot product

#### equal (rhs, tolerance)Boolean

Returns whether this vector is equal to another vector.

Name Type Description
`rhs` RS.Math.Vector4

the vector to compare to

`tolerance` Number optional

if provided then this level of tolerance is used.

##### Returns:
Type Description
Boolean `true` if equal, `false` if not
##### Example
``````let vec = new RS.Math.Vector4(0.01,-0.05,0.03);
vec.equal(vec);
// returns true
vec.equal(new RS.Math.Vector4(0.02,-0.05,0.03));
// returns false
vec.equal(new RS.Math.Vector4(0.02,-0.05,0.03),0.1);
// returns true due to tolerance``````

#### equal_with_tolerance (rhs, tolerance)Boolean

Returns whether this vector is equal to another vector within a tolerance.

Name Type Description
`rhs` RS.Math.Vector4

the vector to compare to

`tolerance` Number optional

if provided then this level of tolerance is used, otherwise tolerance is `10e-5`

##### Returns:
Type Description
Boolean `true` if equal, `false` if not
##### Example
``````let vec = new RS.Math.Vector4(0.01,-0.05,0.03);
vec.equal_with_tolerance(vec);
// returns true
vec.equal_with_tolerance(new RS.Math.Vector4(0.02,-0.05,0.03));
// returns false
vec.equal_with_tolerance(new RS.Math.Vector4(0.01001,-0.05,0.03));
// returns true due to default tolerance
vec.equal_with_tolerance(new RS.Math.Vector4(0.02,-0.05,0.03),0.1);
// returns true due to tolerance``````

#### is_colinear (rhs)Boolean

Returns whether this vector and another are colinear (point in the same direction.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector

##### Returns:
Type Description
Boolean `true` if colinear, `false` if not
##### Example
``````const vec = new RS.Math.Vector4(1,2,3);
vec.is_colinear(new RS.Math.Vector4(2,4,6));
// returns true
vec.is_colinear(new RS.Math.Vector4(1,0,0));
// returns false
vec.is_colinear(new RS.Math.Vector4(-2,-4,-6));
// returns false since they point in opposite directions``````

#### is_null_vector (tolerance)Boolean

Returns whether this vector is the null vector

Name Type Description
`tolerance` Number optional

if provided then this level of tolerance is used.

##### Returns:
Type Description
Boolean `true` if null, `false` if not
##### Example
``````let vec = new RS.Math.Vector4(0.01,-0.05,0.03);
vec.is_null_vector();
// returns false
vec.is_null_vector(0.1);
// returns true due to tolerance
vec = new RS.Math.Vector4();
vec.is_null_vector();
// returns true``````

#### length ()Number

Returns the length of this vector.

##### Returns:
Type Description
Number the length

#### multiply (rhs)RS.Math.Vector4

Multiplies another vector with this vector per-component.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector

##### Returns:
Type Description
RS.Math.Vector4 this
##### Example
``````const vec = new RS.Math.Vector4(1,2,3);
vec.multiply(new RS.Math.Vector4(7,-3,1));
// vec is now {x:7,y:-6,z:3,w:1}``````

#### normalize ()RS.Math.Vector4

Normalizes this vector.

##### Returns:
Type Description
RS.Math.Vector4 this

#### rotate (matrix)RS.Math.Vector4

Transforms this vector by multiplying the rotation portion of the provided matrix on the right hand side.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

##### Returns:
Type Description
RS.Math.Vector4 this

#### rotate_to (matrix, out)RS.Math.Vector4

Transforms this vector by multiplying the rotation portion of the provided matrix on the right hand side and copies the result into the out vector. This vector is not modified.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

`out` RS.Math.Vector4 optional

The vector to receive the result. If not provided a new vector is created.

##### Returns:
Type Description
RS.Math.Vector4 the transformed vector.

#### rotate_transpose (matrix)RS.Math.Vector4

Transforms this vector by multiplying the rotation portion of the transposed provided matrix on the right hand side.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

##### Returns:
Type Description
RS.Math.Vector4 this

#### rotate_transpose_to (matrix, out)RS.Math.Vector4

Transforms this vector by multiplying the rotation portion of the tranposed provided matrix on the right hand side and copies the result into the out vector. This vector is not modified.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

`out` RS.Math.Vector4 optional

The vector to receive the result. If not provided a new vector is created.

##### Returns:
Type Description
RS.Math.Vector4 the transformed vector.

#### scale (scale)RS.Math.Vector4

Uniformly scales this vector.

Name Type Description
`scale` Number

the scaling factor

##### Returns:
Type Description
RS.Math.Vector4 this
##### Example
``````const vec = new RS.Math.Vector4(1,2,3);
vec.scale(2);
// vec is now {x:2,y:4,z:6,w:1}``````

#### set (source)

Sets this vector. The source may be of the following types:

• RS.Math.Vector4
• an `Array` with 3 or more members
• an `Object`.
• individual arguments for `x`, `y`, `z` and `w`

In the case of an object being supplied it should have the members `x`, `y`, `z`, and optionally `w`. If `w` is omitted or parses as `NaN` then `w` will be set to `1`. Parsing failures on `x`, `y` or `z` will set them to `0`.

Name Type Description
`source` RS.Math.Vector4 | Array | Object | Number

the object to set from or a set of numbers.

##### Example
``````const v = new RS.Math.Vector4();
v.set(1,2,3);
v.set([0.2,-0.3,0.5]);
v.set({x: 0.1, y: 0.53, z: -0.2});``````

#### subtract (rhs)RS.Math.Vector4

Subtracts another vector from this vector.

Name Type Description
`rhs` RS.Math.Vector4 | RS.Math.Vector3

the other vector

##### Returns:
Type Description
RS.Math.Vector4 this
##### Example
``````const vec = new RS.Math.Vector4(1,2,3);
vec.subtract(new RS.Math.Vector4(7,-3,0));
// vec is now {x:-6,y:5,z:3,w:1}``````

#### toString ()String

Returns a string describing this Object.

##### Returns:
Type Description
String A String describing this Object.

#### transform (matrix)RS.Math.Vector4

Transforms this vector by multiplying the provided matrix on the right hand side.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

##### Returns:
Type Description
RS.Math.Vector4 this

#### transform_to (matrix, out)RS.Math.Vector4

Transforms this vector by multiplying the provided matrix on the right hand side and copies the result into the out vector. This vector is not modified.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

`out` RS.Math.Vector4 optional

The vector to receive the result. If not provided a new vector is created.

##### Returns:
Type Description
RS.Math.Vector4 the transformed vector.

#### transform_transpose (matrix)RS.Math.Vector4

Transforms this vector by multiplying the transpose of the provided matrix on the right hand side.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

##### Returns:
Type Description
RS.Math.Vector4 this

#### transform_transpose_to (matrix, out)RS.Math.Vector4

Transforms this vector by multiplying the transpose of the provided matrix on the right hand side and copies the result into the out vector. This vector is not modified.

Name Type Description
`matrix` RS.Math.Matrix4x4

The matrix to transform the vector by.

`out` RS.Math.Vector4 optional

The vector to receive the result. If not provided a new vector is created.

##### Returns:
Type Description
RS.Math.Vector4 the transformed vector.