Package springbots :: Module vector
[hide private]

Source Code for Module springbots.vector

  1  """ 
  2  This module implements the Vector object. Which has several of 
  3  2D vector features such dot product, adding, project and angle. 
  4  It also overloads many commom math operator 
  5  """ 
  6   
  7  import math 
  8   
9 -def _is_numeric(obj):
10 """ 11 Finds out if a variable is a numeric one 12 """ 13 if isinstance(obj, (int, long, float)): 14 return True 15 else: 16 return False
17
18 -class Vector(object):
19 """ 20 Vector class: Objects of this class have several of 21 2D vector implementations such dot product, adding, project and angle. 22 It also overloads many commom math operator 23 """ 24
25 - def __init__(self, a=0, b=0 ):
26 """ 27 Creates a new vector 28 """ 29 if _is_numeric(a): 30 #assume two numbers 31 self.x = a 32 self.y = b 33 else: 34 #assume Vectors/tuples 35 self.x = b[0] - a[0] 36 self.y = b[1] - a[1]
37
38 - def __getitem__(self, index):
39 if index == 0: 40 return self.x 41 elif index == 1: 42 return self.y 43 else: 44 raise IndexError
45
46 - def __add__(self, other):
47 return Vector(self.x + other.x, self.y + other.y)
48
49 - def __sub__(self, other):
50 return Vector(self.x - other.x, self.y - other.y)
51
52 - def __mul__(self, other):
53 try: 54 other = other - 0 55 except: 56 raise TypeError, "Only scalar multiplication is supported." 57 return Vector( other * self.x, other * self.y )
58
59 - def __rmul__(self, other):
60 return self.__mul__(other)
61
62 - def __div__(self, other):
63 return Vector( self.x / other, self.y / other )
64
65 - def __neg__(self):
66 return Vector(-self.x, -self.y)
67
68 - def __abs__(self):
69 return self.length()
70
71 - def __repr__(self):
72 return '(%s, %s)' % (self.x, self.y)
73
74 - def __str__(self):
75 return '(%s, %s)' % (self.x, self.y)
76
77 - def __pow__(self, y, z=None):
78 return Vector(self.x.__pow__(y,z), self.y.__pow__(y,z))
79
80 - def dot(self, vector):
81 """ 82 Return the dot product of two vectors 83 """ 84 return self.x * vector.x + self.y * vector.y
85
86 - def cross(self, vector):
87 """ 88 Return the cross product of two vectors 89 """ 90 return self.x * vector.y - self.y * vector.x
91
92 - def length(self):
93 """ 94 Return the vector's length 95 """ 96 return math.sqrt( self.dot(self) )
97
98 - def perpendicular(self):
99 """ 100 Return the vector that is perpenticular of this 101 """ 102 return Vector(-self.y, self.x)
103
104 - def unit(self):
105 """ 106 Return the vector's unit(length one) 107 """ 108 return self / self.length()
109
110 - def projection(self, vector):
111 """ 112 Return the projection of this vector on another 113 """ 114 return self.dot(vector.unit()) * vector.unit()
115
116 - def angle(self, vector=None):
117 """ 118 Return the angle created by this vector and another of the vector 119 (1,0) if given None 120 """ 121 if vector == None: 122 vector = Vector(1,0) 123 return math.acos((self.dot(vector))/(self.length() * vector.length()))
124
125 - def angle_in_degrees(self, vector=None):
126 """ 127 Return the angle created by this vector and another of the vector 128 (1,0) if given None. Angle in degrees 129 """ 130 return (self.angle(vector) * 180) /math.pi
131