1 """Meat Engine Vector Library
2
3 Provides vectors of 2 and 3 dimensions
4 """
5 import math
6
7
8
11 """Vector constructor"""
12 self.x=x
13 self.y=y
14
16 """Vector Addiction"""
17 return Vec2f(self.x+v.x,
18 self.y+v.y)
19
21 """Vector Subtraction
22
23 returns self-v"""
24 return Vec2f(self.x-v.x,
25 self.y-v.y)
26
28 """Scalar Multiplication
29
30 returns self*s (scalar multiplication)
31 """
32
33 return Vec2f(self.x*s,
34 self.y*s)
35
37 """Dot product."""
38 return self.x*v.x+self.y*v.y
39
41 """length
42
43 return the magnitude(length) of the vector.
44 """
45 return math.sqrt(self.magSqr())
46 pass
47
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
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
66 return "[%0.3f %0.3f]"%(self.x, self.y)
67
68
70 return self.sub(v1).mag()<=epsilon
71
72
73
74
75
78 """Vector constructor"""
79 self.x=x
80 self.y=y
81 self.z=z
82
84 """Vector Addiction"""
85 return Vec3f(self.x+v.x,
86 self.y+v.y,
87 self.z+v.z)
88
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
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
108 """Dot product."""
109 return self.x*v.x+self.y*v.y+self.z*v.z
110
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
122 """length
123
124 return the magnitude(length) of the vector.
125 """
126 return math.sqrt(self.magSqr())
127 pass
128
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
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
149 return "[%0.3f %0.3f %0.3f]"%(self.x, self.y, self.z)
150