-
Notifications
You must be signed in to change notification settings - Fork 0
/
PVector.py
185 lines (129 loc) · 4.33 KB
/
PVector.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
<https://github.com/djokjulapfe/PyCessing>
Abstract:
A Vector with Descarte's coordinates. Enables simple storage of vectors and common linear algebra operations such
as adding, dot product, cross product, rotation...
# TODO: This class is under construction.
"""
import random
from PConstants import *
from math import sin, cos, sqrt
class PVector:
def __init__(self, x=0.0, y=0.0, z=0.0):
""" Initializes vector's coordinates.
Args:
x (float): Initial x coordinate.
y (float): Initial y coordinate.
z (float): Initial z coordinate.
"""
self.x = x
self.y = y
self.z = z
def set(self, x, y, z=None):
""" Changes vector's coordinates.
Args:
x (new): Initial x coordinate.
y (new): Initial y coordinate.
z (new): Initial z coordinate.
"""
self.x = x
self.y = y
self.z = z if z is not None else self.z
def __add__(self, other):
""" Adds two vectors together.
Args:
other (PVector): The rhs operand.
Returns:
PVector: Vectors added together
"""
return PVector(self.x + other.x, self.y + other.y, self.z + other.z)
@staticmethod
def random2D(r=1.0):
""" Creates a vector of magnitude r with a random direction on the xy plane.
Args:
r (float): Magnitude of the vector.
Returns:
PVector: A random vector as described, with it's z coordinate set to 0.
"""
phi = random.uniform(0, 2 * PI)
x, y = r * cos(phi), r * sin(phi)
return PVector(x, y)
@staticmethod
def random3D(r=1.0):
""" Creates a vector of magnitude r with a random direction.
# TODO: Direction should be uniformly distributed on the sphere with size r.
Args:
r (float): Magnitude of the vector.
Returns:
PVector: A random vector as described.
"""
return None
@staticmethod
def fromAngle(phi, r=1.0):
""" Creates a vector who's angle from the x coordinate is phi.
Args:
phi (float): Angle of the new vector.
r (float): Magnitude of the new vector.
Returns:
PVector: A 2D vector with angle phi
"""
return PVector(r * cos(phi), r * sin(phi))
def magSq(self):
""" Calculates the magnitude squared of the vector, or the distance from the point the vector is pointing to,
to the origin using the Pitagora's theorem. This is much faster since it doesn't include square rooting.
Returns:
float: Vector's magnitude squared.
"""
return self.x ** 2 + self.y ** 2 + self.z ** 2
def mag(self):
""" Calculates the magnitude of the vector, or the distance from the point the vector is pointing to, to the
origin using the Pitagora's theorem.
# TODO: Add options to use a different metric (manhattan or any other).
Returns:
float: Vector's magnitude.
"""
return sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def add(self, other):
""" Adds one vector to the current one.
Args:
other (PVector): Vector that should get added.
"""
self.x += other.x
self.y += other.y
self.z += other.z
def sub(self, other):
""" Subtracts one vector from the current one.
Args:
other (PVector): Vector that should be subtracted.
"""
self.x -= other.x
self.y -= other.y
self.z -= other.z
def mult(self, scalar):
""" Multiplies the vector with a scalar.
Args:
scalar (float): Scaling factor.
"""
self.x *= scalar
self.y *= scalar
self.z *= scalar
@staticmethod
def dist(A, B):
""" Calculates the distance between two points.
Args:
A (PVector): First point
B (PVector): Second point
Returns:
float: The value |A - B|
"""
R = PVector()
R.add(A)
R.sub(B)
return R.mag()
def dot(self, other):
""" Calculates the dot product of two vectors
Args:
other (PVector):
Returns:
"""
pass