-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector2.py
More file actions
99 lines (72 loc) · 2.26 KB
/
Copy pathVector2.py
File metadata and controls
99 lines (72 loc) · 2.26 KB
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
import math
class Vector2():
def __init__(self):
self.x = 0.0
self.y = 0.0
#
###
def dot(self, vector):
return ((self.x * vector.x) + (self.y * vector.y))
###
def magnitudeSquared(self):
return self.dot(self)
###
def magnitude(self):
return math.sqrt(self.magnitudeSquared())
###
def amountProjectedOnto(self, projectOnto):
# if (projectOnto.magnitudeSquared() == 0):
# return 0
# else:
# normalizedProj = self.normalized()
# return (((self.x * projectOnto.x) + (self.y * projectOnto.y)) / projectOnto.magnitude()) / projectOnto.magnitude()
projectionMagnitude = self.projectedOnto(projectOnto).magnitude()
amountProjected = projectionMagnitude / projectOnto.magnitude()
return amountProjected
#
###
def projectedOnto(self, projectOnto):
if ((self.x == 0 and self.y == 0) or (projectOnto.x == 0 and projectOnto.y == 0)):
return Vector2()
else:
projectedVector = Vector2()
AdotB = self.dot(projectOnto)
BdotB = projectOnto.dot(projectOnto)
dotRatio = AdotB / BdotB
projectedVector.x = projectOnto.x * dotRatio
projectedVector.y = projectOnto.y * dotRatio
return projectedVector
#
###
def subtract(self, vector):
result = Vector2()
result.x = self.x - vector.x
result.y = self.y - vector.y
return result
###
def add(self, vector):
result = Vector2()
result.x = self.x + vector.x
result.y = self.y + vector.y
return result
###
def multiply(self, scalar):
result = Vector2()
result.x = self.x * scalar
result.y = self.y * scalar
return result
###
def divide(self, scalar):
result = Vector2()
result.x = self.x / scalar
result.y = self.y / scalar
return result
###
def normalized(self):
normalized = Vector2()
magnitude = self.magnitude()
normalized.x = self.x / magnitude
normalized.y = self.y / magnitude
return normalized
###
###