Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 23:5f2921ad4f7e
made Trajectory indexable and timeinterval iterable
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Fri, 04 Dec 2009 13:47:22 -0500 |
parents | 169cd4679366 |
children | 28e546861263 |
comparison
equal
deleted
inserted
replaced
22:169cd4679366 | 23:5f2921ad4f7e |
---|---|
17 self.first=first | 17 self.first=first |
18 self.last=last | 18 self.last=last |
19 | 19 |
20 def __str__(self): | 20 def __str__(self): |
21 return '%d %d'%(self.first, self.last) | 21 return '%d %d'%(self.first, self.last) |
22 | |
23 def __iter__(self): | |
24 self.iterInstantNum = 0 | |
25 return self | |
26 | |
27 def next(self): | |
28 if self.iterInstantNum >= self.length(): | |
29 raise StopIteration | |
30 else: | |
31 self.iterInstantNum += 1 | |
32 return self.first+self.iterInstantNum | |
22 | 33 |
23 def empty(self): | 34 def empty(self): |
24 ''' | 35 ''' |
25 >>> TimeInterval().empty() | 36 >>> TimeInterval().empty() |
26 True | 37 True |
96 self.positions = positions | 107 self.positions = positions |
97 | 108 |
98 def __str__(self): | 109 def __str__(self): |
99 return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1])) | 110 return ' '.join(map(utils.printPoint, self.positions[0], self.positions[1])) |
100 | 111 |
112 def __getitem__(self, i): | |
113 return [self.positions[0][i], self.positions[1][i]] | |
114 | |
101 def __iter__(self): | 115 def __iter__(self): |
102 self.iterInstantNum = 0 | 116 self.iterInstantNum = 0 |
103 return self | 117 return self |
104 | 118 |
105 def next(self): | 119 def next(self): |
106 if self.iterInstantNum >= self.length(): | 120 if self.iterInstantNum >= self.length(): |
107 self.iterInstantNum = 0 | |
108 raise StopIteration | 121 raise StopIteration |
109 else: | 122 else: |
110 self.iterInstantNum += 1 | 123 self.iterInstantNum += 1 |
111 return self.getPosition(self.iterInstantNum-1) | 124 return self[self.iterInstantNum-1] |
112 | |
113 def getPosition(self, i): | |
114 return [self.positions[0][i], self.positions[1][i]] | |
115 | 125 |
116 def addPosition(self, point): | 126 def addPosition(self, point): |
117 if not self.positions: | 127 if not self.positions: |
118 self.positions = [[point[0]],[point[1]]] | 128 self.positions = [[point[0]],[point[1]]] |
119 else: | 129 else: |
123 def draw(self): | 133 def draw(self): |
124 from matplotlib.pylab import plot | 134 from matplotlib.pylab import plot |
125 plot(self.positions[0], self.positions[1]) | 135 plot(self.positions[0], self.positions[1]) |
126 | 136 |
127 def length(self): | 137 def length(self): |
128 return len(self.getXCoordinates()) | 138 return len(self.positions[0]) |
129 | 139 |
130 def getXCoordinates(self): | 140 def getXCoordinates(self): |
131 return self.positions[0] | 141 return self.positions[0] |
132 | 142 |
133 def getYCoordinates(self): | 143 def getYCoordinates(self): |