-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvector.py
More file actions
51 lines (37 loc) · 1.28 KB
/
vector.py
File metadata and controls
51 lines (37 loc) · 1.28 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
from math import sqrt
class Vector:
def __init__(self,tup=(0,0,0)):
self.x=tup[0]
self.y=tup[1]
self.z=tup[2]
def __abs__(self):
return sqrt(self.x**2+self.y**2+self.z**2)
def __add__(self, other):
return Vector((self.x+other.x,self.y+other.y,self.z+other.z))
def __sub__(self, other):
return Vector((self.x-other.x,self.y-other.y,self.z-other.z))
def __mul__(self, other):
return Vector((self.x*other,self.y*other,self.z*other))
def __truediv__(self, other):
return Vector((self.x/other,self.y/other,self.z/other))
def dot(self,other):
return self.x*other.x+self.y*other.y+self.z*other.z
def cross(self,other):
result=Vector()
result.x=self.y*other.z-self.z*other.y
result.y=self.z*other.x-self.x*other.z
result.z=self.x*other.y-self.y*other.x
return result
def normalize(self):
mag=abs(self)
self.x/=mag
self.y/=mag
self.z/=mag
def __str__(self):
return '('+str(self.x)+','+str(self.y)+','+str(self.z)+')'
def normalize(vec):
mag=abs(vec)
return Vector((vec.x/mag,vec.y/mag,vec.z/mag))
def getPerpComponent(ref,target):
refHat=normalize(ref)
return target-(refHat*refHat.dot(target))