Vector3

import introcs

Vectors have magnitude and direction, but they do not have position. Use the class Point3 if you want position. Vectors support basic point arithmetic via the operators. However, pay close attention to how we handle typing. For example, the adding a point to a vector produces another point (as it should). But vectors may freely convert to points and vice versa.

The name Vector is an alias for Vector3.

Constructor

class introcs.Vector3([x, [y, [z]]])

An instance is a vector in 3D space.

All values are 0.0 by default.

Parameters
  • x (int or float) – initial x value

  • y (int or float) – initial y value

  • z (int or float) – initial z value

Attributes

Vector3.x

The x coordinate

Invariant: Value must be an int or float.

Vector3.y

The y coordinate

Invariant: Value must be an int or float.

Immutable Methods

Immutable methods return a new object and do not modify the original.

Vector3.toPoint()
Returns

The Point3 object equivalent to this vector

Return type

Point3

Vector3.length()

Computes the magnitude of this vector.

Returns

the length of this vector.

Return type

float

Vector3.length2()

Computes the square of the magnitude of this vector

This method is slightly faster than length().

Returns

the square of the length of this vector.

Return type

float

Vector3.angle(other)

Computes the angle between two vectors.

The answer provided is in radians. Neither this vector nor other may be the zero vector.

Parameters

other (nonzero Vector2) – value to compare against

Return:

the angle between this vector and other.

Return type

float

Vector3.isUnit()

Determines whether or not this object is ‘close enough’ to a unit vector.

A unit vector is one that has length 1. This method uses allclose() to test whether the coordinates are “close enough”. It does not require exact equivalence.

Returns

True if this object is ‘close enough’ to a unit vector; False otherwise

Return type

bool

Vector3.normal()

Normalizes this vector, producing a new object.

