comparison 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
comparison
equal deleted inserted replaced
25:28e546861263 26:54d9cb0c902b
9 9
10 __metaclass__ = type 10 __metaclass__ = type
11 11
12 #class MovingObject: 12 #class MovingObject:
13 13
14 class TimeInterval: 14 class Interval:
15 '''Temporal interval''' 15 '''Generic Interval'''
16 def __init__(self, first=0, last=-1): 16 def __init__(self, first=0, last=-1, revert = False):
17 self.first=first 17 'Warning, do not revert if last<first, it contradicts the definition of empty'
18 self.last=last 18 if revert and last<first:
19 self.first=last
20 self.last=first
21 else:
22 self.first=first
23 self.last=last
19 24
20 def __str__(self): 25 def __str__(self):
21 return '%d %d'%(self.first, self.last) 26 return '%d %d'%(self.first, self.last)
22 27
23 def __iter__(self): 28 def __iter__(self):
31 self.iterInstantNum += 1 36 self.iterInstantNum += 1
32 return self.first+self.iterInstantNum 37 return self.first+self.iterInstantNum
33 38
34 def empty(self): 39 def empty(self):
35 ''' 40 '''
36 >>> TimeInterval().empty() 41 >>> Interval().empty()
37 True 42 True
38 >>> TimeInterval(0,1).empty() 43 >>> Interval(0,1).empty()
39 False 44 False
40 ''' 45 '''
41 return self.first > self.last 46 return self.first > self.last
42 47
43 def length(self): 48 def length(self):
44 '''Returns the length of the interval 49 '''Returns the length of the interval
45 50
46 >>> TimeInterval(0,1).length() 51 >>> Interval(0,1).length()
47 2 52 1
48 >>> TimeInterval(10,8).length() 53 >>> Interval(23.2,24.9).length()
54 1.6999999999999993
55 >>> Interval(10,8).length()
49 0 56 0
50 ''' 57 '''
51 return max(0,self.last-self.first+1) 58 return max(0,self.last-self.first)
52 59
53 def getList(self): 60 def getList(self):
54 return [self.first, self.last] 61 return [self.first, self.last]
55 62
56 def contains(self, instant): 63 def contains(self, instant):
66 73
67 def intersection(self, interval2): 74 def intersection(self, interval2):
68 '''Largest interval comprising self and interval2''' 75 '''Largest interval comprising self and interval2'''
69 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last)) 76 return TimeInterval(max(self.first, interval2.first), min(self.last, interval2.last))
70 77
78 def TimeInterval(Interval):
79 '''Temporal interval'''
80
81 def __init__(self, first=0, last=-1):
82 Interval.__init__(self, first, last, False)
83
84 def length(self):
85 '''Returns the length of the interval
86
87 >>> TimeInterval(0,1).length()
88 2
89 >>> TimeInterval(10,8).length()
90 0
91 '''
92 return max(0,self.last-self.first+1)
71 93
72 # class BoundingPolygon: 94 # class BoundingPolygon:
73 # '''Class for a polygon bounding a set of points 95 # '''Class for a polygon bounding a set of points
74 # with methods to create intersection, unions... 96 # with methods to create intersection, unions...
75 # ''' 97 # '''