1 """Imago geometry module."""
3 from math import sin, cos, atan, pi
6 """Class for vector manipulation."""
8 def __init__(self, x, y):
12 def __add__(self, other):
13 return V(self.x + other.x, self.y + other.y)
15 def __sub__(self, other):
16 return V(self.x - other.x, self.y - other.y)
18 def __rmul__(self, other):
19 return V(other * self.x, other * self.y)
24 def __getitem__(self, key):
29 elif type(key) != int:
30 raise TypeError("V indices must be integers")
32 raise KeyError("V index ({}) out of range".format(key))
40 return V(-self.y, self.x)
42 def projection(p, l, v):
44 return V(*intersection(line(p, p + v.normal), line(*l)))
46 def l2ad((a, b), size):
47 """Represent line as (angle, distance).
49 Take a line (represented by two points) and image size.
50 Return the line represented by its angle and distance
51 from the center of the image.
54 if (a[0] - b[0]) == 0:
57 q = float(a[1] - b[1]) / (a[0] - b[0])
65 distance = (((a[0] - (size[0] / 2)) * sin(angle)) +
66 ((a[1] - (size[1] / 2)) * - cos(angle)))
67 return (angle, distance)
70 """Return parametric representation of line."""
73 c = a * y[0] + b * y[1]
76 def intersection(p, q):
77 """Return intersection of two lines."""
78 det = p[0] * q[1] - p[1] * q[0]
81 return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)),
82 int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))