parameters
[imago.git] / src / manual.py
index b761f5e..c00881b 100644 (file)
@@ -5,6 +5,7 @@ from PIL import ImageDraw
 import pygame
 
 from geometry import l2ad, line, intersection
+from manual_lines import *
 
 class UserQuitError(Exception):
     pass
@@ -22,8 +23,6 @@ class Screen:
         self._screen.blit(pg_img, (0, 0))
         pygame.display.flip()
 
-def dst((x1, y1), (x2, y2)):
-    return (x1 - x2) ** 2 + (y1 - y2) ** 2
 
 def display_instr():
 
@@ -71,6 +70,9 @@ def find_lines(im_orig):
     line_width = 1
     lines_r = []
 
+    def dst((x1, y1), (x2, y2)):
+        return (x1 - x2) ** 2 + (y1 - y2) ** 2
+
     while True:
         for event in pygame.event.get():
             if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
@@ -91,7 +93,10 @@ def find_lines(im_orig):
                 if len(corners) == 4:
                     im = im_orig.copy()
                     draw = ImageDraw.Draw(im)
-                    l_vert, l_hor = lines(corners)
+                    try:
+                        l_vert, l_hor = lines(corners)
+                    except Exception:
+                        corners = corners[:-1]
                     for l in l_vert:
                         draw.line(l, fill=color, width=line_width)
                     for l in l_hor:
@@ -109,62 +114,4 @@ def find_lines(im_orig):
         screen.display_picture(im)
         clock.tick(15)
 
-def lines(corners):
-    # TODO Error on triangle 
-    corners.sort() # TODO does this help?
-    # TODO refactor this vvv
-    cor_d = [(corners[0], (c[0] - corners[0][0], c[1] - corners[0][1]), c) for c in
-             corners[1:]]
-    cor_d = [(float(a[0] * b[0] + a[1] * b[1]) / (sqrt(a[0] ** 2 + a[1] ** 2) *
-              sqrt(b[0] **2 + b[1] ** 2)), a[0] * b[1] - b[0] * a[1], c) for a, b, c in cor_d]
-    cor_d = sorted([(copysign(acos(min(a, 1)), b), c) for a, b, c in cor_d])
-    corners = [corners[0]] + [c for _, c in cor_d]
-    return (_lines(corners, 0) + 
-            [(corners[0], corners[3]), (corners[1], corners[2])],
-            _lines(corners[1:4] + [corners[0]], 0) + 
-            [(corners[0], corners[1]), (corners[2], corners[3])])
-
-def _lines(corners, n):
-    # TODO what is this?
-    if n == 0:
-        x = half_line(corners)
-        return (_lines([corners[0], x[0], x[1], corners[3]], 1) + [x] + 
-                _lines([x[0], corners[1], corners[2], x[1]], 1))
-    else:
-        x = half_line(corners)
-        c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
-        d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
-        if d:
-            l = (intersection(line(corners[0], corners[1]), line(c, d)),
-                 intersection(line(corners[2], corners[3]), line(c, d)))
-        else:
-            lx = line(c, (c[0] + corners[0][0] - corners[3][0], 
-                      c[1] + corners[0][1] - corners[3][1]))
-            l = (intersection(line(corners[0], corners[1]), lx),
-                 intersection(line(corners[2], corners[3]), lx))
-        l2 = half_line([corners[0], l[0], l[1], corners[3]])
-        if n == 1:
-            return ([l, l2] + _lines([l[0], l2[0], l2[1], l[1]], 2)
-                    + _lines([corners[0], l2[0], l2[1], corners[3]], 2)
-                    + _lines([l[0], corners[1], corners[2], l[1]], 2))
-        if n == 2:
-            return [l, l2]
-
-
-def half_line(corners):
-    # TODO what is this?
-    c = center(corners)
-    d = intersection(line(corners[0], corners[3]), line(corners[1], corners[2]))
-    if d:
-        l = line(c, d)
-    else:
-        l = line(c, (c[0] + corners[0][0] - corners[3][0], 
-                     c[1] + corners[0][1] - corners[3][1]))
-    p1 = intersection(l, line(corners[0], corners[1]))
-    p2 = intersection(l, line(corners[2], corners[3]))
-    return (p1, p2)
-
-def center(corners):
-    """Given a list of four corner points, return the center of the square."""
-    return intersection(line(corners[0], corners[2]), 
-                        line(corners[1], corners[3]))
+