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):
43 return V(*intersection(line(p, p + v.normal), line(*l)))
45 def l2ad((a, b), size):
46 """Represent line as (angle, distance).
48 Take a line (represented by two points) and image size.
49 Return the line represented by its angle and distance
50 from the center of the image.
53 if (a[0] - b[0]) == 0:
56 q = float(a[1] - b[1]) / (a[0] - b[0])
64 distance = (((a[0] - (size[0] / 2)) * sin(angle)) +
65 ((a[1] - (size[1] / 2)) * - cos(angle)))
66 return (angle, distance)
69 """Return parametric representation of line."""
72 c = a * y[0] + b * y[1]
75 def intersection(p, q):
76 """Return intersection of two lines."""
77 det = p[0] * q[1] - p[1] * q[0]
80 return (int(round(float(q[1] * p[2] - p[1] * q[2]) / det)),
81 int(round(float(p[0] * q[2] - q[0] * p[2]) / det)))