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