-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.py
More file actions
155 lines (116 loc) · 3.41 KB
/
Vector.py
File metadata and controls
155 lines (116 loc) · 3.41 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
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
import math
from copy import deepcopy
class Vector(object):
"""
"""
def __init__(self, array):
"""
:param array:
"""
if len(array) < 1 or not type(array) == list:
raise AttributeError("Vektor braucht mind. 1 Parameter (als Liste)")
self.array = deepcopy([float(x) for x in array])
self.dimensions = len(self.array)
def magnitude(self):
"""
:return:
"""
quadsum = 0.0
for x in self.array:
quadsum += x**2
return math.sqrt(quadsum)
def normalize(self):
"""
:return:
"""
quadsum = 0.0
mag = self.magnitude()
self.array = [x/mag for x in self.array] if mag != 0 else [0.0, 0.0, 0.0]
def normalized(self):
"""
:return:
"""
v = Vector(self.array)
v.normalize()
return v
def scale(self, t):
"""
:param t:
:return:
"""
return Vector([x * t for x in self.array])
def scaled(self, t):
"""
:param t:
:return:
"""
v = Vector(self.array)
v.scale(t)
return v
def dot(self, other):
"""
:param other:
:return:
"""
"Skalarprodukt"
# erstmal nur fuer 3 dimensionen
if isinstance(other,Vector):
return self.array[0]*other.array[0] + self.array[1]*other.array[1] + self.array[2]*other.array[2]
else:
raise TypeError("Kein Vektor")
def cross(self, other):
"""
:param other:
:return:
"""
return Vector([self.array[1] * other.array[2] - self.array[2] * other.array[1],
self.array[2] * other.array[0] - self.array[0] * other.array[2],
self.array[0] * other.array[1] - self.array[1] * other.array[0]])
def __add__(self, other):
"""
:param other:
:return:
"""
if self.dimensions != other.dimensions:
raise ArithmeticError("Vektoren müssen die gleiche Anzahl Dimensionen haben")
a = []
for i in range(len(self.array)):
a.append(self.array[i] + other.array[i])
return Vector(a)
def __sub__(self, other):
"""
:param other:
:return:
"""
if self.dimensions != other.dimensions:
raise ArithmeticError("Vektoren müssen die gleiche Anzahl Dimensionen haben")
a = []
for i in range(len(self.array)):
a.append(self.array[i] - other.array[i])
return Vector(a)
def __div__(self, other):
"""
:param other:
:return:
"""
if type(other) != float:
raise TypeError("Divisor muss float sein")
return Vector([x/other for x in self.array])
__truediv__ = __div__
def __mul__(self, other):
"""
:param other:
:return:
"""
return Vector([x*other for x in self.array])
__rmul__ =__mul__
def __neg__(self):
"""
:return:
"""
return self.scale(-1)
def __repr__(self):
"""
:return:
"""
return "Vector(%s)" % ", ".join([str(x) for x in self.array])