Imported Upstream version 2008.1+svn1656
[opeanno-debian-packaging.git] / game / util / point.py
blob6b4fac02deb6d87a74d46411c2d6a334193e9cbc
1 # ###################################################
2 # Copyright (C) 2008 The OpenAnno Team
3 # team@openanno.org
4 # This file is part of OpenAnno.
6 # OpenAnno is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # ###################################################
22 class Point(object):
23 def __init__(self, x, y):
24 self.x, self.y = x, y
26 def copy(self):
27 return Point(self.x, self.y)
29 def distance(self, other):
30 if isinstance(other, Point):
31 return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
32 elif isinstance(other, tuple):
33 return ((self.x - other[0]) ** 2 + (self.y - other[1]) ** 2) ** 0.5
34 else:
35 return other.distance(self)
37 def get_coordinates(self):
38 """ Returns point as coordinate
39 This is useful, because Rect supports this too.
40 """
41 return [(self.x, self.y)]
43 def offset(self, x_offset, y_offset):
44 """Returns a Point with an offset of x, y relative to this Point.
45 @param x_offset: int relative x-offset of the point to return
46 @param y_offset: int relative y-offset of the point to return
47 @return: a Point with offset x, y relative to the 'self' Point
48 """
49 return Point(self.x + x_offset, self.y + y_offset)
51 def __str__(self):
52 """ nice representation for debugging purposes """
53 return 'Point(%s, %s)' % (self.x, self.y)
55 def __eq__(self, other):
56 if other is None:
57 return False
58 elif isinstance(other, Point):
59 return (self.x == other.x and self.y == other.y)
60 else: # other is tuple
61 try:
62 return (self.x == other[0] and self.y == other[1])
63 except TypeError:
64 return False
66 from game.util.encoder import register_classes
67 register_classes(Point)