Browse Source

add Grid class

olinox 9 years ago
parent
commit
c609a18d54
3 changed files with 98 additions and 28 deletions
  1. 73 0
      core/Grid.py
  2. 23 27
      core/bresenham.py
  3. 2 1
      core/constants.py

+ 73 - 0
core/Grid.py

@@ -0,0 +1,73 @@
+'''
+Created on 7 nov. 2016
+    Game Grid
+@author: olinox
+'''
+from core import bresenham
+from core.constants import GRID_GEOMETRIES
+
+
+class Grid(object):
+    def __init__(self, geometry, width, height):
+        self._geometry = geometry
+        self._width = width
+        self._height = height
+        
+    # properties
+    @property
+    def geometry(self):
+        return self._geometry
+    
+    @geometry.setter
+    def geometry(self, geometry):
+        if not geometry in GRID_GEOMETRIES:
+            raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
+        self._geometry = geometry
+        
+    @property
+    def width(self):
+        return self._width
+    
+    @width.setter
+    def width(self, width):
+        if not isinstance(width, int) or not width > 0:
+            raise ValueError("'width' has to be a strictly positive integer")
+        self._width = width
+        
+    @property
+    def height(self):
+        return self._height
+    
+    @height.setter
+    def height(self, height):
+        if not isinstance(height, int) or not height > 0:
+            raise ValueError("'width' has to be a strictly positive integer")
+        self._height = height    
+    
+    # methods
+    def cases_number(self):
+        return self.height * self.width
+    
+    def line(self, *args):
+        if len(args) == 4:
+            x1, y1, x2, y2 = args
+            return bresenham.line2d(self.geometry, x1, y1, x2, y2)
+        if len(args) == 6:
+            x1, y1, z1, x2, y2, z2 = args
+            return bresenham.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
+    
+    
+    
+    
+if __name__ == '__main__':
+    gr = Grid(5, 100, 100)
+    print(gr.cases_number())
+    print(gr.line(1,1,5,10))
+    print(gr.line(1,1,1,5,10,10))
+    
+    
+    
+    
+    
+    
+    

+ 23 - 27
core/bresenham.py

@@ -5,11 +5,32 @@ Created on 7 nov. 2016
     Compute lines between coordinates in 2d or 3d, on hexagonal or square grid
 @author: olinox
 '''
-
 from math import sqrt
-
 from core.constants import SQUAREGRID, HEXGRID
 
+def line2d(geometry, x1, y1, x2, y2):
+    """returns a line from x1,y1 to x2,y2
+    grid could be one of the GRIDTYPES values"""
+    if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
+        raise TypeError("x1, y1, x2, y2 have to be integers")
+    if geometry == SQUAREGRID:
+        return _brH(x1, y1, x2, y2)
+    elif geometry == HEXGRID: 
+        return _brC(x1, y1, x2, y2)
+    else:
+        raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
+
+def line3d(geometry, x1, y1, z1, x2, y2, z2):
+    """returns a line from x1,y1,z1 to x2,y2,z2
+    grid could be one of the GRIDTYPES values"""
+    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")
+    hoLine = line2d(geometry, x1, y1, x2, y2)
+    if z1 == z2:
+        return [(x, y, z1) for x, y in hoLine]
+    else:
+        ligneZ = _brC(0, z1, (len(hoLine)-1), z2)
+        return [(hoLine[d][0], hoLine[d][1], z) for d, z in ligneZ]
 
 def hex_2d_line(x1, y1, x2, y2):
     """returns a line from x1,y1 to x2,y2 on an hexagonal grid
@@ -25,7 +46,6 @@ def hex_3d_line(x1, y1, z1, x2, y2, z2):
     line is a list of coordinates"""
     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")
-    
     hoLine = hex_2d_line(x1, y1, x2, y2)
     if z1 == z2:
         return [(x, y, z1) for x, y in hoLine]
@@ -59,29 +79,7 @@ def squ_3d_line(x1, y1, z1, x2, y2, z2):
         zLine = _brC(0, z1, (len(hoLine)-1), z2)
         return [(hoLine[d][0], hoLine[d][1], z) for d, z in zLine]
 
-def line2d(grid, x1, y1, x2, y2):
-    """returns a line from x1,y1 to x2,y2
-    grid could be one of the GRIDTYPES values"""
-    if not all(isinstance(c, int) for c in [x1, y1, x2, y2]):
-        raise TypeError("x1, y1, x2, y2 have to be integers")
-    if grid == SQUAREGRID:
-        return _brH(x1, y1, x2, y2)
-    elif grid == HEXGRID: 
-        return _brC(x1, y1, x2, y2)
-    else:
-        raise ValueError("grid has to be a value from GRIDTYPES")
 
-def ligne3d(grid, x1, y1, z1, x2, y2, z2):
-    """returns a line from x1,y1,z1 to x2,y2,z2
-    grid could be one of the GRIDTYPES values"""
-    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")
-    hoLine = line2d(grid, x1, y1, x2, y2)
-    if z1 == z2:
-        return [(x, y, z1) for x, y in hoLine]
-    else:
-        ligneZ = _brC(0, z1, (len(hoLine)-1), z2)
-        return [(hoLine[d][0], hoLine[d][1], z) for d, z in ligneZ]
     
 def _brC(x1, y1, x2, y2):
     """Line Bresenham algorithm for square grid"""
@@ -115,8 +113,6 @@ def _brC(x1, y1, x2, y2):
         result.reverse()
     return result
     
-    
-    
 def _brH(x1, y1, x2, y2):
     """Line Bresenham algorithm for hexagonal grid"""
     reversed_sym = (x1 > x2)

+ 2 - 1
core/constants.py

@@ -4,6 +4,7 @@ Created on 7 nov. 2016
 @author: olinox
 '''
 
-GRIDTYPES = [4, 5]
+# geometry
+GRID_GEOMETRIES = [4, 5]
 SQUAREGRID = 4
 HEXGRID = 5