|
| 1 | +""" |
| 2 | +A 2-dimensional vector class |
| 3 | +
|
| 4 | + >>> v1 = Vector2d(3, 4) |
| 5 | + >>> print(v1.x, v1.y) |
| 6 | + 3.0 4.0 |
| 7 | + >>> x, y = v1 |
| 8 | + >>> x, y |
| 9 | + (3.0, 4.0) |
| 10 | + >>> v1 |
| 11 | + Vector2d(3.0, 4.0) |
| 12 | + >>> v1_clone = eval(repr(v1)) |
| 13 | + >>> v1 == v1_clone |
| 14 | + True |
| 15 | + >>> print(v1) |
| 16 | + (3.0, 4.0) |
| 17 | + >>> octets = bytes(v1) |
| 18 | + >>> octets |
| 19 | + b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@' |
| 20 | + >>> abs(v1) |
| 21 | + 5.0 |
| 22 | + >>> bool(v1), bool(Vector2d(0, 0)) |
| 23 | + (True, False) |
| 24 | +
|
| 25 | +
|
| 26 | +Test of ``.frombytes()`` class method: |
| 27 | +
|
| 28 | + >>> v1_clone = Vector2d.frombytes(bytes(v1)) |
| 29 | + >>> v1_clone |
| 30 | + Vector2d(3.0, 4.0) |
| 31 | + >>> v1 == v1_clone |
| 32 | + True |
| 33 | +
|
| 34 | +
|
| 35 | +Tests of ``format()`` with Cartesian coordinates: |
| 36 | +
|
| 37 | + >>> format(v1) |
| 38 | + '(3.0, 4.0)' |
| 39 | + >>> format(v1, '.2f') |
| 40 | + '(3.00, 4.00)' |
| 41 | + >>> format(v1, '.3e') |
| 42 | + '(3.000e+00, 4.000e+00)' |
| 43 | +
|
| 44 | +
|
| 45 | +Tests of the ``angle`` method:: |
| 46 | +
|
| 47 | + >>> Vector2d(0, 0).angle() |
| 48 | + 0.0 |
| 49 | + >>> Vector2d(1, 0).angle() |
| 50 | + 0.0 |
| 51 | + >>> epsilon = 10**-8 |
| 52 | + >>> abs(Vector2d(0, 1).angle() - math.pi/2) < epsilon |
| 53 | + True |
| 54 | + >>> abs(Vector2d(1, 1).angle() - math.pi/4) < epsilon |
| 55 | + True |
| 56 | +
|
| 57 | +
|
| 58 | +Tests of ``format()`` with polar coordinates: |
| 59 | +
|
| 60 | + >>> format(Vector2d(1, 1), 'p') # doctest:+ELLIPSIS |
| 61 | + '<1.414213..., 0.785398...>' |
| 62 | + >>> format(Vector2d(1, 1), '.3ep') |
| 63 | + '<1.414e+00, 7.854e-01>' |
| 64 | + >>> format(Vector2d(1, 1), '0.5fp') |
| 65 | + '<1.41421, 0.78540>' |
| 66 | +
|
| 67 | +""" |
| 68 | + |
| 69 | +# BEGIN VECTOR2D_V2 |
| 70 | +from array import array |
| 71 | +import math |
| 72 | + |
| 73 | + |
| 74 | +class Vector2d: |
| 75 | + typecode = 'd' |
| 76 | + |
| 77 | + def __init__(self, x, y): |
| 78 | + self.x = float(x) |
| 79 | + self.y = float(y) |
| 80 | + |
| 81 | + def __iter__(self): |
| 82 | + return (i for i in (self.x, self.y)) |
| 83 | + |
| 84 | + def __repr__(self): |
| 85 | + class_name = type(self).__name__ |
| 86 | + return '{}({!r}, {!r})'.format(class_name, *self) |
| 87 | + |
| 88 | + def __str__(self): |
| 89 | + return str(tuple(self)) |
| 90 | + |
| 91 | + def __bytes__(self): |
| 92 | + return (bytes([ord(self.typecode)]) + |
| 93 | + bytes(array(self.typecode, self))) |
| 94 | + |
| 95 | + def __eq__(self, other): |
| 96 | + return tuple(self) == tuple(other) |
| 97 | + |
| 98 | + def __abs__(self): |
| 99 | + return math.hypot(self.x, self.y) |
| 100 | + |
| 101 | + def __bool__(self): |
| 102 | + return bool(abs(self)) |
| 103 | + |
| 104 | + def angle(self): |
| 105 | + return math.atan2(self.y, self.x) |
| 106 | + |
| 107 | + def __format__(self, fmt_spec=''): |
| 108 | + if fmt_spec.endswith('p'): |
| 109 | + fmt_spec = fmt_spec[:-1] |
| 110 | + coords = (abs(self), self.angle()) |
| 111 | + outer_fmt = '<{}, {}>' |
| 112 | + else: |
| 113 | + coords = self |
| 114 | + outer_fmt = '({}, {})' |
| 115 | + components = (format(c, fmt_spec) for c in coords) |
| 116 | + return outer_fmt.format(*components) |
| 117 | + |
| 118 | + @classmethod |
| 119 | + def frombytes(cls, octets): |
| 120 | + typecode = chr(octets[0]) |
| 121 | + memv = memoryview(octets[1:]).cast(typecode) |
| 122 | + return cls(*memv) |
| 123 | +# END VECTOR2D_V2 |
0 commit comments