forked from AzyCrw4282/CS1830-papaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vectors.py
115 lines (86 loc) · 2.82 KB
/
Vectors.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
import simplegui, math, random
CANVAS_WIDTH = 600
CANVAS_HEIGHT = 400
################################################################################
# Classes
# The Vector class
class Vector:
# Initialiser
def __init__(self, p=(0, 0)):
self.x = p[0]
self.y = p[1]
# Returns a string representation of the vector
def __str__(self):
return "(" + str(self.x) + "," + str(self.y) + ")"
# Tests the equality of this vector and another
def __eq__(self, other):
return self.x == other.x and self.y == other.y
# Tests the inequality of this vector and another
def __ne__(self, other):
return not self.__eq__(other)
# Returns a tuple with the point corresponding to the vector
def getP(self):
return (self.x, self.y)
# Returns a copy of the vector
def copy(self):
v = Vector()
v.x = self.x
v.y = self.y
return v
# Adds another vector to this vector
def add(self, other):
self.x += other.x
self.y += other.y
return self
def __add__(self, other):
return self.copy().add(other);
# Negates the vector (makes it point in the opposite direction)
def negate(self):
return self.mult(-1)
def __neg__(self):
return self.copy().negate()
# Subtracts another vector from this vector
def sub(self, other):
return self.add(-other)
def __sub__(self, other):
return self.copy().subtract(other)
# Multiplies the vector by a scalar
def mult(self, k):
self.x *= k
self.y *= k
return self
def __mul__(self, k):
return self.copy().multiply(k)
def __rmul__(self, k):
return self.copy().multiply(k)
# Divides the vector by a scalar
def divide(self, k):
return self.multiply(1 / k)
def __truediv__(self, k):
return self.copy().divide(k)
# Normalizes the vector
def normalize(self):
return self.divide(self.length())
# Returns a normalized version of the vector
def getNormalized(self):
return self.copy().normalize()
# Returns the dot product of this vector with another one
def dot(self, other):
return self.x * other.x + self.y * other.y
# Returns the length of the vector
def length(self):
return math.sqrt(self.x ** 2 + self.y ** 2)
# Returns the squared length of the vector
def lengthSquared(self):
return self.x ** 2 + self.y ** 2
# Reflect this vector on a normal
def reflect(self, normal):
n = normal.copy()
n.mult(2 * self.dot(normal))
self.sub(n)
return self
# Returns the angle between this vector and another one
# You will need to use the arccosine function:
# acos in the math library
def angle(self, other):
pass