Deleted the old Orbit.
[Orbitizer.git] /
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
#!/usr/bin/python3

from numbers import Number
from math import acos

__all__ = ['Vector3D', 'iVector', 'jVector', 'kVector']

class Vector3D:
	def __init__(self, i=0, j=0, k=0):
		self._i = i
		self._j = j
		self._k = k
	
	@classmethod
	def CrossProduct(cls, uVector, vVector):
# 		print(uVector, vVector)
		i = uVector._j * vVector._k - uVector._k * vVector._j
# 		print(uVector._j * vVector._k, uVector._k * vVector._j, i)
		j = uVector._k * vVector._i - uVector._i * vVector._k
# 		print(uVector._k * vVector._i, uVector._i * vVector._k, j)
		k = uVector._i * vVector._j - uVector._j * vVector._i
# 		print(uVector._i * vVector._j, uVector._j * vVector._i, k)
		return cls(i, j, k)
	
	def LeftCross(self, vVector):
		return self.CrossProduct(self, vVector)
	
	def RightCross(self, uVector):
		return self.CrossProduct(uVector, self)
	
	@classmethod
	def DotProduct(cls, uVector, vVector):
		return uVector._i * vVector._i + uVector._j * vVector._j + uVector._k * vVector._k
	
	def Dot(self, vVector):
		return self.DotProduct(self, vVector)
	
	def ScalarMultiply(self, Scalar):
		return Vector3D(self._i * Scalar, self._j * Scalar, self._k * Scalar)
	
	def DivideByScalar(self, Scalar):
		return self.ScalarMultiply(1. / Scalar)
	
	def Magnitude(self):
		return (self._i ** 2. + self._j ** 2. + self._k ** 2.) ** .5
	
	def UnitVector(self):
		return self / self.Magnitude()
	
	def AngleTo(self, vVector):
		Theta = acos(self.Dot(vVector) / (self.Magnitude() * vVector.Magnitude()))
		return Theta
	
	def AddVector(self, vVector):
		return Vector3D(self._i + vVector._i, self._j + vVector._j, self._k + vVector._k)
	
	def SubtractVector(self, vVector):
		return self.AddVector(-vVector)
	
	def Negate(self):
		return Vector3D(-self._i, -self._j, -self._k)
	
	def ConvertToFrame(self, iVector1, jVector1, kVector1):
		if abs(iVector1.Magnitude() - 1.) > .0001:
			iVector1 = iVector1.UnitVector()
		if abs(jVector1.Magnitude() - 1.) > .0001:
			jVector1 = jVector1.UnitVector()
		if abs(kVector1.Magnitude() - 1.) > .0001:
			kVector1 = kVector1.UnitVector()
		
		iPart = self.Dot(iVector1)
		jPart = self.Dot(jVector1)
		kPart = self.Dot(kVector1)
		
		return Vector3D(iPart, jPart, kPart)
	
	def GetTuple(self):
		return (self._i, self._j, self._k)
	
	def __neg__(self):
		return self.Negate()
	
	def __mul__(self, vVector):
		if isinstance(vVector, Vector3D):
			return self.LeftCross(vVector)
		if isinstance(vVector, Number):
			return self.ScalarMultiply(vVector)
		
		return NotImplemented
	
	def __rmul__(self, uVector):
		if isinstance(uVector, Vector3D):
			return self.RightCross(uVector)
		if isinstance(uVector, Number):
			return self.ScalarMultiply(uVector)
		
		return NotImplemented
	
	def __add__(self, vVector):
		if isinstance(vVector, Vector3D):
			return self.AddVector(vVector)
		
		return NotImplemented
	
	def __sub__(self, vVector):
		if isinstance(vVector, Vector3D):
			return self.SubtractVector(vVector)
		
		return NotImplemented
	
	def __truediv__(self, Scalar):
		if isinstance(Scalar, Number):
			return self.DivideByScalar(Scalar)
		
		return NotImplemented
	
	def __eq__(self, vVector):
		if isinstance(vVector, Vector3D):
			return (self._i == vVector._i) and (self._j == vVector._j) and (self._k == vVector._k)
		
		return NotImplemented
	
	def __neq__(self, vVector):
		return not self.__eq__(vVector) 
	
	def __round__(self, ndigits):
		return Vector3D(round(self._i, ndigits), round(self._j, ndigits), round(self._j, ndigits))
	
	def __format__(self, FormatSpec):
		return("[{0}i, {1}j, {2}k]".format(self._i.__format__(FormatSpec), self._j.__format__(FormatSpec), self._k.__format__(FormatSpec)))
	
	def __str__(self):
		return self.__format__("")
	
	def __repr__(self):
		return("Vector3D({0}, {1}, {2})".format(self._i, self._j, self._k))

iVector = Vector3D(1, 0, 0)
jVector = Vector3D(0, 1, 0)
kVector = Vector3D(0, 0, 1)

if __name__ == "__main__":
	from random import random
	
	uVector = Vector3D(random() * 10. - 5., random() * 10. - 5., random() * 10. - 5.)
	print("u: {0}".format(uVector))
	vVector = Vector3D(random() * 10. - 5., random() * 10. - 5., random() * 10. - 5.)
	print("v: {0}".format(vVector))
	
	print("u = repr(u) = {0}".format(repr(uVector)))
	
	print("u × v = {0}".format(uVector * vVector))
	print("v × u = {0}".format(vVector * uVector))
	
	print("u · v = {0}".format(uVector.Dot(vVector)))
	print("v · u = {0}".format(vVector.Dot(uVector)))
	
	print("|u| = {0}".format(uVector.Magnitude()))
	print("|v| = {0}".format(vVector.Magnitude()))
	
	print("û = {0}".format(uVector.UnitVector()))
	print("|û| = {0}".format(uVector.UnitVector().Magnitude()))
	
	print("Angle between u and v: {0}".format(uVector.AngleTo(vVector)))
	print("Angle between v and u: {0}".format(vVector.AngleTo(uVector)))
	
	print("u + v = {0}".format(uVector + vVector))
	print("v + u = {0}".format(vVector + uVector))
	
	print("-u = {0}".format(-uVector))
	print("u - v = {0}".format(uVector - vVector))
	print("v - u = {0}".format(vVector - uVector))
	
	a = random() * 10. - 5.
	print("a = {0}".format(a))
	
	print("u / a = {0}".format(uVector / a))