Generic Vector Code

For handling math-type vectors in Python.


point2d.py

A Two-Dimensional Point/Vector Class

There are surely much better implementations of this sort of thing, for various definitions of ‘better.’ This module is designed to by easily readable and portable, without having to fuss with installation/importing of modules such as numpy that would probably perform better.

class point2d.Point2d(x=0, y=0)

Creates a 2d vector, defaulting to <0,0>.

Parameters:
  • x (float) – x-coordinate (defaults to 0).
  • y (float) – y-coordinate (defaults to 0).
zero()

Set all coordinates of this point to zero.

>>> a = Point2d(3,-2)
>>> print(a)
Point2d: <3.000000000, -2.000000000>
>>> a.zero()
>>> print(a)
Point2d: <0.000000000, 0.000000000>
ntuple()

Returns the coordinates of this point in a Python tuple.

Example

>>> a = Point2d(3,-2)
>>> print(a)
Point2d: <3.000000000, -2.000000000>
>>> a.ntuple()
(3.0, -2.0)
scm(scalar)

Scalar multiplication of this vector.

Example

>>> a = Point2d(1,-2)
>>> print(a.scm(3.5))
Point2d: <3.500000000, -7.000000000>
>>> print(a.scm(-2))
Point2d: <-2.000000000, 4.000000000>
rotated_by(angle, use_deg=False)

Get this vector rotated anticlockwise.

Parameters:
  • angle (int or float) – Directed anticlockwise angle to rotate by,
  • degrees (boolean) – If True, angle is in degrees. Otherwise radians (default)

Example

>>> from math import pi
>>> a = Point2d(2,-2)
>>> print(a.rotated_by(pi))
Point2d: <-2.000000000, 2.000000000>
>>> print(a.rotated_by(-pi/3))
Point2d: <-0.732050808, -2.732050808>
>>> print(a.rotated_by(90,True))
Point2d: <2.000000000, 2.000000000>
norm()

Get the norm (length) of this vector.

Example

>>> Point2d(1,-2).norm()
2.23606797749979
sqnorm()

Get the squared norm (length) of this vector.

Example

>>> Point2d(1,-2).sqnorm()
5.0
unit()

Get a unit vector in the same direction as this one.

Note

Be aware of round-off errors; see the example below.

Example

>>> a = Point2d(1,-2)
>>> print(a.unit())
Point2d: <0.447213595, -0.894427191>
>>> a.unit().norm()
0.9999999999999999
normalize()

Rescale this vector to have length 1.

Note

Be aware of round-off errors; see the example below.

Example

>>> a = Point2d(1,-2)
>>> a.normalize()
>>> print(a)
Point2d: <0.447213595, -0.894427191>
>>> a.norm()
0.9999999999999999
truncate(maxlength)

Rescale this vector if needed so its length is not too large.

Parameters:maxlength (float) – Upper limit on the length. If the current length exceeds this, the vector will be rescaled.
Returns:True if rescaling was done, False otherwise.
Return type:bool

Examples

>>> a = Point2d(1,-2)
>>> a.truncate(1.0)
True
>>> print(a)
Point2d: <0.447213595, -0.894427191>
>>> b = Point2d(-1,2)
>>> b.truncate(5.0)
False
>>> print(b)
Point2d: <-1.000000000, 2.000000000>
scale_to(mag)

Change this vector’s scale to the given magnitude.

Parameters:mag (float) – New magnitude for this vector; negative will reverse direction.

Example

>>> a = Point2d(2,3)
>>> a.scale_to(-4.2)
>>> print(a)
Point2d: <-2.329740824, -3.494611236>
>>> a.norm()
4.2
angle()

Get the polar angle of this vector in radians; range (-pi,pi]

Raises:ZeroDivisionError: If called on a zero vector.

Examples

>>> Point2d(1,-2).angle()
-1.1071487177940904
>>> Point2d(1,0).angle()
0.0
>>> Point2d(0,1).angle()
1.5707963267948966
>>> Point2d(-1,0).angle()
3.141592653589793
>>> Point2d(-2,-2).angle()
-2.356194490192345
>>> Point2d(0,0).angle()
Traceback (most recent call last):
    ...
ZeroDivisionError: float division by zero
proj(direction)

Get the orthogonal projection of this vector onto another.

Parameters:direction (Point2d) – The vector we project onto; not required to be a unit vector.
Returns:The unique vector v2 such that self = q*v2 + v3, where v2 is in the span of direction and v2 and v3 are orthogonal.
Return type:Point2d

Example

>>> a = Point2d(2,4)
>>> b = Point2d(3,-2)
>>> print(a.proj(b))
Point2d: <-0.461538462, 0.307692308>
>>> print(b.proj(a))
Point2d: <-0.200000000, -0.400000000>

Notes

If you want both v2 and v3, use Point2d.resolve(direction) instead.

resolve(direction)

Orthogonal decomposition of this vector in a given direction.

Parameters:direction (Point2d) – The vector we project onto; not required to be a unit vector.
Returns:v2,v3 such that self = q*v2 + v3, where v2 is in the span of direction and v2 and v3 are orthogonal.
Return type:Point2d, Point2d

Example

>>> a = Point2d(2,-3)
>>> b = Point2d(1,4)
>>> print(a.resolve(b)[0])
Point2d: <-0.588235294, -2.352941176>
>>> print(a.resolve(b)[1])
Point2d: <2.588235294, -0.647058824>
>>> print(a.resolve(b)[0]+a.resolve(b)[1])
Point2d: <2.000000000, -3.000000000>
left_normal()

Returns the left-facing normal of this vector.

Example

>>> print(Point2d(1,-2).left_normal())
Point2d: <2.000000000, 1.000000000>
class point2d.RollingVectorMean(n_size=2)

Helper class for computing rolling averages.

Parameters:n_size (int) – Number of previous values to average over; must be at least 2.

Note

Experimental, will raise NotImplementedError on __init__