The value returned has the same type as self (so if self is an instance of a subclass, it uses that object instead of the original class. The contents of this object are not altered.

Returns

the normalized version of this vector

Return type

type(self)

Vector3.interpolant(other, alpha)

Interpolates this object with another, producing a new object

The resulting value is:

alpha*self+(1-alpha)*other 

according to the rules of addition and scalar multiplication.

Parameters
  • other (Vector3) – object to interpolate with

  • alpha (int or float) – scalar to interpolate by

Returns

the interpolation of this object and other via alpha.

Return type

Vector3

Vector3.dot(other)

Computes the dot project of this vector with other

Parameters

other (Vector3) – value to dot

Returns

the dot product between this vector and other.

Return type

float

Vector3.cross(other)

Computes the cross project of this vector with other, producing a new vector

The value returned has the same type as self (so if self is an instance of a subclass, it uses that object instead of the original class. The contents of this object are not altered.

Parameters

other (Vector3) – value to cross

Returns

the cross product between this vector and other.

Return type

Vector3

Vector3.projection(other)

Computes the project of this vector on to other

The value returned has the same type as self (so if self is an instance of a subclass, it uses that object instead of the original class. The contents of this object are not altered.

Parameters

other (Vector3) – value to project on to

::return: the projection of this vector on to other. :rtype: Vector3

Vector3.interpolant(other, alpha)

Interpolates this object with another, producing a new object

The resulting value is:

alpha*self+(1-alpha)*other 

according to the rules of addition and scalar multiplication.

Parameters
  • other (Vector3) – object to interpolate with

  • alpha (int or float) – scalar to interpolate by

Returns

the interpolation of this object and other via alpha.

Return type

Vector3

Vector3.copy()
Returns

A copy of this point

Return type

Vector3

Vector3.list()
Returns

A python list with the contents of this point.

Return type

list

Mutable Methods

Mutable methods modify the underlying object.

Vector3.normalize()

Normalizes this vector in place.

This method alters the vector so that it has the same direction, but its length is now 1. The method returns this object for chaining.

Returns

This object, newly modified

Vector3.crossify(other)

Computes the cross project of this vector with other in place

This method alters the vector so it is the result of the cross product, but its length is now 1. The method returns this object for chaining.

Parameters

other (Vector3) – value to cross

Returns

This object, newly modified

Vector3.project(other)

Computes the project of this vector on to other

This method will modify the attributes of this oject. This method returns this object for chaining.

Parameters

other (Vector3) – value to project on to

Returns

This object, newly modified

Vector3.interpolate(other, alpha)

Interpolates this object with another in place

This method will modify the attributes of this oject. The new attributes will be equivalent to:

alpha*self+(1-alpha)*other 

according to the rules of addition and scalar multiplication.

This method returns this object for chaining.

Parameters
  • other (Vector3) – object to interpolate with

  • alpha (int or float) – scalar to interpolate by

Returns

This object, newly modified

Operators

Operators redefine the meaning of the basic operations. For example:: p + q is the same as p.__add__(q). This allows us to treat points like regular numbers. For the sake of brevity, we have not listed all operators – only the most important ones. The equivalences are as follows:

p == q     -->    p.__eq__(q)
p < q      -->    p.__lt__(q)
p + q      -->    p.__add__(q)
p - q      -->    p.__sub__(q)
p * q      -->    p.__mul__(q)
q * p      -->    p.__rmul__(q)
p / q      -->    p.__truediv__(q)
q / p      -->    p.__rtruediv__(q)
Vector3.__eq__(other)

Compares this point with other

This method uses allclose() to test whether the coordinates are “close enough”. It does not require exact equality for floats. Equivalence also requires type equivalence.

Parameters

other (any) – The object to check

Returns

True if self and other are equivalent

Return type

bool

Vector3.__lt__(other)

Compares the lexicographic ordering of self and other.

Lexicographic ordering checks the x-coordinate first, and then y.

Parameters

other (Vector3) – The object to check

Returns

True if self is lexicographic kess than other

Return type

float

Vector3.__add__(other)

Performs a context dependent addition of this vector and other.

If other is a vector, the result is vector addition. If it is point, the result is the head of the vector when it is anchored at this point.

Parameters

other (Point3 or Vector3) – object to add

Returns

the sum of this object and other.

Return type

Point3 or Vector3

Vector3.__sub__(other)

Performs a context dependent subtraction of this vector and other.

If other is a vector, the result is vector subtraction. If it is point, the result is the tail of the vector when it has its head at this point.

Parameters

other (Point3 or Vector3) – object to subtract

Returns

the difference of this object and other.

Return type

Point3 or Vector3

Vector3.__mul__(value)

Multiples this object by a scalar, Vector3, or a Matrix, producing a new object.

The exact effect is determined by the type of value. If value is a scalar, the result is standard scalar multiplication. If it is a point, then the result is pointwise multiplication. Finally, if is a matrix, then we use the matrix to transform the object. We treat matrix transformation as multiplication on the right to make in-place multiplication easier. See Matrix doe more

Parameters

value (int, float, Vector3 or Matrix) – value to multiply by

Returns

the altered object

Return type

Vector3

Vector3.__rmul__(value)

Multiplies this object by a scalar or Vector3 on the left.

The exact effect is determined by the type of value. If value is a scalar, the result is standard scalar multiplication. If it is a 2d tuple, then the result is pointwise multiplication. We do not allow matrix multiplication on the left.

Parameters

value (int, float, or Vector3) – The value to multiply by

Returns

the scalar multiple of self and scalar

Return type

Vector3

Vector3.__truediv__(value)

Divides this object by a scalar or a Vector3 on the right, producting a new object.

The exact effect is determined by the type of value. If value is a scalar, the result is standard scalar division. If it is a Vector3, then the result is pointwise division.

The value returned has the same type as self (so if self is an instance of a subclass, it uses that object instead of the original class. The contents of this object are not altered.

Parameters

value (int, float, or Vector3) – The value to multiply by

Returns

the division of self by value

Return type

Vector3

Vector3.__rtruediv__(value)

Divides a scalar or Vector3 by this object.

Dividing by a point means pointwise reciprocation, followed by multiplication.

Parameters

value (int, float, or Vector3) – The value to divide

Returns

the division of value by self

Return type