Vector2

import introcs

Vectors have magnitude and direction, but they do not have position. Use the class Point2 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.

Constructor

class introcs.Vector2(x=0, y=0)

An instance is a vector in 2D space.

Variables
  • x (float) – The x-coordinate

  • y (float) – The y-coordinate

All values are 0.0 by default.

Attributes

Vector2.x

The x coordinate

Invariant: Value must be an int or float.

Vector2.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.

Vector2.toPoint()
Returns

The Point2 object equivalent to this vector

Return type

Point2

Vector2.length()

Computes the magnitude of this vector.

Returns

the length of this vector.

Return type

float

Vector2.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

Vector2.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

Vector2.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

Vector2.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)

Vector2.rotation(angle)

Rotates this vector by the angle (in radians) around the origin, producing a new object

The rotation angle is given in degrees, not radians. Rotation is counterclockwise around the z-axis.

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

angle (int or float) – angle of rotation in degrees

Returns

The rotation of this vector by angle

Return type

type(self)

Vector2.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 (Vector2) – object to interpolate with

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

Returns

the interpolation of this object and other via alpha.

Return type

Vector2

Vector2.dot(other)

Computes the dot project of this vector with other

Parameters

other (Vector2) – value to dot

Returns

the dot product between this vector and other.

Return type

float

Vector2.cross(other)

Computes the cross project of this vector with other

In two dimensions, the value is the magnitude of the z-axis.

Parameters

other (Vector2) – value to cross

Returns

the cross product between this vector and other.

Return type

float

Vector2.perp()

Computes a vector perpendicular to this one.

The resulting vector is rotated 90 degrees counterclockwise.

Returns

a 2D vector perpendicular to this one

Return type

type(self)

Vector2.rperp()

Computes a vector perpendicular to this one.

The resulting vector is rotated 90 degrees clockwise.

Returns

a 2D vector perpendicular to this one

Return type

type(self)

Vector2.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 (Vector2) – value to project on to

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

Vector2.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 (Vector2) – object to interpolate with

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

Returns

the interpolation of this object and other via alpha.

Return type

Vector2

Vector2.copy()
Returns

A copy of this point

Return type

Vector2

Vector2.list()
Returns

A python list with the contents of this point.

Return type

list

Mutable Methods

Mutable methods modify the underlying object.

Vector2.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

Vector2.rotate(angle)

Rotates this vector by the angle (in radians) around the origin in place

The rotation angle is given in degrees, not radians. Rotation is counterclockwise around the z-axis.

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

Parameters

angle (int or float) – angle of rotation in degrees

Returns

This object, newly modified

Vector2.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 (Vector2) – value to project on to

Returns

This object, newly modified

Vector2.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 (Vector2) – 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)
Vector2.__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

Vector2.__lt__(other)

Compares the lexicographic ordering of self and other.

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

Parameters

other (Vector2) – The object to check

Returns

True if self is lexicographic kess than other

Return type

float

Vector2.__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 (Point2 or Vector2) – object to add

Returns

the sum of this object and other.

Return type

Point2 or Vector2

Vector2.__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 (Point2 or Vector2) – object to subtract

Returns

the difference of this object and other.

Return type

Point2 or Vector2

Vector2.__mul__(value)

Multiples this object by a scalar, Vector2, 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, Vector2 or Matrix) – value to multiply by

Returns

the altered object

Return type

Vector2

Vector2.__rmul__(value)

Multiplies this object by a scalar or Vector2 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 Vector2) – The value to multiply by

Returns

the scalar multiple of self and scalar

Return type

Vector2

Vector2.__truediv__(value)

Divides this object by a scalar or a Vector2 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 Vector2, 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 Vector2) – The value to multiply by

Returns

the division of self by value

Return type

Vector2

Vector2.__rtruediv__(value)

Divides a scalar or Vector2 by this object.

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

Parameters

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

Returns

the division of value by self

Return type