olinox пре 8 година
родитељ
комит
53a8db5f69
2 измењених фајлова са 21 додато и 14 уклоњено
  1. 19 12
      pypog/geometry_objects.py
  2. 2 2
      tests/test_geometry.py

+ 19 - 12
pypog/geometry_objects.py

@@ -238,29 +238,31 @@ class SquareGeometry(BaseGeometry):
         """ reimplemented from BaseGeometry.line
         Implementation of bresenham's algorithm
         """
+        # check the arguments
         cls.assertCoordinates((x1, y1), (x2, y2))
+
+        # special case
         if (x1, y1) == (x2, y2):
             return [(x1, y1)]
 
-        result = []
-
         # diagonal symmetry
         vertically_oriented = (abs(y2 - y1) > abs(x2 - x1))
         if vertically_oriented:
             y1, x1, y2, x2 = x1, y1, x2, y2
 
-        # vertical symmetry
+        # horizontal symmetry
         reversed_sym = (x1 > x2)
         if reversed_sym:
             x2, y2, x1, y1 = x1, y1, x2, y2
 
-        # Compute
-        dx = x2 - x1
-        dy = y2 - y1
+        # angle
+        dx, dy = x2 - x1, y2 - y1
         alpha = (abs(dy) / dx)
+
         offset = 0.0
         step = 1 if dy > 0 else -1
 
+        result = []
         y = y1
         for x in range(x1, x2 + 1):
             if vertically_oriented:
@@ -459,20 +461,23 @@ class FHexGeometry(HexGeometry):
         if (x1, y1) == (x2, y2):
             return [(x1, y1)]
 
+        # vertical symmetry
         reversed_sym = (x1 > x2)
         if reversed_sym:
-            x1, x2 = x2, x1
-            y1, y2 = y2, y1
+            x2, y2, x1, y1 = x1, y1, x2, y2
+
+
+        # The unit that will be used is half the width of an hexagon: u = 0.5773
+        # In that system, half-height of an hexagon is 0.8860u, or sqrt(3)/2 * u
 
         if abs(x2 - x1) < (2 * abs((y2 - y1)) + abs(x2 % 2) - abs(x1 % 1)):
             # vertical quadrants
 
-            # unit is half the width: u = 0.5773
-            # half-height is then 0.8860u, or sqrt(3)/2
             direction = 1 if y2 > y1 else -1
 
             dx = 1.5 * (x2 - x1)
             dy = direction * (y2 - y1)
+
             if (x1 + x2) % 2 == 1:
                 if x1 % 2 == 0:
                     dy += direction * 0.5
@@ -480,7 +485,7 @@ class FHexGeometry(HexGeometry):
                     dy -= direction * 0.5
 
             k = dx / (dy * sqrt(3))
-            pas = 0.5 * sqrt(3)
+            pas = sqrt(3) / 2
 
             result = []
             offset = 0.0
@@ -510,7 +515,9 @@ class FHexGeometry(HexGeometry):
 
         else:
             # horizontal quadrants
-            dx = x2 - x1 ; dy = y2 - y1
+            dx = x2 - x1
+            dy = y2 - y1
+
             if (x1 + x2) % 2 == 1:
                 dy += 0.5 if x1 % 2 == 0 else -0.5
 

+ 2 - 2
tests/test_geometry.py

@@ -13,7 +13,7 @@ from pypog.geometry_objects import FHexGeometry, SquareGeometry, BaseGeometry, \
 class Test(unittest.TestCase):
 
     def setUp(self):
-        SquareGeometry.set_no_diagonals(False)
+        SquareGeometry.set_no_diags(False)
 
     def test_bounding_rect(self):
         br = BoundingRect(0, 1, 10, 11)
@@ -63,7 +63,7 @@ class Test(unittest.TestCase):
         self.assertCountEqual(FHexGeometry.neighbors(4, 4), [(4, 3), (5, 3), (5, 4), (4, 5), (3, 4), (3, 3)])
 
         self.assertCountEqual(SquareGeometry.neighbors(3, 3), [(2, 3), (2, 2), (3, 2), (4, 2), (4, 3), (4, 4), (3, 4), (2, 4)])
-        SquareGeometry.set_no_diagonals(True)
+        SquareGeometry.set_no_diags(True)
         self.assertCountEqual(SquareGeometry.neighbors(3, 3), [(2, 3), (3, 2), (4, 3), (3, 4)])
 
     def test_zone(self):