1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
# -*- coding: utf-8 -*-
import math
class Vector2(object):
def __init__(self, x=0., y=0.):
if hasattr(x, "__getitem__"):
x, y = x
self._v = [float(x), float(y)]
else:
self._v = [float(x), float(y)]
def __str__(self):
return "(%s, %s)" % (self._v[0], self._v[1])
def __getitem__(self, index):
"""Gets a component as though the vector were a list."""
try:
return self._v[index]
except IndexError:
pass
# raise IndexError, "There are 2 values in this object, index should be 0 or 1"
def __setitem__(self, index, value):
"""Sets a component as though the vector were a list."""
try:
self._v[index] = 1.0 * value
except IndexError:
pass
# raise IndexError, "There are 2 values in this object, index should be 0 or 1!"
except TypeError:
pass
# raise TypeError, "Must be a number"
@staticmethod
def from_points(P1, P2):
return Vector2(P2[0] - P1[0], P2[1] - P1[1])
@classmethod
def from_floats(cls, x, y):
vec = cls.__new__(cls, object)
vec._v = [x, y]
return vec
@classmethod
def dot_prod(cls, v1, v2):
dot = v1[0] * v2[0] + v1[1] * v2[1]
return dot
@classmethod
def scale(cls, scalar, v):
vec = cls.__new__(cls, object)
vec._v = [scalar * v[0], scalar * v[1]]
return vec
def get_magnitude(self):
return math.sqrt(self._v[0] ** 2 + self._v[1] ** 2)
def normalize(self):
magnitude = self.get_magnitude()
if magnitude != 0:
self._v[0] /= magnitude
self._v[1] /= magnitude
else:
self._v = [0., 0.]
def __iter__(self):
return iter(self._v[:])
def __len__(self):
return 2
def __add__(self, rhs):
return Vector2(self._v[0] + rhs._v[0], self._v[1] + rhs._v[1])
def __sub__(self, rhs):
return Vector2(self._v[0] - rhs._v[0], self._v[1] - rhs._v[1])
def __neg__(self):
return Vector2(-self._v[0], -self._v[1])
"""
def __mul__(self, scalar):
return Vector2(self._v[0] * scalar, self._v[1] * scalar)
"""
def __mul__(self, rhs):
"""Return the result of multiplying this vector with a scalar or a vector-list object."""
x, y = self._v
if hasattr(rhs, "__getitem__"):
xx, yy = rhs
return Vector2.from_floats(x * xx, y * yy)
else:
return Vector2.from_floats(x * rhs, y * rhs)
def __div__(self, scalar):
return Vector2(self._v[0] / scalar, self._v[1] / scalar)
"""
A = (10.0, 20.0)
B = (30.0, 35.0)
AB = Vector2.from_points(A, B)
step = AB * .1
position = Vector2(*A)
for n in range(10):
position += step
#print position
"""
|