Mercurial Hosting > traffic-intelligence
comparison python/moving.py @ 152:74b1fc68d4df
re-organized code to avoid cyclic python module dependencies
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Tue, 06 Sep 2011 18:44:23 -0400 |
parents | ad21db62b785 |
children | d3f6de6c3918 |
comparison
equal
deleted
inserted
replaced
151:4af774bb186d | 152:74b1fc68d4df |
---|---|
196 | 196 |
197 @staticmethod | 197 @staticmethod |
198 def plotAll(points, color='r'): | 198 def plotAll(points, color='r'): |
199 from matplotlib.pyplot import scatter | 199 from matplotlib.pyplot import scatter |
200 scatter([p.x for p in points],[p.y for p in points], c=color) | 200 scatter([p.x for p in points],[p.y for p in points], c=color) |
201 | |
202 def segmentIntersection(p1, p2, p3, p4): | |
203 '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' | |
204 from numpy import matrix | |
205 from numpy.linalg import linalg, det | |
206 | |
207 dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] | |
208 dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] | |
209 | |
210 A = matrix([[dp1.y, -dp1.x], | |
211 [dp2.y, -dp2.x]]) | |
212 B = matrix([[dp1.y*p1.x-dp1.x*p1.y], | |
213 [dp2.y*p3.x-dp2.x*p3.y]]) | |
214 | |
215 if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: | |
216 return None | |
217 else: | |
218 intersection = linalg.solve(A,B) | |
219 if (utils.inBetween(p1.x, p2.x, intersection[0,0]) | |
220 and utils.inBetween(p3.x, p4.x, intersection[0,0]) | |
221 and utils.inBetween(p1.y, p2.y, intersection[1,0]) | |
222 and utils.inBetween(p3.y, p4.y, intersection[1,0])): | |
223 return Point(intersection[0,0], intersection[1,0]) | |
224 else: | |
225 return None | |
201 | 226 |
202 class Trajectory: | 227 class Trajectory: |
203 '''Class for trajectories | 228 '''Class for trajectories |
204 i.e. a temporal sequence of positions | 229 i.e. a temporal sequence of positions |
205 | 230 |