1 """Imago geometry module"""
3 from math import sin, cos, atan, pi
6 def __init__(self, x, y):
10 def __add__(self, other):
11 return V(self.x + other.x, self.y + other.y)
13 def __sub__(self, other):
14 return V(self.x - other.x, self.y - other.y)
16 def __rmul__(self, other):
17 return V(other * self.x, other * self.y)
22 def __getitem__(self, key):
27 elif type(key) != int:
28 raise TypeError("V indices must be integers")
30 raise KeyError("V index ({}) out of range".format(key))
38 return V(-self.y, self.x)
40 def projection(p, l, v):
41 return V(*intersection(line(p, p + v.normal), line(*l)))
43 def l2ad((a, b), size):
44 """Represent line as (angle, distance).
46 Take a line (represented by two points) and image size.
47 Return the line represented by its angle and distance
48 from the center of the image.
51 if (a[0] - b[0]) == 0:
54 q = float(a[1] - b[1]) / (a[0] - b[0])
62 distance = (((a[0] - (size[0] / 2)) * sin(angle)) +
63 ((a[1] - (size[1] / 2)) * - cos(angle)))
64 return (angle, distance)
67 """Return parametric representation of line."""
70 c = a * y[0] + b * y[1]
73 def intersection(p, q):
74 """Return intersection of two lines."""
75 det = p[0] * q[1] - p[1] * q[0]
78 return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)),
79 int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))