Bläddra i källkod

add zone function

olinox 9 år sedan
förälder
incheckning
1b20d656b6

+ 20 - 7
core/Grid.py

@@ -3,10 +3,10 @@ Created on 7 nov. 2016
     Game Grid
 @author: olinox
 '''
-from core import bresenham, triangle, rectangle, pathfinder
-from core.constants import GRID_GEOMETRIES
-from core.triangle import ANGLES
 from core.Cell import Cell
+from core.constants import GRID_GEOMETRIES
+from core.geometry import triangle, rectangle, line, zone
+from core.pathfinder import pathfinder
 
 
 class Grid(object):
@@ -66,10 +66,10 @@ class Grid(object):
         return (x > 0 and x <= self._width and y > 0 and y <= self._height)
     
     def line(self, x1, y1, x2, y2):
-        return bresenham.line2d(self.geometry, x1, y1, x2, y2)
+        return line.line2d(self.geometry, x1, y1, x2, y2)
     
     def line3d(self, x1, y1, z1, x2, y2, z2):
-        return bresenham.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
+        return line.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
     
     def triangle(self, xa, ya, xh, yh, iAngle):
         return triangle.triangle(self.geometry, xa, ya, xh, yh, iAngle)
@@ -86,7 +86,20 @@ class Grid(object):
     def path(self, x1, y1, x2, y2):
         return pathfinder.path( self, (x1, y1), (x2,y2) )
     
+    def zone(self, x, y):
+        return zone.zone(self.geometry, x, y)
     
+class HexGrid(Grid):
+    def __init__(self, width, height):
+        Grid.__init__(5, width, height)
+
+class SquareGrid(Grid):
+    def __init__(self, width, height):
+        Grid.__init__(4, width, height)
+
+
+
+
     
     
 if __name__ == '__main__':
@@ -96,8 +109,8 @@ if __name__ == '__main__':
     print(gr.line(1,1,5,10))
     print(gr.line3d(1,1,1,5,10,10))
     
-    print(gr.triangle(1,1,5,10,ANGLES[1]))
-    print(gr.triangle3d(1,1,1,5,10,10, ANGLES[1]))
+    print(gr.triangle(1,1,5,10,triangle.ANGLES[1]))
+    print(gr.triangle3d(1,1,1,5,10,10, triangle.ANGLES[1]))
     
     print(gr.path(1,1,5,10))
     

+ 0 - 0
core/geometry/__init__.py


+ 0 - 0
core/cube_coords.py → core/geometry/cube_coords.py


+ 0 - 0
core/bresenham.py → core/geometry/line.py


+ 27 - 0
core/geometry/neighbours.py

@@ -0,0 +1,27 @@
+'''
+Created on 19 nov. 2016
+
+@author: olinox
+'''
+
+def neighbours_of(geometry, x, y):
+    if geometry == 4:
+        return squ_neighbours_of(x, y)
+    elif geometry == 5: 
+        return hex_neighbours_of(x, y)
+    else:
+        raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
+
+def hex_neighbours_of(x, y):
+    """ returns the list of coords of the neighbours of a cell on an hexagonal grid"""
+    if x%2 == 0:
+        return [(x, y-1), (x+1, y-1), (x+1, y), (x,  y+1), (x-1, y), (x-1, y-1)]
+    else:
+        return [(x, y-1), (x+1, y), (x+1, y+1), (x,  y+1), (x-1, y+1), (x-1, y)]        
+
+def squ_neighbours_of(x, y):
+    """ returns the list of coords of the neighbours of a cell on an square grid"""
+    return [(x-1, y-1), (x, y-1), (x+1, y-1), \
+            (x-1, y)  , (x, y-1), (x+1, y)  , \
+            (x-1, y+1), (x, y+1),(x+1, y+1)]
+    

+ 0 - 0
core/rectangle.py → core/geometry/rectangle.py


+ 9 - 8
core/triangle.py → core/geometry/triangle.py

@@ -6,8 +6,9 @@ Created on 8 nov. 2016
 
 from math import sqrt
 
-from core import bresenham, constants
-from core.cube_coords import cv_off_cube, cube_round, cv_cube_off
+from core import constants
+from core.geometry import line
+from core.geometry.cube_coords import cv_off_cube, cube_round, cv_cube_off
 
 
 ANGLES = [1, 2, 3]
@@ -61,7 +62,7 @@ def triangle_sq(xa, ya, xh, yh, iAngle):
     
     # base (lower slope)
     x1, y1, x2, y2 = min(lines, key=lambda x: (abs ( (x[3] - x[1]) / (x[2] - x[0]) ) if x[2] != x[0] else 10**10))
-    base = bresenham.squ_2d_line(x1, y1, x2, y2)
+    base = line.squ_2d_line(x1, y1, x2, y2)
     y_base = y1
     lines.remove( (x1, y1, x2, y2) )
     
@@ -71,7 +72,7 @@ def triangle_sq(xa, ya, xh, yh, iAngle):
     for x1, y1, x2, y2 in lines:
         if y_top == None: 
             y_top = y2
-        hat.extend( bresenham.squ_2d_line(x1, y1, x2, y2) )
+        hat.extend( line.squ_2d_line(x1, y1, x2, y2) )
     
     # sense (1 if top is under base, -1 if not)
     sense = 1 if y_top > y_base else -1
@@ -102,7 +103,7 @@ def triangle_sq_3d(xa, ya, za, xh, yh, zh, iAngle):
 
     length = max( abs(xh - xa), abs(yh - ya) )
 
-    vertical_line = bresenham.squ_2d_line(0, za, length, zh)
+    vertical_line = line.squ_2d_line(0, za, length, zh)
     
     #on cree un dictionnaire ou x est la cle, et ou la valeur est une liste de z
     vertical_line_dict = {d:[] for d, z in vertical_line}
@@ -163,7 +164,7 @@ def triangle_hex(xa, ya, xh, yh, iAngle):
     
     # base (lower slope)
     x1, y1, x2, y2 = min(segments, key=lambda x: (abs ( (x[3] - x[1]) / (x[2] - x[0]) ) if x[2] != x[0] else 10**10))
-    base = bresenham.hex_2d_line(x1, y1, x2, y2)
+    base = line.hex_2d_line(x1, y1, x2, y2)
     y_base = y1
     segments.remove( (x1, y1, x2, y2) )
     
@@ -173,7 +174,7 @@ def triangle_hex(xa, ya, xh, yh, iAngle):
     for x1, y1, x2, y2 in segments:
         if y_sommet == None: 
             y_sommet = y2
-        chapeau.extend( bresenham.hex_2d_line(x1, y1, x2, y2) )
+        chapeau.extend( line.hex_2d_line(x1, y1, x2, y2) )
     
     # sense (1 if top is under base, -1 if not)
     sens = 1 if y_sommet > y_base else -1
@@ -204,7 +205,7 @@ def triangle_hex_3d(xa, ya, za, xh, yh, zh, iAngle):
     
     length = max( abs(xuh - xua), abs(yuh - yua), abs(zuh - zua) )
 
-    vertical_line = bresenham.squ_2d_line(0, za, length, zh)
+    vertical_line = line.squ_2d_line(0, za, length, zh)
     
     #on cree un dictionnaire ou x est la cle, et ou la valeur est une liste de z
     vertical_line_dict = {d:[] for d, z in vertical_line}

+ 19 - 0
core/geometry/zone.py

@@ -0,0 +1,19 @@
+'''
+Created on 19 nov. 2016
+
+@author: olinox
+'''
+from core.geometry import neighbours
+
+
+def zone(geometry, x0, y0, radius):
+    """ returns the list of the coordinates of the cells in the zone around (x0, y0)
+    """
+    buffer = frozenset( [ (x0, y0) ] )
+        
+    for _ in range(0, radius):
+        current = buffer
+        for x, y in current:
+            buffer |= frozenset( neighbours.neighbours_of( geometry, x, y ) )
+
+    return list(buffer)

+ 0 - 0
core/pathfinder/__init__.py


+ 1 - 1
core/pathfinder.py → core/pathfinder/pathfinder.py

@@ -3,7 +3,7 @@ Created on 17 déc. 2015
    Implement the A* algorithm
 @author: olivier.massot
 '''
-from core import cube_coords, Cell
+from core.geometry import cube_coords
 
 
 def distance(coord1, coord2):

+ 0 - 0
tests/__init__.py