#### Vector Class

Realsoft 3D defines two separate vector classes for managing three and four dimensional vectors. Vector classes are built-in classes for sake of speed.Three dimensional vector class defines three attributes: x, y, z: Four dimensional vector defines attributes x, y, z and w.

##### Methods
Method:

r3Vect - constructor for 3d vector

Syntax:

v = new r3Vect()

Parameters:

x, y, z, w - floating point numbers

Returns:

v - new vector object

Description:

Creates a new vector object. If you don't pass any parameters to the constructor, a null vector is created i.e. all the vector attributes are initialized to zero.

Example:

```v = new r3Vect();
v = new r3Vect(0.1, 0,2, 0.3);
```

Method:

add - addition

Syntax:

v2 = v.add(v1);

Parameters:

v1 - vector

Returns:

v2 = result vector

Description:

This adds the given vector to the object in question and returns the result as a new vector. The operation doesn't change the attributes of the object.

Example:

```v1 = new r3Vect(0.1, 0, 0);
v2 = new r3Vect(0.2, 0, 0);
v3 = v1.add(v2);
```

Method:

fadd - vector addition

Syntax:

v.fadd(v2);

Parameters:

v2 - vector to be added

Returns:

-

Description:

Vector operation v = v+v2.

Example:

```v = new r3Vect(1, 0, 0);
v2 = new r3Vect(0.1, 0, 0);
v.fadd(v2);
```

Method:

set - set value

Syntax:

v.set(x, y, z); // r3Vect

Parameters:

x, y, z, w - floating point numbers

Returns:

-

Description:

Set new value to the vector

Example:

```v = new r3Vect();
v.set(0.1, 0.2, 1.0);
```

Method:

sub - vector subtraction

Syntax:

v3 = v.sub(v2);

Parameters:

v2 - vector to be subracted from the given object

Returns:

vector - the result of the subtraction

Description:

Returns a new vector containing the result of vector operation 'v - v2'.

Example:

```v1 = new r3Vect(1, 1, 1);
v2 = new r3Vect(0.5, 0.5, 0.5);
v3 = v2.sub(v1);
```

Method:

fsub - vector subtraction

Syntax:

v.fsub(v2);

Parameters:

v2 - vector to be subracted from the given object

Returns:

-

Description:

Subtracts given vector 'v2' from the vector in question.

Example:

```v = new r3Vect(1, 1, 1);
v2 = new r3Vect(1, 1, 1);
v.fsub(v2); // 0, 0, 0
```

Method:

dot - dot product

Syntax:

f = v.dot(v2);

Parameters:

v2 - vector

Returns:

f - floating point number

Description:

Performs dot product between 'v' and given parameter 'v2' and returns the result. The operation has no impact to the object itself

Example:

```v1 = new r3Vect(1, 0, 0);
v2 = new r3Vect(1, 1, 0);
f = v1.dot(v2);
```

Method:

cross - cross product

Syntax:

v3 = v.cross(v2);

Parameters:

v2 - vector

Returns:

v3 - result vector

Description:

Computes cross product 'v x v2' and returns the resulting vector. The operation doesn't impact the object itself

Example:

```v1 = new r3Vect(1, 0, 0);
v2 = new r3Vect(0, 1, 0);
v3 = v1.cross(v2);
```

Method:

len - lenght of vector

Syntax:

length = v.len()

Parameters:

-

Returns:

lenght - floating point number

Description:

Compute and return the lenght of the vector.

Example:

```v = new r3Vect(0, 2, 0);
l = v.len(); // l = 2;
```

Method:

norm - normalize vector

Syntax:

len = v.norm();

Parameters:

-

Returns:

len - original lenght of the vector

Description:

Normalizes the vector and returns the original lenght.

Example:

```v = new r3Vect(0.5, 0, 0);
v.norm();
```

Method:

orth - orthogonal projection

Syntax:

v3 = v.orth(v2);

Parameters:

v2 - vector

Returns:

v3 - result vector

Description:

Orthogonal projection. The resulting 'v3' vector is perpendicular to 'v' and on the same plane as both 'v' and 'v2'.

Example:

```v1 = new r3Vect(1, 0, 0);
v2 = new r3Vect(1, 1, 1);
v3 = v1.orth(v2);
```

Method:

proj - projection

Syntax:

v3 = v.proj(v2);

Parameters:

v2 - vector to be projected on 'v'

