Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 24:6fb59cfb201e
first version of segmentIntersection
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Sat, 05 Dec 2009 11:04:03 -0500 |
parents | 5a21d2cfee44 |
children | 44689029a86f |
comparison
equal
deleted
inserted
replaced
23:5f2921ad4f7e | 24:6fb59cfb201e |
---|---|
5 #from pylab import * | 5 #from pylab import * |
6 | 6 |
7 __metaclass__ = type | 7 __metaclass__ = type |
8 | 8 |
9 commentChar = '#'; | 9 commentChar = '#'; |
10 | |
11 # maths | |
12 | |
13 def segmentIntersection(s1, s2): | |
14 '''Returns the intersecting point, None otherwise | |
15 format for a segment is [[x1, x2], [y1,y2]] | |
16 | |
17 >>> segmentIntersection([[0, 1], [0,1]], [[0, 1], [1,2]]) | |
18 >>> segmentIntersection([[0, 1], [1,0]], [[0, 1], [2,1]]) | |
19 >>> segmentIntersection([[0, 2], [0,0]], [[1, 1], [-1,1]]) | |
20 [1.0, 0.0] | |
21 >>> segmentIntersection([[0, 2], [0,0]], [[1, 1], [1,2]]) | |
22 ''' | |
23 from numpy import matrix | |
24 from numpy.linalg import linalg, det | |
25 | |
26 ds1 = [s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] | |
27 ds2 = [s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] | |
28 | |
29 A = matrix([[ds1[1], -ds1[0]], | |
30 [ds2[1], -ds2[0]]]) | |
31 B = matrix([[ds1[1]*s1[0][0]-ds1[0]*s1[1][0]], | |
32 [ds2[1]*s2[0][0]-ds2[0]*s2[1][0]]]) | |
33 | |
34 if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: | |
35 return None | |
36 else: | |
37 intersection = linalg.solve(A,B) | |
38 if (intersection[0,0] >= s1[0][0] and intersection[0,0] <= s1[0][1] | |
39 and intersection[0,0] >= s2[0][0] and intersection[0,0] <= s2[0][1] | |
40 and intersection[1,0] >= s1[1][0] and intersection[1,0] <= s1[1][1] | |
41 and intersection[1,0] >= s2[1][0] and intersection[1,0] <= s2[1][1]): | |
42 return [intersection[0,0], intersection[1,0]] | |
43 else: | |
44 return None | |
45 | |
46 def crossProduct(l1, l2): | |
47 return l1[0]*l2[1]-l1[1]*l2[0] | |
48 | |
49 # file I/O | |
10 | 50 |
11 def openCheck(filename, option = 'r', quit = False): | 51 def openCheck(filename, option = 'r', quit = False): |
12 '''Open file filename in read mode by default | 52 '''Open file filename in read mode by default |
13 and checks it is open | 53 and checks it is open |
14 | 54 |