tanchiki
diff vector.py @ 0:5d007d516739
module vector added
author | Olga Zolotareva <olya_zol@inbox.ru> |
---|---|
date | Sat, 18 Dec 2010 05:36:01 +0300 |
parents | |
children | af02cd410e37 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/vector.py Sat Dec 18 05:36:01 2010 +0300 1.3 @@ -0,0 +1,60 @@ 1.4 +from math import * 1.5 + 1.6 +class Vector(object): 1.7 + 1.8 + def __init__(self, x=0 , y=0): 1.9 + self.x = x 1.10 + self.y = y 1.11 + 1.12 + def __add__(self, other): 1.13 + result = Vector() 1.14 + result.x = self.x + other.x 1.15 + result.y = self.y + other.y 1.16 + return result 1.17 + 1.18 + def mul_v(self, alpha): 1.19 + result = Vector() 1.20 + result.x = self.x * alpha 1.21 + result.y = self.y * alpha 1.22 + return result 1.23 + 1.24 + def dot_product(self, other): 1.25 + return self.x*other.x + self.y*other.y 1.26 + 1.27 + def len_v(self): 1.28 + return (self.x**2 + self.y**2)**0.5 1.29 + 1.30 + def __str__(self): 1.31 + return "(%s, %s)" % (self.x, self.y) 1.32 + 1.33 + def is_null(self): 1.34 + if self.len_v() == 0 : 1.35 + return 1 1.36 + else : 1.37 + return 0 1.38 + 1.39 + 1.40 + def get_rho(self): 1.41 + return self.len_v() 1.42 + 1.43 + def set_rho(self, new_rho): 1.44 + self.x , self.y = self.x*(new_rho/self.len_v()) , self.y*(new_rho/self.len_v()) 1.45 + 1.46 + rho = property(get_rho, set_rho) 1.47 + 1.48 + def get_phi(self): 1.49 + cos = self.dot_product(Vector(1,0))/(self.len_v()*1) 1.50 + if self.y < 0 : 1.51 + phi = -acos(cos) 1.52 + else : 1.53 + phi = acos(cos) 1.54 + return phi 1.55 + 1.56 + def set_phi(self, new_phi): 1.57 + self.x , self.y = self.len_v()*cos(new_phi) , self.len_v()*sin(new_phi) 1.58 + 1.59 + phi = property(get_phi, set_phi) 1.60 + 1.61 +i = Vector(1,0) 1.62 +j = Vector(0,1) 1.63 +null = Vector(0,0) 1.64 \ No newline at end of file