[Molby Document Home] [Contents]

Class: | Vector3D
Parent: | Object

### Description

The class Vector3D represents a three-dimensional vector with three Float numbers.

### Public Class methods

Vector3d[fx, fy, fz] → (new) Vector3D

Create a new vector3d object. Equivalent to Vector3D#new([fx, fy, fz]).

new → (new) Vector3D
new(Vector3D) → (new) Vector3D
new([fx, fy, fz]) → (new) Vector3D

Returns a new Vector3D object. In the first form, a zero vector is returned. In the second form, the given vector3d is duplicated. In the third form, a vector [fx, fy, fz] is returned.

### Public Instance methods

self * numeric → (new) Vector3D
self * val → Float

In the first form, the vector is scaled by the numeric. In the second form, the dot (inner) product of the two vectors are returned, which is equivalent to self.dot(val).

self + val → (new) Vector3D

Add two vectors element by element. Val is converted to a Vector3D if necessary.

self - val → (new) Vector3D

Subtract two vectors element by element. Val is converted to a Vector3D if necessary.

-self → (new) Vector3D

Calculate the opposite vector.

self / numeric → (new) Vector3D

The vector is scaled by the inverse of the given numeric.

self == val → boolean

Two Vector3D objects are equal if their elements are all equal. Usual caution about comparison between floating point numbers should be paid. Also consider using something like `self.length < 1e-10`.

self[index] → Float

Element Reference. Returns the element at the given index (0, 1, 2 for x, y, z component, respectively). If the index is less than 0 or more than 2, an exception is thrown.

self[index] = val

Element Assignment. Set the element at index (0, 1, 2 for x, y, z component, respectively). If index is less than 0 or more than 2, an exception is thrown.

self.cross(val) → (new) Vector3D

Calculate the cross (outer) product of the two vectors. Val is converted to a Vector3D if necessary.

self.dot(val) → Float

Calculate the dot (inner) product of the two vectors. Val is converted to a Vector3D if necessary.

See Also: Vector3D.*.

each {|item| ...}

Calls block for x, y, z elements, passing that element as a block parameter.

inspect → String

Create a readable string like "Vector3D[fx, fy, fz]".

length → Float

Calculate the Pythagorean length of the vector. Note that this method is not an alias of Vector3D#size, which returns 3.

See Also: Vector3D#length2.

length2 → Float

Calculate the square of the Pythagorean length of the vector.

See Also: Vector3D#length.

normalize → (new) Vector3D

Returns a unit vector with the same direction. Raises an exception when the vector is a zero vector.

r → Float

Alias for length

r2 → Float

Alias for length2

size → Integer

Returns 3. This method is present only to be consistent with classes like Array or Vector.

to_a → Array

Returns [self.x, self.y, self.z].

to_s → String

Alias for inspect.

x → Float

Get the x element of the vector.

x = val

Set the x element of the vector.

y → Float

Get the y element of the vector.

y = val

Set the y element of the vector.

z → Float

Get the z element of the vector.

z = val

Set the z element of the vector.