Browse Source

begin pencils classes

olinox 9 years ago
parent
commit
41b2922634

+ 1 - 1
core/geometry/gneighbours.py

@@ -24,6 +24,6 @@ def hex_neighbours_of(x, 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), (x+1, y)  , \
             (x-1, y+1), (x, y+1),(x+1, y+1)]
     

+ 0 - 0
core/pencil/__init__.py


+ 62 - 0
core/pencil/pbase.py

@@ -0,0 +1,62 @@
+'''
+Created on 25 nov. 2016
+
+@author: olinox
+'''
+
+class BasePencil(object):
+    
+    def __init__(self, grid):
+        self._grid = grid
+        
+        self._origin = None
+        self._position = None
+        
+        self._size = 1
+        self._selection = []
+
+        self._added = []
+        self._removed = []
+
+    @property
+    def origin(self):
+        return self._coord0
+
+    @origin.setter
+    def origin(self, x, y):
+        self._origin = (x, y)
+
+    @property
+    def size(self):
+        return self._size
+
+    @size.setter
+    def size(self, size):
+        if not size > 0:
+            raise ValueError("size has to be strictly positive")
+        self._size = size
+
+    @property
+    def position(self):
+        return self._position
+
+    @position.setter
+    def position(self, x, y):
+        self._position = (x, y)
+        self._update()
+
+    @property
+    def selection(self):
+        return self._selection
+        
+    @property
+    def added(self):
+        return self._added
+
+    @property
+    def removed(self):
+        return self._removed
+
+    def _update(self):
+        pass
+        

+ 25 - 0
core/pencil/pline.py

@@ -0,0 +1,25 @@
+'''
+Created on 25 nov. 2016
+
+@author: olinox
+'''
+from core import geometry
+from core.pencil.pbase import BasePencil
+
+
+class LinePencil(BasePencil):
+    def __init__(self, *args):
+        BasePencil.__init__(*args)
+        
+    def _update(self):
+        x0, y0 = self.origin
+        x, y = self.position
+        
+        result = set([])
+        line = geometry.gline.line2d(self._grid.grid_shape, x0, y0, x, y)
+        for x, y in line:
+            result |= set( geometry.gzone.zone(self._grid.grid_shape, x, y, self.size) )
+        
+        self._added = list( result - self._selection )
+        self._removed = list( self._selection - result ) 
+        self._selection = list( result )

+ 19 - 0
core/pencil/psimple.py

@@ -0,0 +1,19 @@
+'''
+Created on 25 nov. 2016
+
+@author: olinox
+'''
+from core import geometry
+from core.pencil.pbase import BasePencil
+
+
+class SimplePencil(BasePencil):
+    def __init__(self, *args):
+        BasePencil.__init__(*args)
+        
+    def _update(self):
+        x, y = self.position
+        zone = geometry.gzone.zone(self._grid.grid_shape, x, y, self.size)
+
+        self._added = list( set(zone) - set(self._selection) )
+        self._selection = list( set(self._selection) + set(zone))

+ 30 - 0
tests/geometry/test_neighbours.py

@@ -0,0 +1,30 @@
+'''
+Created on 25 nov. 2016
+
+@author: olinox
+'''
+import unittest
+
+from core import geometry
+from core.geometry import gneighbours
+
+
+class Test(unittest.TestCase):
+
+
+    def test_neighbours_of(self):
+        for coord in ( (0,0), (-10,-10), (10,10) ):
+            x, y = coord
+            self.assertEqual( gneighbours.neighbours_of(geometry.HEX, x, y), gneighbours.hex_neighbours_of(x, y) )
+            self.assertEqual( gneighbours.neighbours_of(geometry.SQUARE, x, y), gneighbours.squ_neighbours_of(x, y) )
+
+    def test_hex_neighbours_of(self):
+        self.assertCountEqual( gneighbours.hex_neighbours_of(3,3), [(3,2), (4,3), (4,4), (3,4), (2,4), (2,3)] )
+        self.assertCountEqual( gneighbours.hex_neighbours_of(4,4), [(4,3), (5,3), (5,4), (4,5), (3,4), (3,3)] )
+
+    def test_squ_neighbours_of(self):
+        self.assertCountEqual( gneighbours.squ_neighbours_of(3,3), [(2,3), (2,2), (3,2), (4,2), (4,3), (4,4), (3,4), (2,4)] )
+        
+
+if __name__ == "__main__":
+    unittest.main()