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

Source Code for Module horizons.util.shapes.circle

 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 Circle(Shape):
27 """Class for the shape of a circle 28 You can access center and radius of the circle as public members."""
29 - def __init__(self, center, radius):
30 """ 31 @param center: Point 32 @param radius: int 33 """ 34 assert isinstance(center, Point) 35 self.center = center 36 self.radius = radius
37
38 - def contains(self, point):
39 assert isinstance(point, Point) 40 dx = point.x - self.center.x 41 dy = point.y - self.center.y 42 return dx * dx + dy * dy <= self.radius * self.radius
43
44 - def intersects_rect(self, rect):
45 return rect.distance(self.center) > self.radius
46
47 - def __str__(self):
48 return "Circle(center={},radius={})".format(self.center, self.radius)
49
50 - def __eq__(self, other):
51 try: 52 return self.center == other.center and self.radius == other.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.radius))
61
62 - def tuple_iter(self):
63 """Iterate through all coords in the circle as tuples.""" 64 cx = self.center.x 65 cy = self.center.y 66 radius_sq = self.radius * self.radius 67 for x in range(cx - self.radius, cx + self.radius + 1): 68 for y in range(cy - self.radius, cy + self.radius + 1): 69 dx = cx - x 70 dy = cy - y 71 dist_sq = dx * dx + dy * dy 72 if dist_sq <= radius_sq: 73 yield (x, y)
74
75 - def get_border_coordinates(self, bordersize=1):
76 """Returns only coordinates at the border. Very naive implementation""" 77 for x in range(self.center.x - self.radius, self.center.x + self.radius + 1): 78 for y in range(self.center.y - self.radius, self.center.y + self.radius + 1): 79 if (self.radius - bordersize) <= self.center.distance((x, y)) <= self.radius: 80 yield (x, y)
81