-
Notifications
You must be signed in to change notification settings - Fork 1
Vectors
v = Vector(1, 2, 3, 4)
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]
Set all vector elements to 0
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]
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]
Returns the vector scaled by const
v = Vector(1, 2, 3, 4)
v.scale(3)
# OR
v * 3
>>>[3, 6, 9, 12]
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
Returns the magnitude of the current vector.
v = Vector(1, 2, 3)
v.magnitude()
# OR
abs(v)
>>>3.741...
Returns a vector of the same direction, but magnitude of 1.
v = Vector(1, 2, 3)
v.normalize()
>>>[0.26..., 0.534..., 0.801...]
Returns the angle between two vectors (in radians).
v = Vector(-1, 1)
u = Vector(1, 1)
v.angle(u)
>>>1.5708 (2pi)
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).