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

Source Code for Package horizons.util.shapes

 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.shapes import distances 
23   
24   
25 -class Shape:
26
27 - def get_coordinates(self):
28 """Return all coordinates in the shape.""" 29 return list(self.tuple_iter())
30
31 - def __iter__(self):
32 """Return all coordinates in the shape as points.""" 33 for x, y in self.tuple_iter(): 34 yield Point(x, y)
35
36 - def tuple_iter(self):
37 raise NotImplementedError
38
39 - def distance(self, other):
40 # TODO pre-build a dictionary for fast function lookup 41 co1 = self.__class__.__name__.lower() 42 co2 = other.__class__.__name__.lower() 43 44 # ConstX and X are the same w.r.t to distances 45 co1 = co1.replace('const', '') 46 co2 = co2.replace('const', '') 47 48 dist = getattr(distances, "distance_{}_{}".format(co1, co2), None) 49 if dist: 50 return dist(self, other) 51 else: 52 dist = getattr(distances, "distance_{}_{}".format(co2, co1), None) 53 if dist: 54 return dist(other, self) 55 56 raise TypeError("No distance defined between {} and {}".format(co1, co2))
57
58 - def get_distance_function(self, other):
59 # TODO pre-build a dictionary for fast function lookup 60 co1 = self.__class__.__name__.lower() 61 co2 = other.__class__.__name__.lower() 62 63 # ConstX and X are the same w.r.t to distances 64 co1 = co1.replace('const', '') 65 co2 = co2.replace('const', '') 66 67 dist_func = getattr(distances, "distance_{}_{}".format(co1, co2), None) 68 if dist_func: 69 return dist_func 70 71 dist_func = getattr(distances, "distance_{}_{}".format(co2, co1), None) 72 if dist_func: 73 return dist_func 74 75 raise TypeError("No distance defined between {} and {}".format(co1, co2))
76 77 # Convenience methods so you can use 'from horizons.util.shapes import Circle, Rect' 78 79 80 from horizons.util.shapes.point import ConstPoint, Point # isort:skip 81 from horizons.util.shapes.rect import ConstRect, Rect # isort:skip 82 from horizons.util.shapes.annulus import Annulus # isort:skip 83 from horizons.util.shapes.circle import Circle # isort:skip 84 from horizons.util.shapes.radiusshape import RadiusRect, RadiusShape # isort:skip 85