Mercurial > hg > nsaunier > traffic-intelligence
comparison python/moving.py @ 184:d70e9b36889c
initial work on flow vectors and clustering algorithms
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 25 Nov 2011 18:38:54 -0500 |
| parents | d3f6de6c3918 |
| children | c06379f25ab8 |
comparison
equal
deleted
inserted
replaced
| 183:ed944ff45e8c | 184:d70e9b36889c |
|---|---|
| 126 def __str__(self): | 126 def __str__(self): |
| 127 return '(%f,%f)'%(self.x,self.y) | 127 return '(%f,%f)'%(self.x,self.y) |
| 128 | 128 |
| 129 def __repr__(self): | 129 def __repr__(self): |
| 130 return self.__str__() | 130 return self.__str__() |
| 131 | |
| 132 def __add__(self, other): | |
| 133 return Point(self.x+other.x, self.y+other.y) | |
| 131 | 134 |
| 132 def __sub__(self, other): | 135 def __sub__(self, other): |
| 133 return Point(self.x-other.x, self.y-other.y) | 136 return Point(self.x-other.x, self.y-other.y) |
| 134 | 137 |
| 135 def multiply(self, alpha): | 138 def multiply(self, alpha): |
| 196 | 199 |
| 197 @staticmethod | 200 @staticmethod |
| 198 def plotAll(points, color='r'): | 201 def plotAll(points, color='r'): |
| 199 from matplotlib.pyplot import scatter | 202 from matplotlib.pyplot import scatter |
| 200 scatter([p.x for p in points],[p.y for p in points], c=color) | 203 scatter([p.x for p in points],[p.y for p in points], c=color) |
| 204 | |
| 205 class FlowVector: | |
| 206 '''Class to represent 4-D flow vectors, | |
| 207 ie a position and a velocity''' | |
| 208 def __init__(self, position, velocity): | |
| 209 'position and velocity should be Point instances' | |
| 210 self.position = position | |
| 211 self.velocity = velocity | |
| 212 | |
| 213 def __add__(self, other): | |
| 214 return FlowVector(self.position+other.position, self.velocity+other.velocity) | |
| 215 | |
| 216 def multiply(self, alpha): | |
| 217 return FlowVector(self.position.multiply(alpha), self.velocity.multiply(alpha)) | |
| 218 | |
| 219 def draw(self, options = ''): | |
| 220 from matplotlib.pylab import plot | |
| 221 plot([self.position.x, self.position.x+self.velocity.x], [self.position.y, self.position.y+self.velocity.y], options) | |
| 222 self.position.draw(options+'x') | |
| 223 | |
| 224 @staticmethod | |
| 225 def similar(f1, f2, maxDistance2, maxDeltavelocity2): | |
| 226 return (f1.position-f2.position).norm2Squared()<maxDistance2 and (f1.velocity-f2.velocity).norm2Squared()<maxDeltavelocity2 | |
| 201 | 227 |
| 202 def segmentIntersection(p1, p2, p3, p4): | 228 def segmentIntersection(p1, p2, p3, p4): |
| 203 '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' | 229 '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' |
| 204 from numpy import matrix | 230 from numpy import matrix |
| 205 from numpy.linalg import linalg, det | 231 from numpy.linalg import linalg, det |
