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

Source Code for Module horizons.util.shapes.annulus

 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 . import Shape 
23  from .point import Point 
24   
25   
26 -class Annulus(Shape):
27 """Class for the shape of an annulus 28 You can access center and radius of the annulus as public members."""
29 - def __init__(self, center, min_radius, max_radius):
30 """ 31 @param center: Point 32 @param min_radius: int 33 @param max_radius: int 34 """ 35 assert isinstance(center, Point) 36 self.center = center 37 self.min_radius = min_radius 38 self.max_radius = max_radius
39
40 - def contains(self, point):
41 assert isinstance(point, Point) 42 return self.min_radius <= point.distance(self.center) <= self.max_radius
43
44 - def __str__(self):
45 return ("Annulus(center={},min_radius={},max_radius={})" 46 .format(self.center, self.min_radius, self.max_radius))
47
48 - def __eq__(self, other):
49 try: 50 return self.center == other.center and \ 51 self.min_radius == other.min_radius and \ 52 self.max_radius == other.max_radius 53 except AttributeError: 54 return False
55
56 - def __ne__(self, other):
57 return not self.__eq__(other)
58
59 - def __hash__(self):
60 return hash((self.center, self.min_radius, self.max_radius))
61
62 - def tuple_iter(self):
63 for x in range(self.center.x - self.max_radius, self.center.x + self.max_radius + 1): 64 for y in range(self.center.y - self.max_radius, self.center.y + self.max_radius + 1): 65 if self.min_radius <= self.center.distance((x, y)) <= self.max_radius: 66 yield (x, y)
67