4 def __init__(self, x, y, z):
18 def getRealPart(self):
26 def getComplexConjugate(self):
27 return Vector3D(self.
x.conjugate(), self.
y.conjugate(), self.
z.conjugate())
30 return self.
x.conjugate() * rhs.x + self.
y.conjugate() * rhs.y + self.
z.conjugate() * rhs.z
33 cx = self.
y * rhs.z - self.
z * rhs.y;
34 cy = self.
z * rhs.x - self.
x * rhs.z;
35 cz = self.
x * rhs.y - self.
y * rhs.x;
38 def getLengthSquared(self):
39 return self.
dot(self).real
44 def getNormalized(self):
46 return Vector3D(self.
x * factor, self.
y * factor, self.
z * factor)
50 self.
x = self.
x * factor
51 self.
y = self.
y * factor
52 self.
z = self.
z * factor
54 def getProjectionOnto(self, rhs):
55 normalized = rhs.getNormalized()
56 return normalized * normalized.dot(self)
58 def getPerpendicularTo(self, rhs):
61 def getRotatedAroundNormalizedAxis(self, axis, angle):
62 thisDotAxis = self.
dot(axis)
63 axisCrossThis = axis.cross(self)
64 projectionOntoAxis = axis * thisDotAxis
66 projectionOntoAxis + \
67 (self -projectionOntoAxis) * math.cos(angle) + \
68 axisCrossThis * math.sin(angle)
70 def rotateAroundNormalizedAxis(self, axis, angle):
76 def __eq__(self, rhs):
85 def __add__(self, rhs):
86 if not isinstance(rhs, Vector3D):
89 return Vector3D(self.
x + rhs.x, self.
y + rhs.y, self.
z + rhs.z)
91 def __sub__(self, rhs):
92 if not isinstance(rhs, Vector3D):
95 return Vector3D(self.
x - rhs.x, self.
y - rhs.y, self.
z - rhs.z)
97 def __mul__(self, rhs):
98 if isinstance(rhs, float)
or isinstance(rhs, int)
or isinstance(rhs, complex):
99 return Vector3D(self.
x * rhs, self.
y * rhs, self.
z * rhs)
101 return NotImplemented
103 def __div__(self, rhs):
104 if isinstance(rhs, float)
or isinstance(rhs, int)
or isinstance(rhs, complex):
105 return Vector3D(self.
x / rhs, self.
y / rhs, self.
z / rhs)
107 return NotImplemented
109 def __getitem__(self, index):
119 raise ValueError(
"invalid index")
122 return "(" + str(self.
x) +
", " + str(self.
y) +
", " + str(self.
z) +
")"