makefile, readme
[imago.git] / geometry.py
index 09b0199..23f4095 100644 (file)
@@ -1,5 +1,7 @@
 """Imago geometry module"""
 
 """Imago geometry module"""
 
+from math import sin, cos, atan, pi
+
 class V(object):
     def __init__(self, x, y):
         self.x = x
 class V(object):
     def __init__(self, x, y):
         self.x = x
@@ -15,7 +17,7 @@ class V(object):
         return V(other * self.x, other * self.y)
 
     def __len__(self):
         return V(other * self.x, other * self.y)
 
     def __len__(self):
-        return 2;
+        return 2
 
     def __getitem__(self, key):
         if key == 0:
 
     def __getitem__(self, key):
         if key == 0:
@@ -35,5 +37,43 @@ class V(object):
     def normal(self):
         return V(-self.y, self.x)
 
     def normal(self):
         return V(-self.y, self.x)
 
-def projection(point, line, vector):
-    return V(*intersection(g_line(point, point + vector.normal), g_line(*line)))
+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)))