Package horizons :: Package util :: Package shapes :: Module point
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.shapes.point

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons 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. 
10  # 
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. 
15  # 
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  # ################################################### 
21   
22  from horizons.util.python import Const 
23  from horizons.util.shapes import Shape 
24 25 26 -class Point(Shape):
27 - def __init__(self, x, y):
28 self.x = x 29 self.y = y
30
31 - def copy(self):
32 return Point(self.x, self.y)
33
34 - def to_tuple(self):
35 """Returns point as a tuple""" 36 return (self.x, self.y)
37 38 @property
39 - def center(self):
40 """Returns the center of the point (this makes Point interface more coherent with Rect). 41 """ 42 return self
43
44 - def offset(self, x_offset, y_offset):
45 """Returns a Point with an offset of x, y relative to this Point. 46 @param x_offset: int relative x-offset of the point to return 47 @param y_offset: int relative y-offset of the point to return 48 @return: a Point with offset x, y relative to the 'self' Point 49 """ 50 return Point(self.x + x_offset, self.y + y_offset)
51
52 - def contains(self, point):
53 """For compatibility with Rect""" 54 return self.x == point.x and self.y == point.y
55
56 - def __str__(self):
57 """ nice representation for debugging purposes """ 58 return 'Point({}, {})'.format(self.x, self.y)
59
60 - def __eq__(self, other):
61 if other is None: 62 return False 63 elif isinstance(other, Point): 64 return self.x == other.x and self.y == other.y 65 else: # other is tuple 66 try: 67 return self.x == other[0] and self.y == other[1] 68 except TypeError: 69 return False
70
71 - def __add__(self, other):
72 assert isinstance(other, Point) 73 return Point(self.x + other.x, self.y + other.y)
74
75 - def __sub__(self, other):
76 assert isinstance(other, Point) 77 return Point(self.x - other.x, self.y - other.y)
78
79 - def __hash__(self):
80 return hash((self.x, self.y))
81
82 - def tuple_iter(self):
83 yield self.to_tuple()
84
85 - def iter_without_border(self):
86 yield self
87
88 89 -class ConstPoint(Const, Point):
90 """An immutable Point""" 91 pass
92