Browse Source

refactoring 2

olinox 9 years ago
parent
commit
5da2bbb5d6
5 changed files with 53 additions and 50 deletions
  1. 3 4
      pypog/Grid.py
  2. 7 7
      pypog/geometry.py
  3. 4 0
      pypog/pencil.py
  4. 26 26
      tests/geometry/test_line.py
  5. 13 13
      tests/geometry/test_triangle.py

+ 3 - 4
pypog/Grid.py

@@ -25,8 +25,7 @@ class Grid(object):
             for y in range(self.height):
             for y in range(self.height):
                 cell = Cell(self.cell_shape, x, y)
                 cell = Cell(self.cell_shape, x, y)
                 self._cells[(x, y)] = cell
                 self._cells[(x, y)] = cell
-        
-        
+
     # properties
     # properties
     @property
     @property
     def cell_shape(self):
     def cell_shape(self):
@@ -98,11 +97,11 @@ class Grid(object):
 
 
 
 
     # pathfinding methods
     # pathfinding methods
-    def moving_cost(self, x, y):
+    def moving_cost(self, *args):
         return 1
         return 1
     
     
     def path(self, x1, y1, x2, y2):
     def path(self, x1, y1, x2, y2):
-        return pathfinder.path( self, (x1, y1), (x2,y2), self.moving_cost )
+        return pathfinder.path( self, (x1, y1), (x2,y2), self.moving_cost_function )
 
 
     
     
 class HexGrid(Grid):
 class HexGrid(Grid):

+ 7 - 7
pypog/geometry.py

@@ -40,7 +40,7 @@ def squ_neighbours_of(x, y):
 
 
 ## zones
 ## zones
 
 
-def zone(grid_shape, x0, y0, radius):
+def zone(cell_shape, x0, y0, radius):
     """ returns the list of the coordinates of the cells in the zone around (x0, y0)
     """ returns the list of the coordinates of the cells in the zone around (x0, y0)
     """
     """
     if not all(isinstance(c, int) for c in [x0, y0, radius]):
     if not all(isinstance(c, int) for c in [x0, y0, radius]):
@@ -52,33 +52,33 @@ def zone(grid_shape, x0, y0, radius):
     for _ in range(0, radius):
     for _ in range(0, radius):
         current = buffer
         current = buffer
         for x, y in current:
         for x, y in current:
-            buffer |= frozenset( neighbours_of( grid_shape, x, y ) )
+            buffer |= frozenset( neighbours_of( cell_shape, x, y ) )
 
 
     return list(buffer)
     return list(buffer)
 
 
 
 
 ## line : bresenham algorithm
 ## line : bresenham algorithm
 
 
-def line2d(grid_shape, x1, y1, x2, y2):
+def line2d(cell_shape, x1, y1, x2, y2):
     """returns a line from x1,y1 to x2,y2
     """returns a line from x1,y1 to x2,y2
     grid could be one of the GRIDTYPES values"""
     grid could be one of the GRIDTYPES values"""
     if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
     if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
         raise TypeError("x1, y1, x2, y2 have to be integers")
         raise TypeError("x1, y1, x2, y2 have to be integers")
     if (x1, y1) == (x2, y2):
     if (x1, y1) == (x2, y2):
         return [(x1, y1)]
         return [(x1, y1)]
-    if grid_shape == HEX:
+    if cell_shape == HEX:
         return hex_2d_line(x1, y1, x2, y2)
         return hex_2d_line(x1, y1, x2, y2)
-    elif grid_shape == SQUARE: 
+    elif cell_shape == SQUARE: 
         return squ_2d_line(x1, y1, x2, y2)
         return squ_2d_line(x1, y1, x2, y2)
     else:
     else:
         raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
         raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
 
 
-def line3d(grid_shape, x1, y1, z1, x2, y2, z2):
+def line3d(cell_shape, x1, y1, z1, x2, y2, z2):
     """returns a line from x1,y1,z1 to x2,y2,z2
     """returns a line from x1,y1,z1 to x2,y2,z2
     grid could be one of the GRIDTYPES values"""
     grid could be one of the GRIDTYPES values"""
     if not all(isinstance(c, int) for c in [x1, y1, z1, x2, y2, z2]):
     if not all(isinstance(c, int) for c in [x1, y1, z1, x2, y2, z2]):
         raise TypeError("x1, y1, z1, x2, y2, z2 have to be integers")
         raise TypeError("x1, y1, z1, x2, y2, z2 have to be integers")
-    hoLine = line2d(grid_shape, x1, y1, x2, y2)
+    hoLine = line2d(cell_shape, x1, y1, x2, y2)
     if z1 == z2:
     if z1 == z2:
         return [(x, y, z1) for x, y in hoLine]
         return [(x, y, z1) for x, y in hoLine]
     else:
     else:

+ 4 - 0
pypog/pencil.py

@@ -3,10 +3,13 @@ Created on 5 d
 
 
 @author: olinox
 @author: olinox
 '''
 '''
+from abc import ABCMeta
+
 from pypog.geometry import line2d, zone
 from pypog.geometry import line2d, zone
 
 
 
 
 class BasePencil(object):
 class BasePencil(object):
+    __metaclass__ = ABCMeta
     
     
     def __init__(self, grid):
     def __init__(self, grid):
         self._grid = grid
         self._grid = grid
@@ -94,3 +97,4 @@ class SimplePencil(BasePencil):
         
         
         
         
 
 
+

+ 26 - 26
tests/geometry/test_line.py

@@ -13,82 +13,82 @@ class Test(unittest.TestCase):
 
 
     def test_hex_line(self):
     def test_hex_line(self):
         """ 2d line on hexagonal grid """
         """ 2d line on hexagonal grid """
-        grid_shape = geometry.HEX
+        cell_shape = geometry.HEX
         
         
-        line = geometry.line2d(grid_shape, 1,1,1,1)
+        line = geometry.line2d(cell_shape, 1,1,1,1)
         self.assertEqual(line, [(1,1)])
         self.assertEqual(line, [(1,1)])
         
         
-        line = geometry.line2d(grid_shape, 0,0,1,1)
+        line = geometry.line2d(cell_shape, 0,0,1,1)
         self.assertEqual(line, [(0,0), (0,1), (1,1)])
         self.assertEqual(line, [(0,0), (0,1), (1,1)])
  
  
-        line = geometry.line2d(grid_shape, 0,0,7,3)
+        line = geometry.line2d(cell_shape, 0,0,7,3)
         self.assertEqual(line, [(0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)] )
         self.assertEqual(line, [(0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)] )
  
  
-        line = geometry.line2d(grid_shape, 4,3,0,3)
+        line = geometry.line2d(cell_shape, 4,3,0,3)
         self.assertEqual(line, [(4,3), (3,2), (2,3), (1,2), (0,3)] )
         self.assertEqual(line, [(4,3), (3,2), (2,3), (1,2), (0,3)] )
  
  
-        line = geometry.line2d(grid_shape, 3,0,3,3)
+        line = geometry.line2d(cell_shape, 3,0,3,3)
         self.assertEqual(line, [(3,0), (3,1), (3,2), (3,3)] )
         self.assertEqual(line, [(3,0), (3,1), (3,2), (3,3)] )
         
         
         
         
     def test_squ_line(self):
     def test_squ_line(self):
         """ 2d line on square grid """
         """ 2d line on square grid """
-        grid_shape = geometry.SQUARE
-        line = geometry.line2d(grid_shape,0,0,0,1)
+        cell_shape = geometry.SQUARE
+        line = geometry.line2d(cell_shape,0,0,0,1)
         self.assertEqual(line, [(0,0), (0,1)])
         self.assertEqual(line, [(0,0), (0,1)])
         
         
-        line = geometry.line2d(grid_shape,0,0,1,1)
+        line = geometry.line2d(cell_shape,0,0,1,1)
         self.assertEqual(line, [(0,0), (1,1)])
         self.assertEqual(line, [(0,0), (1,1)])
         
         
-        line = geometry.line2d(grid_shape,0,0,7,3)
+        line = geometry.line2d(cell_shape,0,0,7,3)
         self.assertEqual(line, [(0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)] )
         self.assertEqual(line, [(0,0), (1,0), (2,1), (3,1), (4,2), (5,2), (6,3), (7,3)] )
  
  
-        line = geometry.line2d(grid_shape,4,3,0,3)
+        line = geometry.line2d(cell_shape,4,3,0,3)
         self.assertEqual(line, [(4,3), (3,3), (2,3), (1,3), (0,3)] )
         self.assertEqual(line, [(4,3), (3,3), (2,3), (1,3), (0,3)] )
  
  