Returns:

v3 - vector

Description:

Returns vectors 'v2' projection on 'v'.

Example:

```v1 = new r3Vect(1, 0, 0);
v2 = new r3Vect(1, 1, 1);
v3 = v1.proj(v2);
```

Method:

neg - negate

Syntax:

v2 = v.neg();

Parameters:

-

Returns:

v2 - vector

Description:

Returns a vector pointing to opposite direction than the object in question.

Example:

```v = new r3Vect(1, 0, 0);
nv = v.neg();
```

Method:

fneg - negative

Syntax:

v.neg();

Parameters:

-

Returns:

-

Description:

Makes the vector negative vector i.e. inverts the direction of the vector

Example:

```v = new r3Vect(1, 0, 0);
v.fneg();
v.print(); // -1, 0, 0
```

Method:

mul - scalar multiplication

Syntax:

v2 = v.mul(f);

Parameters:

f - scalar for multiplying the vector

Returns:

v2 - result

Description:

Multiplies the vector with the given scalar value and returns the resulting new vector.

Example:

```v = new r3Vect(1, 2, 3);
v2 = v.mul(10); // v2 = 10, 20, 30
```

Method:

fmul - scalar multiplication

Syntax:

v.fmul(f);

Parameters:

f - floating point number

Returns:

-

Description:

Multiplies the vector by given scalar.

Example:

```v = new r3Vect(1, 2, 3);
v.fmul(10);
v.print(); // v = 10, 20, 30
```

Method:

div - division by scalar

Syntax:

v2 = v.div(f);

Parameters:

f - floating point number

Returns:

v2 - vector

Description:

Divides the vector by given scalar and returns a new vector holding the result.

Example:

```v = new r3Vect(1, 0, 0);
v2 = v.div(2.0);
```

Method:

fdiv - division by scalar

Syntax:

v.fdiv(f);

Parameters:

f - floating point number

Returns:

-

Description:

Divides the vector by given scalar.

Example:

```v = new r3Vect(1, 0, 0);
v.fdiv(2.0);
v.print(); // 0.5, 0, 0
```

Method:

cmul

Syntax:

v3 = v.cmul(v2);

Parameters:

v2 - vector

Returns:

v3 - vector

Description:

Component wise multiplication. Multiplies each component in 'v' by corresponding component of 'v2' and returns the result as a new vector.

Example:

```v1 = new r3Vect(1, 2, 3);
v2 = new r3Vect(1, 2, 3);
v3 = v1.cmul(v2); // v3 == 1, 4, 9
```

Method:

fcmul - vector multiplication

Syntax:

v.fcmul(v2);

Parameters:

v2 - vector

Returns:

-

Description:

Multiplies the 'v' by given vector 'v2'.

Example:

```v = new r3Vect(1, 2, 3);
v2 = new r3Vect(1, 2, 3);
v.fcmul(v2);
v.print(); // 1, 4, 9
```

Method:

cmp - compare

Syntax:

b = v.cmp(v2, tolerance);

Parameters:

v2 - vector

tolerance - tolerance

Returns:

b - boolean, true if vectors are identical within the given tolerance

Description:

Compare the given vector 'v2' agains the vector in question and return true if the vectors are identical. Otherwise returns false.

Example:

```v1 = new r3Vect(1, 1, 1);
v2 = new r3Vect(1.1, 1, 1);
v1.cmp(v2, 0.001);
```

Method:

print - print vector

Syntax:

v.print()

Parameters:

-

Returns:

-

Description:

Print out the current value of the vector.

Example:

```v.set(1, 2, 3);
v.print();
```

Method:

angle - angle to given vector

Syntax:

angle = v.angle(v2);

Parameters:

v2 - vector

Returns:

angle - angle in radians

Description:

Compute the angle between the vector in question and the given parameter 'v2'.

Note This method is not supported by 4d vector.

Example:

```v = new r3Vect(1, 0, 0);
v2 = new r3Vect(0, 1, 0);
angle = v.angle(v2);
```

Method:

dist - distance to given point

Syntax:

dist = v.dist(v2);

Parameters:

v2 - vector

Returns:

dist - distance

Description:

Measure the distance between two points specified by 'v' and 'v2'

Example:

```v = new r3Vect(1, 0, 0);
v2 = new r3Vect(2, 0, 0);
distance = v.dist(v2); // distance == 1.0
```