tuikit/core/coords.py
changeset 106 abcadb7e2ef1
parent 104 742e504ec053
child 107 1822c37b2688
--- a/tuikit/core/coords.py	Mon Sep 01 08:55:40 2014 +0200
+++ b/tuikit/core/coords.py	Wed Sep 03 08:53:44 2014 +0200
@@ -1,3 +1,4 @@
+
 class Point:
 
     """Point in cartesian space.
@@ -11,12 +12,6 @@
         self.y = 0
         self.update(*args, **kwargs)
 
-    def __getitem__(self, key):
-        return (self.x, self.y)[key]
-
-    def __repr__(self):
-        return 'Point(x={0.x},y={0.y})'.format(self)
-
     def move(self, relx, rely):
         self.x += relx
         self.y += rely
@@ -46,6 +41,48 @@
             else:
                 raise ValueError('Bad keyword arg: %r' % key)
 
+    # sequence interface
+
+    def __len__(self):
+        return 2
+
+    def __getitem__(self, key):
+        return (self.x, self.y)[key]
+
+    # point arithmetics
+
+    def __add__(self, other):
+        return Point(self.x + other[0], self.y + other[1])
+
+    def __sub__(self, other):
+        return Point(self.x - other[0], self.y - other[1])
+
+    def __eq__(self, other):
+        """Comparison operator.
+
+        Point can be compared to any sequence of at least two elements:
+
+        >>> p = Point(1, 2)
+        >>> p == Point(1, 2)
+        True
+        >>> p == (1, 2)
+        True
+        >>> p == (0, 0)
+        False
+        >>> p == None
+        False
+
+        """
+        try:
+            return self.x == other[0] and self.y == other[1]
+        except (TypeError, IndexError):
+            return False
+
+    # string representation
+
+    def __repr__(self):
+        return 'Point(x={0.x},y={0.y})'.format(self)
+
 
 class Size: