1
+ import numpy as np
2
+
3
+
4
+ def crossovers (line1 ,line2 ):
5
+
6
+ import numpy .core .umath_tests as ut
7
+
8
+ x_down = line1 [:,0 ]
9
+ y_down = line1 [:,1 ]
10
+ x_up = line2 [:,0 ]
11
+ y_up = line2 [:,1 ]
12
+
13
+ p = np .column_stack ((x_down , y_down ))
14
+ q = np .column_stack ((x_up , y_up ))
15
+
16
+ (p0 , p1 , q0 , q1 ) = p [:- 1 ], p [1 :], q [:- 1 ], q [1 :]
17
+ rhs = q0 - p0 [:, np .newaxis , :]
18
+
19
+ mat = np .empty ((len (p0 ), len (q0 ), 2 , 2 ))
20
+ mat [..., 0 ] = (p1 - p0 )[:, np .newaxis ]
21
+ mat [..., 1 ] = q0 - q1
22
+ mat_inv = - mat .copy ()
23
+ mat_inv [..., 0 , 0 ] = mat [..., 1 , 1 ]
24
+ mat_inv [..., 1 , 1 ] = mat [..., 0 , 0 ]
25
+
26
+ det = mat [..., 0 , 0 ] * mat [..., 1 , 1 ] - mat [..., 0 , 1 ] * mat [..., 1 , 0 ]
27
+ mat_inv /= det [..., np .newaxis , np .newaxis ]
28
+
29
+
30
+ params = ut .matrix_multiply (mat_inv , rhs [..., np .newaxis ])
31
+ intersection = np .all ((params > 0.0001 ) & (params < 0.9999 ), axis = (- 1 , - 2 ))
32
+ p0_s = params [intersection , 0 , :] * mat [intersection , :, 0 ]
33
+
34
+ xover_point = p0_s + p0 [np .where (intersection )[0 ]]
35
+
36
+ return {'intersection_ind' :[np .where (intersection )[0 ],np .where (intersection )[1 ]], 'intersection_points' :xover_point }
0 commit comments