-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vector.py
52 lines (43 loc) · 1.14 KB
/
Vector.py
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
#Vector
from math import sin, cos, atan, hypot
# The quick way to know how big the pie is
PI = 3.141592653589793238462643383
TwoPI = PI * 2.0
HalfPI = PI * 0.5
OneAndHalfPI = PI * 1.5
# new math function
def direction(x, y):
"""Return the direction component of a vector (in radians), given
cartesian coordinates.
"""
if x > 0:
if y >= 0:
return atan(y / x)
else:
return atan(y / x) + TwoPI
elif x == 0:
if y > 0:
return HalfPI
elif y == 0:
return 0
else:
return OneAndHalfPI
else:
return atan(y / x) + PI
class Vector:
"""Store a vector in cartesian coordinates."""
def __init__(self,x,y):
f = 0.0
mag = 0.0
self.x = x
self.y = y
def add(self, b):
"""Add b to self, where b is another Vector."""
self.x += b.x
self.y += b.y
def heading(self):
"""Return the direction of the Vector in radians."""
return direction(self.x, self.y)
def mag(self):
"""Return the magnitude of the Vector."""
return hypot(self.x, self.y)