migrated code to public repository
This commit is contained in:
parent
a7df82d7a4
commit
f34dc653e5
233 changed files with 16279 additions and 186 deletions
66
code/vector.py
Normal file
66
code/vector.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
from math import sqrt
|
||||
from numpy import array
|
||||
|
||||
class Vector:
|
||||
x, y, z = (0.0, 0.0, 0.0)
|
||||
|
||||
def __init__(self, *xyz):
|
||||
if len(xyz) == 1:
|
||||
xyz = xyz[0]
|
||||
if isinstance(xyz, Vector):
|
||||
self.x, self.y, self.z = xyz.x, xyz.y, xyz.z
|
||||
else:
|
||||
if len(xyz) == 2: xyz = xyz[0], xyz[1], 0
|
||||
self.x, self.y, self.z = xyz
|
||||
self.__update__()
|
||||
|
||||
def __update__(self):
|
||||
self.mag = sqrt(self.x**2 + self.y**2 + self.z**2)
|
||||
|
||||
def norm(self):
|
||||
return Vector(self.x/self.mag, self.y/self.mag, self.z/self.mag)
|
||||
|
||||
def dot(self, other):
|
||||
return self.x*other.x + self.y*other.y + self.z*other.z
|
||||
|
||||
def cross(self, other):
|
||||
return Vector(self.y*other.z - self.z*other.y,
|
||||
self.z*other.x - self.x*other.z,
|
||||
self.x*other.y - self.y*other.x)
|
||||
|
||||
def tuple(self):
|
||||
return self.x, self.y, self.z
|
||||
def __array__(self):
|
||||
return array(self.tuple())
|
||||
|
||||
def __radd__(self, other):
|
||||
return self.__add__(other)
|
||||
def __add__(self, other):
|
||||
return Vector(self.x + other.x, self.y + other.y, self.z + other.z)
|
||||
def __rsub__(self, other):
|
||||
return self.__sub__(other)
|
||||
def __sub__(self, other):
|
||||
return Vector(self.x - other.x, self.y - other.y, self.z - other.z)
|
||||
|
||||
def __rtruediv__(self, scalar):
|
||||
return self.__div__(scalar)
|
||||
def __rfloordiv__(self, scalar):
|
||||
return self.__div__(scalar)
|
||||
def __truediv__(self, scalar):
|
||||
return self.__div__(scalar)
|
||||
def __floordiv__(self, scalar):
|
||||
return self.__div__(scalar)
|
||||
def __div__(self, scalar):
|
||||
scalar = float(scalar)
|
||||
return Vector(self.x/scalar, self.y/scalar, self.z/scalar)
|
||||
|
||||
def __mul__(self, scalar):
|
||||
return self.__rmul__(scalar)
|
||||
def __rmul__(self, scalar):
|
||||
return Vector(self.x*scalar, self.y*scalar, self.z*scalar)
|
||||
|
||||
def __neg__(self):
|
||||
return self*-1
|
||||
|
||||
def __repr__(self):
|
||||
return '({0}, {1}, {2})'.format(self.x, self.y, self.z)
|
Loading…
Add table
Add a link
Reference in a new issue