An optimised lightweight module, containing the Vector
class for 2D vectors, written in Cython.
No additional modules required.
Just import CyVector and use the Vector
class
Note: Changing the
CyVector.pyd
file name will break the module. If you want to change its name, you'll have to recompile it.
Returns the x and y values of the 2D vector.
Calculates and returns the magnitude.
Note: The first time
magnitude
is called, the magnitude is calculated, saved in_magVal
and returned. Subsequentmagnitude
calls will return_magVal
, unless thex
ory
value is changed, whereby the nextmagnitude
call will recalcuate, save and return the new magnitude value again.
Constructs and returns the unit vector.
Note: Similar to
magnitude
, first timeunitVec
is called, the unit vector is calculated, saved in_unitVecVal
and returned; unlessx
ory
is changed.
Constructs and returns a new Vector
with the same x
, y
values, and same _magVal
and _unitVecVal
values (if they've been calculated).
Let vec
and other
be Vector
instances,
and k
be a int
or float
:
return Vector(- vec._x, - vec._y)
return vec.copy
Vector addition.
Vector subtraction.
Scalar division.
Returns the dot product.
Note:
vec *= other
raisesTypeError
Scalar multiplication.
Let vec
and other
be Vector
instances:
vec[0]
gets and setsvec.x
vec[1]
gets and setsvec.y
vec[i]
, where i > 1; raisesIndexError
len(vec)
will always return 2
.
Sets vec.x
and vec.y
to newX
and newY
respectively.
vec == other
returns True
if vec.x == other.x
and vec.y == other.y
Returns "<{x}, {y}>"
, where {x}
and {y}
are the instance's x and y values.
Returns:
<{x}, {y}>
Magnitude: {magnitude}
Unit Vector: {unitVector}
where {x}
and {y}
are the instance's x and y values;
if the magnitude has been calculated prior, {magnitude}
is the magnitude value, else <Not yet calculated>
is displayed;
similarly, {unitVector}
is the unit vector or <Not yet calculated>
.
Null vectors (ie. vector: <0, 0>) will return the string:
<0, 0>
Magnitude: 0
Unit Vector: <Undefined>
-
- vec
should returnVector
output
, withoutput._magVal = vec._magVal
andoutput._unitVecVal = new Vector(-vec._unitVecVal.x, -vec._unitVecVal.y)
- Add
.toNumpyArray()
- Add
list(vec)
andtuple(vec)
- Create optimised list of
Vectors
; and compare efficiency against 2D numpy array