add todos

This commit is contained in:
Mats van Reenen 2020-07-29 10:19:30 +02:00
parent 5031e5f2b6
commit 3b1c635034

View File

@ -79,6 +79,7 @@ class Func:
return Add(self, other) return Add(self, other)
def __truediv__(self, other): # self / other def __truediv__(self, other): # self / other
return Div(self, other) return Div(self, other)
#TODO: change round to floor
def __floordiv__(self, other): # self // other def __floordiv__(self, other): # self // other
return Round(Div(self, other)) return Round(Div(self, other))
def __pow__(self, other): # self ** other def __pow__(self, other): # self ** other
@ -125,7 +126,7 @@ class Const(Func):
def copy(self): def copy(self):
return Const return Const
def simplify(self): def simplify(self): #TODO: return copy of itself
return self return self
def __str__(self): def __str__(self):
@ -145,7 +146,7 @@ class Var(Func):
def copy(self): def copy(self):
return Var return Var
def simplify(self): def simplify(self): #TODO: return copy of itself
return self return self
def __str__(self): def __str__(self):
@ -202,7 +203,7 @@ class Round(Func):
def simplify(self): def simplify(self):
f = self.function.simplify() f = self.function.simplify()
if isinstance(f, Const): if isinstance(f, Const):
return Const(self.solve({})) return Const(self.solve({})) #TODO: make use of the solved version
else: else:
return Round(f) return Round(f)
@ -261,7 +262,7 @@ class Neg(Func):
self.function = Const(function) self.function = Const(function)
else: else:
self.function = function self.function = function
def solve(self, vars): def solve(self, vars):
return -self.function.solve(vars) return -self.function.solve(vars)
@ -273,10 +274,10 @@ class Neg(Func):
if isinstance(f, Const): if isinstance(f, Const):
return Const(self.solve({})) return Const(self.solve({}))
else: else:
return Round(f) return Neg(f)
def __str__(self): def __str__(self):
return "-" + addBrackets(self.function, 3) + ")" return "-" + addBrackets(self.function, 3)
# ================================================================= # =================================================================
# == Sub ========================================================== # == Sub ==========================================================
@ -341,7 +342,7 @@ class Eql(Func):
return Eql return Eql
def simplify(self): def simplify(self):
if ( (isinstance(self.left,self.right.copy())) # same opperation if ( (isinstance(self.left, self.right.copy())) # same opperation
and (not isinstance(self.left, (Const, Var, Round))) # not const, var or Round and (not isinstance(self.left, (Const, Var, Round))) # not const, var or Round
and (self.left.right is self.right.right)): # subthing is the same and (self.left.right is self.right.right)): # subthing is the same
return self.copy()(self.left.left.simplify(), self.right.left.simplify()) return self.copy()(self.left.left.simplify(), self.right.left.simplify())
@ -599,22 +600,34 @@ class Vec:
... ...
def polar(self, *args): def polar(self, *args):
if len(args) == 2: if len(args) == 2:
# xy angle xz angle yz angle if not isinstance(args[0], (tuple, list)):
self.x = math.sin(args[0][0]) * math.sin(args[0][1]) raise ValueError('Vec.polar(): first argument must be a tuple or list. ' + str(type(args[0])) + ' given')
self.y = math.cos(args[0][0]) * math.sin(args[0][2]) if len(args[0]) < 3:
raise ValueError('Vec.polar(): first argument must be at least 3 items long')
if not isinstance(args[1], (int, float)):
raise ValueError('Vec.polar(): second argument must be a int or float. ' + str(type(args[1])) + ' given')
# xy angle xz angle yz angle
self.x = math.sin(args[0][0]) #* math.sin(args[0][1])
self.y = math.cos(args[0][0]) # * math.sin(args[0][2])
self.z = math.cos(args[0][1]) * math.cos(args[0][2]) self.z = math.cos(args[0][1]) * math.cos(args[0][2])
self = self * l self.mul(self, args[1])
elif len(args) == 0: elif len(args) == 0:
return ( return (
( (
math.tanh(self.y, self.x), math.atan2(self.y, self.x),
math.tanh(self.z, self.x), math.atan2(self.z, self.x),
0.0 0.0
), ),
self.length(), self.length(),
) )
else:
raise ValueError("Vec.polar(): Wrong number of arguments given. 2 or 0 suported")
def rotate(self, angle): def rotate(self, angle):
if not isinstance(angle, (tuple, list)):
raise ValueError('Vec.rotate(): first argument must be a tuple or list')
if len(angle) < 3:
raise ValueError('Vec.rotate(): first argument must be at least 3 items long')
p = self.polar() p = self.polar()
a = [p[0][0] + angle[0], p[0][1] + angle[1], p[0][2] + angle[2]] a = [p[0][0] + angle[0], p[0][1] + angle[1], p[0][2] + angle[2]]
self.polar(a, p[1]) self.polar(a, p[1])
@ -689,7 +702,7 @@ class Vec:
print('WARNING: tried to add Vec with ' + str(type(other))) print('WARNING: tried to add Vec with ' + str(type(other)))
return other return other
def setitem(self, index, value): def __setitem__(self, index, value):
if(index == 0): if(index == 0):
self.x = value self.x = value
elif(index == 1): elif(index == 1):
@ -698,7 +711,7 @@ class Vec:
self.z = value self.z = value
else: else:
print('ERROR: tried to set index ' + str(index) + ' of a Vec') print('ERROR: tried to set index ' + str(index) + ' of a Vec')
def getitem(self, index): def __getitem__(self, index):
if(index == 0): if(index == 0):
return self.x return self.x
elif(index == 1): elif(index == 1):
@ -706,8 +719,7 @@ class Vec:
elif(index == 2): elif(index == 2):
return self.z return self.z
else: else:
print('ERROR: tried to get index ' + str(index) + ' of a Vec') raise IndexError('tried to get index ' + str(index) + ' of a Vec')
return None
def __str__(self): def __str__(self):
return "Vec" + str(self.cartesian()) return "Vec" + str(self.cartesian())