tanchiki
diff vector.py @ 36:1a0bddee3c54
Automated merge with ssh://kodomo.fbb.msu.ru/tanchiki
author | Olga Zolotareva <olya_zol@inbox.ru> |
---|---|
date | Mon, 20 Dec 2010 18:43:18 +0300 |
parents | b7a85caedc7f dfdc1def5d24 |
children | d740eff76e7e |
line diff
1.1 --- a/vector.py Mon Dec 20 16:12:55 2010 +0300 1.2 +++ b/vector.py Mon Dec 20 18:43:18 2010 +0300 1.3 @@ -1,57 +1,74 @@ 1.4 -import math 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(0,0) 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__(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 __abs__(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 abs(self) == 0 : 1.35 - return 1 1.36 - else : 1.37 - return 0 1.38 - 1.39 - def get_rho(self): 1.40 - return abs(self) 1.41 - 1.42 - def set_rho(self, new_rho): 1.43 - if self.is_null() == 1 : 1.44 - self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi) 1.45 - else : 1.46 - self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self)) 1.47 - 1.48 - rho = property(get_rho, set_rho) 1.49 - 1.50 - def get_phi(self): 1.51 - phi = math.pi/2 - math.atan2(self.x, self.y) 1.52 - if self.is_null == 1: 1.53 - phi = 0 1.54 - return phi 1.55 - 1.56 - def set_phi(self, new_phi): 1.57 - rho = abs(self) 1.58 - self.x, self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 1.59 - 1.60 - phi = property(get_phi, set_phi) 1.61 +import math 1.62 + 1.63 +class Vector(object): 1.64 + 1.65 + def __init__(self, x=0 , y=0): 1.66 + self.x = x 1.67 + self.y = y 1.68 + 1.69 + def __add__(self, other): 1.70 + result = Vector(0,0) 1.71 + result.x = self.x + other.x 1.72 + result.y = self.y + other.y 1.73 + return result 1.74 + 1.75 + def __sub__(self, other): 1.76 + result = Vector(0,0) 1.77 + result.x = self.x - other.x 1.78 + result.y = self.y - other.y 1.79 + return result 1.80 + 1.81 + def __mul__(self, alpha): 1.82 + result = Vector() 1.83 + result.x = self.x * alpha 1.84 + result.y = self.y * alpha 1.85 + return result 1.86 + 1.87 + def dot_product(self, other): 1.88 + return self.x*other.x + self.y*other.y 1.89 + 1.90 + def __abs__(self): 1.91 + return (self.x**2 + self.y**2)**0.5 1.92 + 1.93 + def __str__(self): 1.94 + return "(%s, %s)" % (self.x, self.y) 1.95 + 1.96 + def is_null(self): 1.97 + if abs(self) == 0 : 1.98 + return 1 1.99 + else : 1.100 + return 0 1.101 + 1.102 + def normalize(self): 1.103 + result = Vector() 1.104 + result.x = self.x 1.105 + result.y = self.y 1.106 + result.rho = 1 1.107 + return result 1.108 + 1.109 + def get_rho(self): 1.110 + return abs(self) 1.111 + 1.112 + def set_rho(self, new_rho): 1.113 + if self.is_null() == 1: 1.114 + self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi) 1.115 + else : 1.116 + self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self)) 1.117 + 1.118 + rho = property(get_rho, set_rho) 1.119 + 1.120 + def get_phi(self): 1.121 + phi = math.pi/2 - math.atan2(self.x, self.y) 1.122 + if self.is_null == 1: 1.123 + phi = 0 1.124 + return phi 1.125 + 1.126 + def set_phi(self, new_phi): 1.127 + rho = abs(self) 1.128 + self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi) 1.129 + 1.130 + phi = property(get_phi, set_phi) 1.131 + 1.132 +i = Vector(1,0) 1.133 +j = Vector(0,1) 1.134 +null = Vector(0,0)