Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/tanchiki/file/0598557ca61b/vector.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 12:01:51 2013
Кодировка:
tanchiki: 0598557ca61b vector.py

tanchiki

view vector.py @ 48:0598557ca61b

Nicer tank graphics. Displays body direction too.
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Mon, 20 Dec 2010 20:58:14 +0300
parents b7a85caedc7f dfdc1def5d24
children d740eff76e7e
line source
1 import math
3 class Vector(object):
5 def __init__(self, x=0 , y=0):
6 self.x = x
7 self.y = y
9 def __add__(self, other):
10 result = Vector(0,0)
11 result.x = self.x + other.x
12 result.y = self.y + other.y
13 return result
15 def __sub__(self, other):
16 result = Vector(0,0)
17 result.x = self.x - other.x
18 result.y = self.y - other.y
19 return result
21 def __mul__(self, alpha):
22 result = Vector()
23 result.x = self.x * alpha
24 result.y = self.y * alpha
25 return result
27 def dot_product(self, other):
28 return self.x*other.x + self.y*other.y
30 def __abs__(self):
31 return (self.x**2 + self.y**2)**0.5
33 def __str__(self):
34 return "(%s, %s)" % (self.x, self.y)
36 def is_null(self):
37 if abs(self) == 0 :
38 return 1
39 else :
40 return 0
42 def normalize(self):
43 result = Vector()
44 result.x = self.x
45 result.y = self.y
46 result.rho = 1
47 return result
49 def get_rho(self):
50 return abs(self)
52 def set_rho(self, new_rho):
53 if self.is_null() == 1:
54 self.x , self.y = new_rho*math.cos(self.phi) , new_rho*math.sin(self.phi)
55 else :
56 self.x , self.y = self.x*(new_rho/abs(self)) , self.y*(new_rho/abs(self))
58 rho = property(get_rho, set_rho)
60 def get_phi(self):
61 phi = math.pi/2 - math.atan2(self.x, self.y)
62 if self.is_null == 1:
63 phi = 0
64 return phi
66 def set_phi(self, new_phi):
67 rho = abs(self)
68 self.x , self.y = rho*math.cos(new_phi) , rho*math.sin(new_phi)
70 phi = property(get_phi, set_phi)
72 i = Vector(1,0)
73 j = Vector(0,1)
74 null = Vector(0,0)