lines cache
[imago.git] / geometry.py
1 """Imago geometry module"""
2
3 from manual import line, intersection
4
5 class V(object):
6     def __init__(self, x, y):
7         self.x = x
8         self.y = y
9     
10     def __add__(self, other):
11         return V(self.x + other.x, self.y + other.y)
12
13     def __sub__(self, other):
14         return V(self.x - other.x, self.y - other.y)
15
16     def __rmul__(self, other):
17         return V(other * self.x, other * self.y)
18
19     def __len__(self):
20         return 2
21
22     def __getitem__(self, key):
23         if key == 0:
24             return self.x
25         elif key == 1:
26             return self.y
27         elif type(key) != int:
28             raise TypeError("V indices must be integers") 
29         else:
30             raise KeyError("V index ({}) out of range".format(key))
31
32     def __iter__(self):
33         yield self.x
34         yield self.y
35
36     @property
37     def normal(self):
38         return V(-self.y, self.x)
39
40 def projection(p, l, v):
41     return V(*intersection(line(p, p + v.normal), line(*l)))