diff python/moving.py @ 26:54d9cb0c902b

generalized intervals
author Nicolas Saunier <nico@confins.net>
date Sat, 05 Dec 2009 12:31:28 -0500
parents 28e546861263
children 44689029a86f
line wrap: on
line diff
--- a/python/moving.py	Sat Dec 05 12:08:25 2009 -0500
+++ b/python/moving.py	Sat Dec 05 12:31:28 2009 -0500
@@ -11,11 +11,16 @@
 
 #class MovingObject:
 
-class TimeInterval:
-    '''Temporal interval'''
-    def __init__(self, first=0, last=-1):
-        self.first=first
-        self.last=last
+class Interval:
+    '''Generic Interval'''
+    def __init__(self, first=0, last=-1, revert = False):
+        'Warning, do not revert if last<first, it contradicts the definition of empty'
+        if revert and last<first:
+            self.first=last
+            self.last=first
+        else:
+            self.first=first
+            self.last=last
 
     def __str__(self):
         return '%d %d'%(self.first, self.last)
@@ -33,9 +38,9 @@
 
     def empty(self):
         '''
-        >>> TimeInterval().empty()
+        >>> Interval().empty()
         True
-        >>> TimeInterval(0,1).empty()
+        >>> Interval(0,1).empty()
         False
         '''
         return self.first > self.last
@@ -43,12 +48,14 @@
     def length(self):
         '''Returns the length of the interval
         
-        >>> TimeInterval(0,1).length()
-        2
-        >>> TimeInterval(10,8).length()
+        >>> Interval(0,1).length()
+        1
+        >>> Interval(23.2,24.9).length()
+        1.6999999999999993
+        >>> Interval(10,8).length()
         0
         '''
-        return max(0,self.last-self.first+1)
+        return max(0,self.last-self.first)
 
     def getList(self):
         return [self.first, self.last]
@@ -68,6 +75,21 @@
         '''Largest interval comprising self and interval2'''
         return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last))
 
+def TimeInterval(Interval):
+    '''Temporal interval'''
+
+    def __init__(self, first=0, last=-1):
+        Interval.__init__(self, first, last, False)
+
+    def length(self):
+        '''Returns the length of the interval
+        
+        >>> TimeInterval(0,1).length()
+        2
+        >>> TimeInterval(10,8).length()
+        0
+        '''
+        return max(0,self.last-self.first+1)
 
 # class BoundingPolygon:
 #     '''Class for a polygon bounding a set of points