output bug fixed
authorTomas Musil <tomik.musil@gmail.com>
Wed, 5 Dec 2012 21:17:31 +0000 (22:17 +0100)
committerTomas Musil <tomik.musil@gmail.com>
Wed, 5 Dec 2012 21:17:31 +0000 (22:17 +0100)
geometry.py
gridf.py
intrsc.py
manual.py

index b8aea09..23f4095 100644 (file)
@@ -1,6 +1,6 @@
 """Imago geometry module"""
 
-from manual import line, intersection
+from math import sin, cos, atan, pi
 
 class V(object):
     def __init__(self, x, y):
@@ -39,3 +39,41 @@ class V(object):
 
 def projection(p, l, v):
     return V(*intersection(line(p, p + v.normal), line(*l)))
+
+def l2ad((a, b), size):
+    """Represent line as (angle, distance).
+    
+    Take a line (represented by two points) and image size.
+    Return the line represented by its angle and distance
+    from the center of the image.
+
+    """
+    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)
+
+def line(x, y):
+    """Return parametric representation of line."""
+    a = x[1] - y[1]
+    b = y[0] - x[0]
+    c = a * y[0] + b * y[1]
+    return (a, b, c)
+
+def intersection(p, q):
+    """Return intersection of two lines."""
+    det = p[0] * q[1] - p[1] * q[0]
+    if det == 0:
+        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)))
index 58229a4..e957880 100644 (file)
--- a/gridf.py
+++ b/gridf.py
@@ -5,8 +5,8 @@ from functools import partial
 
 import Image, ImageDraw, ImageFilter
 
-from geometry import V, projection
-from manual import lines as g_grid, l2ad
+from geometry import V, projection, l2ad
+from manual import lines as g_grid
 from intrsc import intersections_from_angl_dist
 from linef import line_from_angl_dist
 import pcf
index 00165b1..cefed90 100644 (file)
--- a/intrsc.py
+++ b/intrsc.py
@@ -1,17 +1,36 @@
-from math import cos, tan
+"""Imago intersections module"""
+
+from math import cos, tan, pi
 from operator import itemgetter
 
 import ImageDraw
 
+def dst(line):
+    """Return normalized line."""
+    if line[0] < pi / 2:
+        line = line[0] + pi, - line[1]
+    return line
+
+def dst_sort(lines):
+    """Return lines sorted by distance."""
+    l_max = max(l[0] for l in lines)
+    l_min = min(l[0] for l in lines)
+    if l_max - l_min > (3. / 4) * pi:
+        lines = [dst(l) for l in lines]
+    lines.sort(key=itemgetter(1))
+    return lines
+
 def board(image, lines, show_all, do_something):
+    """Compute intersections, find stone colors and return board situation."""
+    lines = [dst_sort(l) for l in lines]
     intersections = intersections_from_angl_dist(lines, image.size)
-    image_g = image.copy()
-    draw = ImageDraw.Draw(image_g)
-    for line in intersections:
-        for (x, y) in line:
-            draw.point((x , y), fill=(120, 255, 120))
 
     if show_all:
+        image_g = image.copy()
+        draw = ImageDraw.Draw(image_g)
+        for line in intersections:
+            for (x, y) in line:
+                draw.point((x , y), fill=(120, 255, 120))
         do_something(image_g, "intersections")
 
     board_r = []
@@ -21,22 +40,25 @@ def board(image, lines, show_all, do_something):
                       line])
     return board_r
 
-def intersections_from_angl_dist(lines, size, get_all=False):
+def intersections_from_angl_dist(lines, size, get_all=True):
+    """Take grid-lines and size of the image. Return intersections."""
     intersections = []
-    for (angl1, dist1) in sorted(lines[1], key=itemgetter(1)):
+    for (angl1, dist1) in lines[1]:
         line = []
-        for (angl2, dist2) in sorted(lines[0], key=itemgetter(1)):
+        for (angl2, dist2) in lines[0]:
             if abs(angl1 - angl2) > 0.4:
-                x =  (- ((dist2 / cos(angl2)) - (dist1 / cos(angl1))) 
+                i_x =  (- ((dist2 / cos(angl2)) - (dist1 / cos(angl1))) 
                         / (tan(angl1) - tan(angl2)))
-                y = (tan(angl1) * x) - (dist1 / cos(angl1))
-                if get_all or (-size[0] / 2 < x < size[0] / 2 and 
-                    -size[1] / 2 < y < size[1] / 2):
-                    line.append((int(x + size[0] / 2), int(y + size[1] / 2)))
+                i_y = (tan(angl1) * i_x) - (dist1 / cos(angl1))
+                if get_all or (-size[0] / 2 < i_x < size[0] / 2 and 
+                    -size[1] / 2 < i_y < size[1] / 2):
+                    line.append((int(i_x + size[0] / 2),
+                                 int(i_y + size[1] / 2)))
         intersections.append(line)
     return intersections
    
 def stone_color(image, (x, y)):
+    """Given image and coordinates, return stone color."""
     suma = 0.
     for i in range(-2, 3):
         for j in range(-2, 3):
index cecca75..467882e 100644 (file)
--- a/manual.py
+++ b/manual.py
@@ -2,7 +2,8 @@
 
 import pygame
 import ImageDraw
-from math import atan, sin, cos, pi, sqrt, acos, copysign
+from math import sqrt, acos, copysign
+from geometry import l2ad, line, intersection
 
 class UserQuitError(Exception):
     pass
@@ -126,31 +127,3 @@ def half_line(corners):
 def center(corners):
     return intersection(line(corners[0], corners[2]), 
                         line(corners[1], corners[3]))
-def line(x, y):
-    a = x[1] - y[1]
-    b = y[0] - x[0]
-    c = a * y[0] + b * y[1]
-    return (a, b, c)
-
-def intersection(p, q):
-    det = p[0] * q[1] - p[1] * q[0]
-    if det == 0:
-        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)