diff --git a/ntrfc/utils/math/vectorcalc.py b/ntrfc/utils/math/vectorcalc.py index aa65601347a64811c0869c76ce534fda2cdfb625..48a7e2a10ed4dafe343866fd315f4704a42425db 100644 --- a/ntrfc/utils/math/vectorcalc.py +++ b/ntrfc/utils/math/vectorcalc.py @@ -11,7 +11,7 @@ from scipy.spatial.distance import squareform, pdist import sys import math as m -import scipy + def calc_largedistant_idx(x_koords, y_koords): """ @@ -231,3 +231,20 @@ def lineseg_dist(p, a, b): return np.hypot(h, np.linalg.norm(c)) +def line_intersection(point_a1, point_a2, + point_b1, point_b2): + def det_2d(a, b): + return a[0] * b[1] - a[1] * b[0] + + xdiff = (point_a1[0] - point_a2[0], point_b1[0] - point_b2[0]) + ydiff = (point_a1[1] - point_a2[1], point_b1[1] - point_b2[1]) + + div = det_2d(xdiff, ydiff) + if div == 0: + return None + + d = (det_2d(point_a1, point_a2), det_2d(point_b1, point_b2)) + x = det_2d(d, xdiff) / div + y = det_2d(d, ydiff) / div + return x, y + diff --git a/tests/test_ntrfc_math.py b/tests/test_ntrfc_math.py index 23b1043ed9f3e9eb471ef466d5f9e3b5b3e5b5f9..ae0688fd9f13d1024c9af1719849670371298c80 100644 --- a/tests/test_ntrfc_math.py +++ b/tests/test_ntrfc_math.py @@ -166,3 +166,12 @@ def test_vecAngle(): b = np.array([0, 1, 0]) angle = vecAngle(a, b) assert angle == np.pi / 2 + + +def test_line_intersection(): + import numpy as np + from ntrfc.utils.math.vectorcalc import line_intersection + + intersect = line_intersection((-1,0),(1,0), + (0,-1),(0,1)) + assert all(intersect==np.array([0,0]))