-        line = geometry.line2d(grid_shape,3,0,3,3)
+        line = geometry.line2d(cell_shape,3,0,3,3)
         self.assertEqual(line, [(3,0), (3,1), (3,2), (3,3)] )
         self.assertEqual(line, [(3,0), (3,1), (3,2), (3,3)] )
     
     
     def test_hex_line_3d(self):
     def test_hex_line_3d(self):
         """ 3d line on hexagonal grid """
         """ 3d line on hexagonal grid """
-        grid_shape = geometry.HEX
-        line = geometry.line3d(grid_shape,1,1,1,1,1,1)
+        cell_shape = geometry.HEX
+        line = geometry.line3d(cell_shape,1,1,1,1,1,1)
         self.assertEqual(line, [(1,1,1)])
         self.assertEqual(line, [(1,1,1)])
     
     
-        line = geometry.line3d(grid_shape,1,1,0,1,1,1)
+        line = geometry.line3d(cell_shape,1,1,0,1,1,1)
         self.assertEqual(line, [(1,1,0), (1,1,1)])
         self.assertEqual(line, [(1,1,0), (1,1,1)])
     
     
-        line = geometry.line3d(grid_shape,0,0,0,1,1,1)
+        line = geometry.line3d(cell_shape,0,0,0,1,1,1)
         self.assertEqual(line, [(0,0,0), (0,1,0), (1,1,1)])
         self.assertEqual(line, [(0,0,0), (0,1,0), (1,1,1)])
     
     
-        line = geometry.line3d(grid_shape,0,0,0,7,3,7)
+        line = geometry.line3d(cell_shape,0,0,0,7,3,7)
         self.assertEqual(line, [(0,0,0), (1,0,1), (2,1,2), (3,1,3), (4,2,4), (5,2,5), (6,3,6), (7,3,7)] )
         self.assertEqual(line, [(0,0,0), (1,0,1), (2,1,2), (3,1,3), (4,2,4), (5,2,5), (6,3,6), (7,3,7)] )
  
  
-        line = geometry.line3d(grid_shape,4,3,10,0,3,3)
+        line = geometry.line3d(cell_shape,4,3,10,0,3,3)
         self.assertEqual(line, [(4,3,10), (3,2,9), (3,2,8), (2,3,7), (2,3,6), (1,2,5), (1,2,4), (0,3,3)] )
         self.assertEqual(line, [(4,3,10), (3,2,9), (3,2,8), (2,3,7), (2,3,6), (1,2,5), (1,2,4), (0,3,3)] )
  
  
-        line = geometry.line3d(grid_shape,3,0,0,3,3,0)
+        line = geometry.line3d(cell_shape,3,0,0,3,3,0)
         self.assertEqual(line, [(3,0,0), (3,1,0), (3,2,0), (3,3,0)] )
         self.assertEqual(line, [(3,0,0), (3,1,0), (3,2,0), (3,3,0)] )
         
         
     def test_squ_line_3d(self):
     def test_squ_line_3d(self):
         """ 3d line on square grid """
         """ 3d line on square grid """
-        grid_shape = geometry.SQUARE
-        line = geometry.line3d(grid_shape,1,1,1,1,1,1)
+        cell_shape = geometry.SQUARE
+        line = geometry.line3d(cell_shape,1,1,1,1,1,1)
         self.assertEqual(line, [(1,1,1)])
         self.assertEqual(line, [(1,1,1)])
     
     
-        line = geometry.line3d(grid_shape,1,1,0,1,1,1)
+        line = geometry.line3d(cell_shape,1,1,0,1,1,1)
         self.assertEqual(line, [(1,1,0), (1,1,1)])
         self.assertEqual(line, [(1,1,0), (1,1,1)])
     
     
-        line = geometry.line3d(grid_shape,0,0,0,1,1,1)
+        line = geometry.line3d(cell_shape,0,0,0,1,1,1)
         self.assertEqual(line, [(0,0,0), (1,1,1)])
         self.assertEqual(line, [(0,0,0), (1,1,1)])
     
     
-        line = geometry.line3d(grid_shape,0,0,0,7,3,7)
+        line = geometry.line3d(cell_shape,0,0,0,7,3,7)
         self.assertEqual(line, [(0,0,0), (1,0,1), (2,1,2), (3,1,3), (4,2,4), (5,2,5), (6,3,6), (7,3,7)] )
         self.assertEqual(line, [(0,0,0), (1,0,1), (2,1,2), (3,1,3), (4,2,4), (5,2,5), (6,3,6), (7,3,7)] )
  
  
-        line = geometry.line3d(grid_shape,4,3,10,0,3,3)
+        line = geometry.line3d(cell_shape,4,3,10,0,3,3)
         self.assertEqual(line, [(4,3,10), (3,3,9), (3,3,8), (2,3,7), (2,3,6), (1,3,5), (1,3,4), (0,3,3)] )
         self.assertEqual(line, [(4,3,10), (3,3,9), (3,3,8), (2,3,7), (2,3,6), (1,3,5), (1,3,4), (0,3,3)] )
  
  
-        line = geometry.line3d(grid_shape,3,0,0,3,3,0)
+        line = geometry.line3d(cell_shape,3,0,0,3,3,0)
         self.assertEqual(line, [(3,0,0), (3,1,0), (3,2,0), (3,3,0)] )
         self.assertEqual(line, [(3,0,0), (3,1,0), (3,2,0), (3,3,0)] )
 
 
 
 

+ 13 - 13
tests/geometry/test_triangle.py

@@ -13,40 +13,40 @@ class Test(unittest.TestCase):
 
 
     def test_sq_triangle(self):
     def test_sq_triangle(self):
         """test triangle algorithms on square grid"""
         """test triangle algorithms on square grid"""
-        grid_shape = geometry.SQUARE
+        cell_shape = geometry.SQUARE
         
         
         for i in geometry.ANGLES:
         for i in geometry.ANGLES:
-            self.assertCountEqual(geometry.triangle(grid_shape, 0, 0, 0, 0, i), [(0,0)])
+            self.assertCountEqual(geometry.triangle(cell_shape, 0, 0, 0, 0, i), [(0,0)])
 
 
         #TODO: complete
         #TODO: complete
 
 
 
 
     def test_hex_triangle(self):
     def test_hex_triangle(self):
         """test triangle algorithms on hexagonal grid"""
         """test triangle algorithms on hexagonal grid"""
-        grid_shape = geometry.HEX
+        cell_shape = geometry.HEX
         for i in geometry.ANGLES:
         for i in geometry.ANGLES:
-            self.assertCountEqual(geometry.triangle(grid_shape, 0, 0, 0, 0, i), [(0,0)])
+            self.assertCountEqual(geometry.triangle(cell_shape, 0, 0, 0, 0, i), [(0,0)])
         #TODO: complete
         #TODO: complete
     
     
     def test_sq_triangle_3d(self):
     def test_sq_triangle_3d(self):
         """test triangle3d algorithms on square grid"""
         """test triangle3d algorithms on square grid"""
-        grid_shape = geometry.SQUARE
+        cell_shape = geometry.SQUARE
         #TODO: complete
         #TODO: complete
      
      
     def test_hex_triangle_3d(self):
     def test_hex_triangle_3d(self):
         """test triangle3d algorithms on hexagonal grid"""
         """test triangle3d algorithms on hexagonal grid"""
-        grid_shape = geometry.HEX
+        cell_shape = geometry.HEX
         #TODO: complete
         #TODO: complete
 
 
     def test_errors(self):
     def test_errors(self):
         
         
-        for grid_shape in (geometry.HEX, geometry.SQUARE):
-            self.assertRaises(ValueError, geometry.triangle, grid_shape, 0, 0, 0, 0, 0)
-            self.assertRaises(TypeError, geometry.triangle, grid_shape, "a", 0, 0, 0, 1)
-            self.assertRaises(TypeError, geometry.triangle, grid_shape, 0, "a", 0, 0, 1)
-            self.assertRaises(TypeError, geometry.triangle, grid_shape, 0, 0, "a", 0, 1)
-            self.assertRaises(TypeError, geometry.triangle, grid_shape, 0, 0, 0, "a", 1)
-            self.assertRaises(ValueError, geometry.triangle, grid_shape, 0, 0, 0, 0, "a")
+        for cell_shape in (geometry.HEX, geometry.SQUARE):
+            self.assertRaises(ValueError, geometry.triangle, cell_shape, 0, 0, 0, 0, 0)
+            self.assertRaises(TypeError, geometry.triangle, cell_shape, "a", 0, 0, 0, 1)
+            self.assertRaises(TypeError, geometry.triangle, cell_shape, 0, "a", 0, 0, 1)
+            self.assertRaises(TypeError, geometry.triangle, cell_shape, 0, 0, "a", 0, 1)
+            self.assertRaises(TypeError, geometry.triangle, cell_shape, 0, 0, 0, "a", 1)
+            self.assertRaises(ValueError, geometry.triangle, cell_shape, 0, 0, 0, 0, "a")
     
     
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":