vecrec.Vector

class vecrec.Vector(x, y)[source]

Bases: object

Represent a mutable two-dimensional vector.

In particular, this class features a number of factory methods to create vectors from various inputs and a number of overloaded operators to facilitate vector arithmetic.

Public Data Attributes:

x

Get the x coordinate of this vector.

y

Get the y coordinate of this vector.

xy

Return the vector as a tuple.

tuple

Return the vector as a tuple.

magnitude

Calculate the length of this vector.

magnitude_squared

Calculate the square of the length of this vector.

unit

Return a unit vector parallel to this one.

orthogonal

Return a vector that is orthogonal to this one.

orthonormal

Return a vector that is orthogonal to this one and that has been normalized.

radians

Return the angle between this vector and the positive x-axis measured in radians.

positive_radians

Return the positive angle between this vector and the positive x-axis measured in radians.

degrees

Return the angle between this vector and the positive x-axis measured in degrees.

Public Methods:

null()

Return a null vector.

random([magnitude])

Create a unit vector pointing in a random direction.

from_radians(angle)

Create a unit vector that makes the given angle with the x-axis.

from_degrees(angle)

Create a unit vector that makes the given angle with the x-axis.

from_tuple(coordinates)

Create a vector from a two element tuple.

from_scalar(scalar)

Create a vector from a single scalar value.

from_rectangle(box)

Create a vector randomly within the given rectangle.

from_anything(input)

copy()

Return a copy of this vector.

assign(other)

Copy the given vector into this one.

normalize()

Set the magnitude of this vector to unity, in place.

scale(magnitude)

Set the magnitude of this vector in place.

interpolate(target, extent)

Move this vector towards the given towards the target by the given extent.

project(axis)

Project this vector onto the given axis.

dot_product(other)

Return the dot product of the given vectors.

perp_product(other)

Return the perp product of the given vectors.

rotate(angle)

Rotate the given vector by an angle.

round([digits])

Round the elements of the given vector to the given number of digits.

__init__(x, y)

Construct a vector using the given coordinates.

__repr__()

Return a string representation of this vector.

__str__()

Return a string representation of this vector.

__iter__()

Iterate over this vectors coordinates.

__bool__()

Return true is the vector is not degenerate.

__nonzero__()

Return true is the vector is not degenerate.

__getitem__(i)

Return the specified coordinate.

__eq__(other)

Return true if the given object has the same coordinates as this vector.

__ne__(other)

Return true if the given object has different coordinates than this vector.

__neg__()

Return a copy of this vector with the signs flipped.

__abs__()

Return the absolute value of this vector.

__add__(other)

__radd__(other)

__iadd__(other)

__sub__(other)

__rsub__(other)

__isub__(other)

__mul__(other)

__rmul__(other)

__imul__(other)

__floordiv__(other)

__rfloordiv__(other)

__ifloordiv__(other)

__truediv__(other)

__rtruediv__(other)

__itruediv__(other)

__div__(other)

__rdiv__(other)

__idiv__(other)

__mod__(other)

__rmod__(other)

__imod__(other)

__pow__(other)

__rpow__(other)

__ipow__(other)

get_x()

Get the x coordinate of this vector.

set_x(x)

Set the x coordinate of this vector.

get_y()

Get the y coordinate of this vector.

set_y(y)

Set the y coordinate of this vector.

get_xy()

Return the vector as a tuple.

set_xy(xy)

Set the x and y coordinates of this vector from a tuple.

get_tuple()

Return the vector as a tuple.

set_tuple(xy)

Set the x and y coordinates of this vector.

get_magnitude()

Calculate the length of this vector.

set_magnitude(scale)

Set the magnitude of this vector.

get_magnitude_squared()

Calculate the square of the length of this vector.

get_distance(other)

Return the Euclidean distance between the two input vectors.

get_distance_squared(other)

Return the squared Euclidean distance between the two input vectors.

get_manhattan(other)

Return the Manhattan distance between the two input vectors.

get_unit()

Return a unit vector parallel to this one.

get_orthogonal()

Return a vector that is orthogonal to this one.

get_orthonormal()

Return a vector that is orthogonal to this one and that has been normalized.

get_scaled(magnitude)

Return a unit vector parallel to this one.

get_interpolated(target, extent)

Return a new vector that has been moved towards the given target by the given extent.

get_projection(axis)

Return the projection of this vector onto the given axis.

get_components(other)

Break this vector into one vector that is perpendicular to the given vector and another that is parallel to it.

get_radians()

Return the angle between this vector and the positive x-axis measured in radians.

set_radians(angle)

Set the angle that this vector makes with the x-axis.

get_positive_radians()

Return the positive angle between this vector and the positive x-axis measured in radians.

get_degrees()

Return the angle between this vector and the positive x-axis measured in degrees.

set_degrees(angle)

Set the angle that this vector makes with the x-axis.

get_radians_to(other)

Return the angle between the two given vectors in radians.

get_degrees_to(other)

Return the angle between the two given vectors in degrees.

get_rotated(angle)

Return a vector rotated by angle from the given vector.

get_rounded(digits)

Return a vector with the elements rounded to the given number of digits.

dot(other)

Return the dot product of the given vectors.

perp(other)

Return the perp product of the given vectors.


__abs__()[source]

Return the absolute value of this vector.

__add__(other)
__bool__()[source]

Return true is the vector is not degenerate.

__div__(other)
__eq__(other)[source]

Return true if the given object has the same coordinates as this vector.

__floordiv__(other)
__getitem__(i)[source]

Return the specified coordinate.

__hash__ = None
__iadd__(other)
__idiv__(other)
__ifloordiv__(other)
__imod__(other)
__imul__(other)
__init__(x, y)[source]

