# The vector Object

The vector object is not a displayable object but is a powerful aid to 3D computations. Its properties are similar to vectors used in science and engineering.

vector(x,y,z)

This creates a 3D vector object with the given components x, y, and z.

Vectors can be added or subtracted from each other, or multiplied by an ordinary number. For example,

v1 = vector(1,2,3)
v2 = vector(10,20,30)
print(v1+v2) # displays <1 22 33>
print(2*v1)  # displays <2 4 6>

You can refer to individual components of a vector:

v2.x is 10, v2.y is 20, v2.z is 30

It is okay to make a vector from a vector: vector(v2) is still vector(10,20,30). This is a convenient way to make a separate copy of a vector.

Vector functions

The following functions are available for working with vectors:

mag(A) = A.mag = |A|, the magnitude of a vector

mag2(A) = A.mag2 = |A|*|A|, the vector's magnitude squared

norm(A) = A.norm() = A/|A|, a unit vector in the direction of the vector

hat(A) = A.hat = A/|A|, a unit vector in the direction of the vector; an alternative to A.norm(), based on the fact that unit vectors are customarily written in the form ĉ, with a "hat" over the vector

For convenience, norm(vec(0,0,0)) or vec(0,0,0).hat is calculated to be vec(0,0,0).

dot(A,B) = A.dot(B) = A dot B, the scalar dot product between two vectors

cross(A,B) = A.cross(B), the vector cross product between two vectors

diff_angle(A,B) = A.diff_angle(B), the angle between two vectors, in radians

proj(A,B) = A.proj(B) = dot(A,norm(B))*norm(B), the vector projection of A along B

comp(A,B) = A.comp(B) = dot(A,norm(B)), the scalar projection of A along B

A.equals(B) is True if A and B have the same components (which means that they have the same magnitude and the same direction).

vector.random() produces a vector each of whose components is a random number in the range -1 to +1

Some examples:

mag(A) # calculates length of A
mag(vector(1,1,1))  # = sqrt(3) = 1.732...
mag2(vector(1,1,1)) # = 3, the magnitude squared

It is possible to reset the magnitude or the magnitude squared of a vector:

v2.mag = 5 # sets magnitude to 5; no change in direction
v2.mag2 = 2.7 # sets squared magnitude of v2 to 2.7

You can reset the magnitude to 1 with norm():

norm(A) # A/|A|, normalized; magnitude of 1
norm(vector(1,1,1)) = vector(1,1,1)/sqrt(3)

You can also write v1.norm() or v1.hat.

You can change the direction of a vector without changing its magnitude:

v2.hat = v1 # changes the direction of v2 to that of v1
# but not the magnitude of v2

To calculate the angle between two vectors (the "difference" of the angles of the two vectors).

diff_angle(v1,v2)

You can also write v1.diff_angle(v2). For convenience, if either of the vectors has zero magnitude, the difference of the angles is calculated to be zero.

cross(A,B) or A.cross(B) gives the cross product of two vectors, a vector perpendicular to the plane defined by A and B, in a direction defined by the right-hand rule: if the fingers of the right hand bend from A toward B, the thumb points in the direction of the cross product. The magnitude of this vector is equal mag(A)*mag(B)*sin(diff_angle(A,B)).

dot(A,B) or A.dot(B) gives the dot product of two vectors, which is an ordinary number equal to mag(A)*mag(B)*cos(diff_angle(A,B)). If the two vectors are normalized, the dot product gives the cosine of the angle between the vectors, which is often useful.

Rotating a vector

There is a function for rotating a vector:

v2 = rotate(v1, angle=a, axis=vector(x,y,z))

The angle must be in radians. The default axis is (0,0,1), for a rotation in the xy plane around the z axis. You can also write v2 = v1.rotate(angle=a, axis=vector(x,y,z)). Note that v2 is a new vector; v1 remains unchanged. Unlike the case with rotating a 3D object, there is no origin for rotating a vector.

There is also a rotate capability for 3D objects. which doesn't return anything; you just write obj.rotate(...), which changes the axis of the existing object. It also rotates "up" to remain at right angles to the axis.

There are functions for converting between degrees and radians, where there are 2*pi radians in 360 degrees: