less parameters for manual.lines
[imago.git] / manual.py
index 658f601..83e024e 100644 (file)
--- a/manual.py
+++ b/manual.py
@@ -2,6 +2,7 @@
 
 import pygame
 import Image, ImageDraw
 
 import pygame
 import Image, ImageDraw
+from math import atan, sin, cos, pi
 
 class UserQuitError(Exception):
     pass
 
 class UserQuitError(Exception):
     pass
@@ -10,7 +11,7 @@ class Screen:
     def __init__(self, res):
         pygame.init()
         pygame.display.set_mode(res)
     def __init__(self, res):
         pygame.init()
         pygame.display.set_mode(res)
-        pygame.display.set_caption("Go image capture")
+        pygame.display.set_caption("Imago manual mode")
         self._screen = pygame.display.get_surface()
 
     def display_picture(self, im):
         self._screen = pygame.display.get_surface()
 
     def display_picture(self, im):
@@ -31,19 +32,24 @@ def find_lines(im_orig):
                  fill=(255, 64, 64))
     corners = []
     color=(64, 64, 255)
                  fill=(255, 64, 64))
     corners = []
     color=(64, 64, 255)
-    line_width = 2
+    line_width = 1
+    lines_r = []
+
     while not done:
         for event in pygame.event.get():
     while not done:
         for event in pygame.event.get():
-            if event.type == pygame.QUIT:
+            if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                 pygame.quit()
                 pygame.quit()
-                raise UserQuitError 
+                if len(corners) == 4:
+                    return lines_r
+                else:
+                    raise UserQuitError 
             if event.type == pygame.MOUSEBUTTONDOWN:
             if event.type == pygame.MOUSEBUTTONDOWN:
-               if len(corners) >= 4: 
+                if len(corners) >= 4: 
                     corners = []
                     im = im_orig.copy()
                     draw = ImageDraw.Draw(im)
 
                     corners = []
                     im = im_orig.copy()
                     draw = ImageDraw.Draw(im)
 
-               if len(corners) < 4:
+                if len(corners) < 4:
                     corners.append(pygame.mouse.get_pos())
                     draw.point(corners[:-1], fill=color)
                     if len(corners) == 4:
                     corners.append(pygame.mouse.get_pos())
                     draw.point(corners[:-1], fill=color)
                     if len(corners) == 4:
@@ -55,27 +61,36 @@ def find_lines(im_orig):
                                   width=line_width)
                         draw.line((corners[3], corners[0]), fill=color,
                                   width=line_width)
                                   width=line_width)
                         draw.line((corners[3], corners[0]), fill=color,
                                   width=line_width)
-                        l_vert = lines(corners, 0)
+                        l_vert = lines(corners)
                         for l in l_vert:
                             draw.line(l, fill=color, width=line_width)
                         for l in l_vert:
                             draw.line(l, fill=color, width=line_width)
-                        l_hor = lines(corners[1:4] + [corners[0]], 0)
+                        l_hor = lines(corners[1:4] + [corners[0]])
                         for l in l_hor:
                             draw.line(l, fill=color, width=line_width)
                         for l in l_hor:
                             draw.line(l, fill=color, width=line_width)
-                        l_vert = sorted(l_vert)
-                        l_hor = sorted(l_hor)
-                        for i in [3, 8, 14]:
-                            for j in [3, 8, 14]:
+                        l_vert += [(corners[0], corners[3]),
+                                   (corners[1], corners[2])]
+                        l_hor += [(corners[0], corners[1]),
+                                   (corners[2], corners[3])]
+                        l_vert.sort()
+                        l_hor.sort()
+                        for i in [3, 9, 15]:
+                            for j in [3, 9, 15]:
                                 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
                                                    line(l_hor[j][0], l_hor[j][1])))
                                 hoshi(intersection(line(l_vert[i][0], l_vert[i][1]),
                                                    line(l_hor[j][0], l_hor[j][1])))
+                        lines_r = [[l2ad(l[0], l[1], im.size) for l in l_vert], 
+                                   [l2ad(l[0], l[1], im.size) for l in l_hor]]
 
         screen.display_picture(im)
         clock.tick(15)
 
 
         screen.display_picture(im)
         clock.tick(15)
 
-def lines(corners, n):
+def lines(corners):
+    return _lines(corners, 0)
+
+def _lines(corners, n):
     if n == 0:
         x = half_line(corners)
     if n == 0:
         x = half_line(corners)
-        return (lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + 
-                lines([x[0], corners[1], corners[2], x[1]], n + 1))
+        return (_lines([corners[0], x[0], x[1], corners[3]], n + 1) + [x] + 
+                _lines([x[0], corners[1], corners[2], x[1]], n + 1))
     else:
         x = half_line(corners)
         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
     else:
         x = half_line(corners)
         c = intersection(line(x[0], corners[2]), line(corners[1], corners[3]))
@@ -84,9 +99,9 @@ def lines(corners, n):
              intersection(line(corners[2], corners[3]), line(c,d)))
         l2 = half_line([corners[0], l[0], l[1], corners[3]])
         if n == 1:
              intersection(line(corners[2], corners[3]), line(c,d)))
         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))
+            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]
 
         if n == 2:
             return [l, l2]
 
@@ -119,3 +134,19 @@ def intersection(p, q):
         return None
     return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)), 
             int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))
         return None
     return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)), 
             int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))
+
+def l2ad(a, b, size):
+    if (a[0] - b[0]) == 0:
+        angle = pi / 2
+    else:
+        q = float(a[1] - b[1]) / (a[0] - b[0])
+        angle = atan(q)
+
+    if angle < 0:
+        angle += pi
+    if angle > pi:
+        angle -= pi
+
+    distance = (((a[0] - (size[0] / 2)) * sin(angle)) + 
+                ((a[1] - (size[1] / 2)) * - cos(angle)))
+    return (angle, distance)