Construct a vector using the given coordinates.

__ipow__(other)
__isub__(other)
__iter__()[source]

Iterate over this vectors coordinates.

__itruediv__(other)
__mod__(other)
__mul__(other)
__ne__(other)[source]

Return true if the given object has different coordinates than this vector.

__neg__()[source]

Return a copy of this vector with the signs flipped.

__nonzero__()

Return true is the vector is not degenerate.

__pow__(other)
__radd__(other)
__rdiv__(other)
__repr__()[source]

Return a string representation of this vector.

__rfloordiv__(other)
__rmod__(other)
__rmul__(other)
__rpow__(other)
__rsub__(other)
__rtruediv__(other)
__str__()[source]

Return a string representation of this vector.

__sub__(other)
__truediv__(other)
assign(other)[source]

Copy the given vector into this one.

copy()[source]

Return a copy of this vector.

property degrees

Return the angle between this vector and the positive x-axis measured in degrees.

dot(other)

Return the dot product of the given vectors.

dot_product(other)[source]

Return the dot product of the given vectors.

static from_anything(input)[source]
static from_degrees(angle)[source]

Create a unit vector that makes the given angle with the x-axis.

static from_radians(angle)[source]

Create a unit vector that makes the given angle with the x-axis.

static from_rectangle(box)[source]

Create a vector randomly within the given rectangle.

static from_scalar(scalar)[source]

Create a vector from a single scalar value.

static from_tuple(coordinates)[source]

Create a vector from a two element tuple.

get_components(other)[source]

Break this vector into one vector that is perpendicular to the given vector and another that is parallel to it.

get_degrees()[source]

Return the angle between this vector and the positive x-axis measured in degrees.

get_degrees_to(other)[source]

Return the angle between the two given vectors in degrees. If either of the inputs are null vectors, an exception is thrown.

get_distance(other)[source]

Return the Euclidean distance between the two input vectors.

get_distance_squared(other)[source]

Return the squared Euclidean distance between the two input vectors.

get_interpolated(target, extent)[source]

Return a new vector that has been moved towards the given target by the given extent. The extent should be between 0 and 1.

get_magnitude()[source]

Calculate the length of this vector.

get_magnitude_squared()[source]

Calculate the square of the length of this vector. This is slightly more efficient that finding the real length.

get_manhattan(other)[source]

Return the Manhattan distance between the two input vectors.

get_orthogonal()[source]

Return a vector that is orthogonal to this one. The resulting vector is not normalized.

get_orthonormal()[source]

Return a vector that is orthogonal to this one and that has been normalized.

get_positive_radians()[source]

Return the positive angle between this vector and the positive x-axis measured in radians.

get_projection(axis)[source]

Return the projection of this vector onto the given axis. The axis does not need to be normalized.

get_radians()[source]

Return the angle between this vector and the positive x-axis measured in radians. Result will be between -pi and pi.

get_radians_to(other)[source]

Return the angle between the two given vectors in radians. If either of the inputs are null vectors, an exception is thrown.

get_rotated(angle)[source]

Return a vector rotated by angle from the given vector. Angle measured in radians counter-clockwise.

get_rounded(digits)[source]

Return a vector with the elements rounded to the given number of digits.

get_scaled(magnitude)[source]

Return a unit vector parallel to this one.

get_tuple()[source]

Return the vector as a tuple.

get_unit()[source]

Return a unit vector parallel to this one.

get_x()[source]

Get the x coordinate of this vector.

get_xy()[source]

Return the vector as a tuple.

get_y()[source]

Get the y coordinate of this vector.

interpolate(target, extent)[source]

Move this vector towards the given towards the target by the given extent. The extent should be between 0 and 1.

property magnitude

Calculate the length of this vector.

property magnitude_squared

Calculate the square of the length of this vector. This is slightly more efficient that finding the real length.

normalize()[source]

Set the magnitude of this vector to unity, in place.

static null()[source]

Return a null vector.

property orthogonal

Return a vector that is orthogonal to this one. The resulting vector is not normalized.

property orthonormal

Return a vector that is orthogonal to this one and that has been normalized.

perp(other)

Return the perp product of the given vectors. The perp product is just a cross product where the third dimension is taken to be zero and the result is returned as a scalar.

perp_product(other)[source]

Return the perp product of the given vectors. The perp product is just a cross product where the third dimension is taken to be zero and the result is returned as a scalar.

property positive_radians

Return the positive angle between this vector and the positive x-axis measured in radians.

project(axis)[source]

Project this vector onto the given axis.

property radians

Return the angle between this vector and the positive x-axis measured in radians. Result will be between -pi and pi.

static random(magnitude=1)[source]

Create a unit vector pointing in a random direction.

rotate(angle)[source]

Rotate the given vector by an angle. Angle measured in radians counter-clockwise.

round(digits=0)[source]

Round the elements of the given vector to the given number of digits.

scale(magnitude)[source]

Set the magnitude of this vector in place.

set_degrees(angle)[source]

Set the angle that this vector makes with the x-axis.

set_magnitude(scale)[source]

Set the magnitude of this vector. This is an alias for scale().

set_radians(angle)[source]

Set the angle that this vector makes with the x-axis.

set_tuple(xy)[source]

Set the x and y coordinates of this vector.

set_x(x)[source]

Set the x coordinate of this vector.

set_xy(xy)[source]

Set the x and y coordinates of this vector from a tuple.

set_y(y)[source]

Set the y coordinate of this vector.

property tuple

Return the vector as a tuple.

property unit

Return a unit vector parallel to this one.

property x

Get the x coordinate of this vector.

property xy

Return the vector as a tuple.

property y

Get the y coordinate of this vector.