Package MeatEngine :: Package Math :: Module vector
[hide private]
[frames] | no frames]

Source Code for Module MeatEngine.Math.vector

  1  """Meat Engine Vector Library 
  2   
  3  Provides vectors of 2 and 3 dimensions 
  4  """ 
  5  import math 
  6   
  7   
  8   
9 -class Vec2f:
10 - def __init__(self, x,y):
11 """Vector constructor""" 12 self.x=x 13 self.y=y
14
15 - def add(self, v):
16 """Vector Addiction""" 17 return Vec2f(self.x+v.x, 18 self.y+v.y)
19
20 - def sub(self, v):
21 """Vector Subtraction 22 23 returns self-v""" 24 return Vec2f(self.x-v.x, 25 self.y-v.y)
26
27 - def mul(self, s):
28 """Scalar Multiplication 29 30 returns self*s (scalar multiplication) 31 """ 32 33 return Vec2f(self.x*s, 34 self.y*s)
35
36 - def dot(self, v):
37 """Dot product.""" 38 return self.x*v.x+self.y*v.y
39
40 - def mag(self):
41 """length 42 43 return the magnitude(length) of the vector. 44 """ 45 return math.sqrt(self.magSqr()) 46 pass
47
48 - def magSqr(self):
49 """Square of the magnitude. 50 51 this is faster than calling mag(), and is often just as useful. 52 """ 53 return (self.x*self.x+ 54 self.y*self.y)
55
56 - def norm(self):
57 """normalize 58 59 returns a unit vector in the same direction as this vector. 60 """ 61 m=self.mag() 62 return Vec2f(self.x/m, 63 self.y/m)
64
65 - def __str__(self):
66 return "[%0.3f %0.3f]"%(self.x, self.y)
67 68
69 - def samePoint(self, v1, epsilon):
70 return self.sub(v1).mag()<=epsilon
71 72 73 74 75
76 -class Vec3f:
77 - def __init__(self, x,y,z):
78 """Vector constructor""" 79 self.x=x 80 self.y=y 81 self.z=z
82
83 - def add(self, v):
84 """Vector Addiction""" 85 return Vec3f(self.x+v.x, 86 self.y+v.y, 87 self.z+v.z)
88
89 - def sub(self, v):
90 """Vector Subtraction 91 92 returns self-v""" 93 return Vec3f(self.x-v.x, 94 self.y-v.y, 95 self.z-v.z)
96
97 - def mul(self, s):
98 """Scalar Multiplication 99 100 returns self*s (scalar multiplication) 101 """ 102 103 return Vec3f(self.x*s, 104 self.y*s, 105 self.z*s)
106
107 - def dot(self, v):
108 """Dot product.""" 109 return self.x*v.x+self.y*v.y+self.z*v.z
110
111 - def cross(self, v):
112 """Cross product. 113 114 self cross v 115 """ 116 117 return Vec3f(self.y*v.z-self.z*v.y, 118 self.z*v.x-self.x*v.z, 119 self.x*v.y-self.y*v.x)
120
121 - def mag(self):
122 """length 123 124 return the magnitude(length) of the vector. 125 """ 126 return math.sqrt(self.magSqr()) 127 pass
128
129 - def magSqr(self):
130 """Square of the magnitude. 131 132 this is faster than calling mag(), and is often just as useful. 133 """ 134 return (self.x*self.x+ 135 self.y*self.y+ 136 self.z*self.z)
137
138 - def norm(self):
139 """normalize 140 141 returns a unit vector in the same direction as this vector. 142 """ 143 m=self.mag() 144 return Vec3f(self.x/m, 145 self.y/m, 146 self.z/m)
147
148 - def __str__(self):
149 return "[%0.3f %0.3f %0.3f]"%(self.x, self.y, self.z)
150