comparison python/moving.py @ 776:84420159c5f4 dev

added __eq__ functions for Point and Trajectory
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Fri, 05 Feb 2016 17:17:12 -0500
parents bd13937818a4
children 1b22d81ef5ff
comparison
equal deleted inserted replaced
775:56153d439f8c 776:84420159c5f4
32 def __str__(self): 32 def __str__(self):
33 return '[{0}, {1}]'.format(self.first, self.last) 33 return '[{0}, {1}]'.format(self.first, self.last)
34 34
35 def __repr__(self): 35 def __repr__(self):
36 return self.__str__() 36 return self.__str__()
37
38 def __eq__(self, other):
39 return ((self.first == other.first) and (self.last == other.last)) or ((self.first == other.last) and (self.last == other.first))
37 40
38 def empty(self): 41 def empty(self):
39 return self.first > self.last 42 return self.first > self.last
40 43
41 def center(self): 44 def center(self):
179 def __str__(self): 182 def __str__(self):
180 return '({:f},{:f})'.format(self.x,self.y) 183 return '({:f},{:f})'.format(self.x,self.y)
181 184
182 def __repr__(self): 185 def __repr__(self):
183 return self.__str__() 186 return self.__str__()
187
188 def __eq__(self, other):
189 return (self.x == other.x) and (self.y == other.y)
184 190
185 def __add__(self, other): 191 def __add__(self, other):
186 return Point(self.x+other.x, self.y+other.y) 192 return Point(self.x+other.x, self.y+other.y)
187 193
188 def __sub__(self, other): 194 def __sub__(self, other):
658 return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())]) 664 return ' '.join([self.__getitem__(i).__str__() for i in xrange(self.length())])
659 665
660 def __repr__(self): 666 def __repr__(self):
661 return self.__str__() 667 return self.__str__()
662 668
663
664 def __iter__(self): 669 def __iter__(self):
665 self.iterInstantNum = 0 670 self.iterInstantNum = 0
666 return self 671 return self
667 672
668 def next(self): 673 def next(self):
669 if self.iterInstantNum >= self.length(): 674 if self.iterInstantNum >= self.length():
670 raise StopIteration 675 raise StopIteration
671 else: 676 else:
672 self.iterInstantNum += 1 677 self.iterInstantNum += 1
673 return self[self.iterInstantNum-1] 678 return self[self.iterInstantNum-1]
679
680 def __eq__(self, other):
681 if self.length() == other.length():
682 result = True
683 for p, po in zip(self, other):
684 result = result and (p == po)
685 return result
686 else:
687 return False
674 688
675 def setPositionXY(self, i, x, y): 689 def setPositionXY(self, i, x, y):
676 if i < self.__len__(): 690 if i < self.__len__():
677 self.positions[0][i] = x 691 self.positions[0][i] = x
678 self.positions[1][i] = y 692 self.positions[1][i] = y