Commit a744f00c authored by Fabrice Salvaire's avatar Fabrice Salvaire

Vector: cleanup and implemented __rmul__

parent 539d177f
......@@ -20,6 +20,36 @@
"""Module to implement vector.
Example of usage::
v = Vector2D(10, 20)
v = Vector2D((10, 20))
v = Vector2D([10, 20])
v = Vector2D(iterable)
v = Vector2D(v)
v.x
v.y
# array interface
v[0], v[1]
iter(v)
-v
v + v
v += v
v - v
v -= v
v * 2
2 * v
v *= 2
v / 2
v /= 2
"""
####################################################################################################
......@@ -51,17 +81,6 @@ class Vector2DBase(Primitive, Primitive2DMixin):
def __init__(self, *args):
"""
Example of usage::
Vector(1, 3)
Vector((1, 3))
Vector([1, 3])
Vector(iterable)
Vector(vector)
"""
array = self._check_arguments(args)
# call __getitem__ once
......@@ -516,6 +535,12 @@ class Vector2D(Vector2DFloatBase):
##############################################
def __rmul__(self, scale):
"""Return a new vector equal to the self scaled by scale"""
return self.__mul__(scale)
##############################################
def __imul__(self, scale):
"""Scale self by scale"""
self._v *= scale
......@@ -585,6 +610,12 @@ class NormalisedVector2D(Vector2DFloatBase):
""" Return a new vector equal to the self scaled by scale """
return self.__class__(scale * self._v) # Fixme: Vector2D ?
##############################################
def __rmul__(self, scale):
""" Return a new vector equal to the self scaled by scale """
return self.__mul__(scale)
####################################################################################################
class HomogeneousVector2D(Vector2D):
......
......@@ -92,13 +92,15 @@ class TestVector2D(unittest.TestCase):
a, b, c = 10, 20, 30
# unary -
v1 = Vector2D(a, b)
v2 = Vector2D(b, c)
self.assertEqual(v1 + v2, Vector2D(a+b, b+c))
self.assertEqual(-v1, Vector2D(-a, -b))
# binary + +=
v1 = Vector2D(a, b)
v2 = Vector2D(b, c)
self.assertEqual(v1 - v2, Vector2D(-a, -a))
self.assertEqual(v1 + v2, Vector2D(a+b, b+c))
self.assertEqual(v1 - v2, Vector2D(a-b, b-c))
v1 = Vector2D(a, b)
v1 += Vector2D(b, c)
......@@ -106,18 +108,21 @@ class TestVector2D(unittest.TestCase):
v1 = Vector2D(a, b)
v1 -= Vector2D(b, c)
self.assertEqual(v1, Vector2D(-a, -a))
self.assertEqual(v1, Vector2D(a-b, b-c))
# scale *
v1 = Vector2D(a, b)
self.assertEqual(v1 * a, Vector2D(a*a, b*a))
v2 = Vector2D(a*a, b*a)
self.assertEqual(v1 * a, v2)
self.assertEqual(a * v1, v2)
v1 = Vector2D(a, b)
v1 *= a
self.assertEqual(v1, Vector2D(a*a, b*a))
self.assertEqual(v1, v2)
#?# v1 = Vector2D(a, b)
#?# v1 /= a
#?# self.assertEqual(v1, Vector2D(1, 2))
v1 = Vector2D(a, b)
v1 /= a
self.assertEqual(v1, Vector2D(1, b/a))
##############################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment