-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.py
141 lines (94 loc) · 3.47 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# vector.py
#
import math
class Vector2(object):
def __init__(self,x,y):
self.x = float(x)
self.y = float(y)
def add(self, v):
self.x += v.x
self.y += v.y
def sub(self, v):
self.x -= v.x
self.y -= v.y
def mult(self, m):
self.x *= m
self.y *= m
def div(self, d):
self.x /= float(d)
self.y /= float(d)
def mag(self):
# the length of the vector
return math.sqrt(self.x * self.x + self.y * self.y)
def normalise(self):
m = self.mag()
if m != 0:
self.div(m)
def getCopy(self):
n = Vector2(self.x, self.y)
return n
def set(self, v):
# set this vector to the same values as a vector passed in
self.x = v.x
self.y = v.y
def setFromValues(self, x, y):
# set this vector to x y passed in
self.x = float(x)
self.y = float(y)
def setFromAngle(self, angle_degrees):
# set the vector to angle_degrees (0..360)
self.x = math.cos(math.radians(angle_degrees))
self.y = math.sin(math.radians(angle_degrees))
def rotate(self, angle_radians):
# Rotate the vector by angle_radians radians
cos = math.cos(angle_radians)
sin = math.sin(angle_radians)
x = self.x * cos - self.y * sin
y = self.x * sin + self.y * cos
self.x = x
self.y = y
def rotate_degrees(self, angle_degrees):
# rotate the vector by angle_degrees degrees
self.rotate(math.radians(angle_degrees))
def headingRadians(self):
# returns the angle in radians of the vector
m = self.mag()
if m == 0:
return 0
else:
return math.atan2(self.y, self.x)
def headingDeg180(self):
# returns the angle in degrees of the vector
# below the line it goes 0-180, above -180-0
return math.degrees(self.headingRadians())
def headingDeg360(self):
# returns the heading of the vector in angle of 0..360 degrees
angle = self.headingDeg180()
if angle < 0:
angle = angle + 360
return angle
def limit(self, minn, maxn):
self.x = self.clamp(self.x, minn, maxn)
self.y = self.clamp(self.y, minn, maxn)
def clamp(self, n, minn, maxn):
if n < minn:
return minn
elif n > maxn:
return maxn
else:
return n
def dot(self, other):
# returns dot product of this vector and other vector
return float(self.x * other.x + self.y * other.y)
def angleBetween(self, other):
# returns angle in radians between this vector and other vector
cross = self.x * other.y - self.y * other.x
dot = self.x * other.x + self.y * other.y
return math.atan2(cross, dot)
def angleBetweenDegrees180(self, other):
# returns angle in degrees between this vector and other vector
# below the line it goes 0-180, above -180-0
return math.degrees(self.angleBetween(other))