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:
Get the x coordinate of this vector.
Get the y coordinate of this vector.
Return the vector as a tuple.
Return the vector as a tuple.
Calculate the length of this vector.
Calculate the square of the length of this vector.
Return a unit vector parallel to this one.
Return a vector that is orthogonal to this one.
Return a vector that is orthogonal to this one and that has been normalized.
Return the angle between this vector and the positive x-axis measured in radians.
Return the positive angle between this vector and the positive x-axis measured in radians.
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.
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.
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.
Return the vector as a tuple.
set_tuple
(xy)Set the x and y coordinates of this vector.
Calculate the length of this vector.
set_magnitude
(scale)Set the magnitude of this vector.
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.
Return a vector that is orthogonal to this one.
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.
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.
Return the positive angle between this vector and the positive x-axis measured in radians.
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.
- __add__(other)
- __div__(other)
- __floordiv__(other)
- __hash__ = None
- __iadd__(other)
- __idiv__(other)
- __ifloordiv__(other)
- __imod__(other)
- __imul__(other)
- __ipow__(other)
- __isub__(other)
- __itruediv__(other)
- __mod__(other)
- __mul__(other)
- __nonzero__()
Return true is the vector is not degenerate.
- __pow__(other)
- __radd__(other)
- __rdiv__(other)
- __rfloordiv__(other)
- __rmod__(other)
- __rmul__(other)
- __rpow__(other)
- __rsub__(other)
- __rtruediv__(other)
- __sub__(other)
- __truediv__(other)
- 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.
- 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.
- 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_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_squared()[source]
Calculate the square of the length of this vector. This is slightly more efficient that finding the real length.
- 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.
- 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.
- 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.
- property radians
Return the angle between this vector and the positive x-axis measured in radians. Result will be between -pi and pi.
- rotate(angle)[source]
Rotate the given vector by an angle. Angle measured in radians counter-clockwise.
- 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.