Skip to content
Laurence Armstrong edited this page Jul 27, 2015 · 2 revisions

Vectors


Constructor

v = Vector(1, 2, 3, 4)

Functions

Vector components can be easily accessed and manipulated with the x, y, z and w functions. x, y, z and w are mapped to the first 4 components of the vector, provided they exist. Providing a value to the function will set the variable instead of returning it.

v = Vector(1, 2, 3, 4)

v.x()
>>>1

v.y(3)
>>>v = [1, 3, 3, 4]

reset()

Set all vector elements to 0

add(other)

Returns two vectors added.

v = Vector(1, 2, 3, 4)
u = Vector(2, 3, 4, 5)

v.add(u)
# OR
v + u

>>>[3, 5, 7, 9]

subtract(other)

Returns two vectors subtracted.

v = Vector(1, 2, 3, 4)
u = Vector(2, 3, 4, 5)

v.subtract(u)
# OR
v - u

>>>[-1, 1, -1, -1]

scale(const)

Returns the vector scaled by const

v = Vector(1, 2, 3, 4)

v.scale(3)
# OR
v * 3

>>>[3, 6, 9, 12]

dot(other)

Returns dot product of two vectors.

v = Vector(1, 2, 3, 4)
u = Vector(2, 3, 4, 5)

v.dot(u)
# OR
v * u

>>>40

magnitude()

Returns the magnitude of the current vector.

v = Vector(1, 2, 3)

v.magnitude()
# OR
abs(v)

>>>3.741...

normalize()

Returns a vector of the same direction, but magnitude of 1.

v = Vector(1, 2, 3)

v.normalize()

>>>[0.26..., 0.534..., 0.801...]

angle(other)

Returns the angle between two vectors (in radians).

v = Vector(-1, 1)
u = Vector(1, 1)

v.angle(u)

>>>1.5708 (2pi)

cross(other)

Returns the cross product of two vectors (3D). The cross product being the vector (one of two) perpendicular to both initial vectors

v = Vector(1, 0, 0)
u = Vector(0, 1, 0)

v.cross(u)

>>>[0, 0, 1]

You can also append a component to the end of a vector or extend is by a list of components (same as list manipulation).