Here's the absolutely fastest way. It's about 20 times faster than my previous answer:
interQanton =
Compile[{{v1, _Real, 2}, {v2, _Real, 2}},
Module[{x1, x2, x3, x4, y1, y2, y3, y4, p1, p2, p3, p4, xx, yy, tst,
n1, n2, d1, d2, t1, t2, t3, t4, t5, w, t6, t7, xo, yo, xz, yz,
out, t8},
t2 = t3 = t4 = t5 = t6 = t7 = t8 = w = xz = yz = False;
xo = yo = 999.; xx = yy = 0.;
{p1, p2} = v1;
{p3, p4} = v2;
{x1, y1} = p1;
{x2, y2} = p2;
{x3, y3} = p3;
{x4, y4} = p4;
out = False;
If[p1 == p3 || p1 == p4, t8 = True; out = True;];
If[p2 == p3 || p2 == p4, t8 = True; out = True;];
If[! t8,
n1 = (x1 (x4 (-y2 + y3) + x3 (y2 - y4)) +
x2 (x4 (y1 - y3) + x3 (-y1 + y4)));
d1 = (-(x3 - x4) (y1 - y2) + (x1 - x2) (y3 - y4));
n2 = -((-x2 y1 + x1 y2) (y3 - y4) + (y1 - y2) (x4 y3 -
x3 y4));
d2 = ((x3 - x4) (y1 - y2) + (-x1 + x2) (y3 - y4));
If[Sign[Abs[d1] - 1*^-6] == -1, d1 = d2 = 1;
n1 = Min[{x1, x2}] - 1; w = True];
xx = n1/d1;
yy = n2/d2;
];
If[w,
If[(y3 - y1) != 0 && (y2 - y1) != 0,
t2 = ((x3 - x1)/(y3 - y1)) == ((x2 - x1)/(y2 - y1)); {xx,
yy} = {x3, y3};];
If[(y4 - y1) != 0 && (y2 - y1) != 0,
t3 = ((x4 - x1)/(y4 - y1)) == ((x2 - x1)/(y2 - y1)); {xx,
yy} = {x4, y4};];
If[(y1 - y3) != 0 && (y4 - y3) != 0,
t4 = ((x1 - x3)/(y1 - y3)) == ((x4 - x3)/(y4 - y3)); {xx,
yy} = {x1, y1};];
If[ (y2 - y3) != 0 && (y4 - y3) != 0,
t5 = ((x2 - x3)/(y2 - y3)) == ((x4 - x3)/(y4 - y3)); {xx,
yy} = {x2, y2};];
If[y1 == y2 == y3 == y4,
If[Min[{x1, x2}] <= x3 <= Max[{x1, x2}], xo = x3; xz = True;];
If[Min[{x1, x2}] <= x4 <= Max[{x1, x2}], xo = x4; xz = True;];
If[xz, t6 = True; {xx, yy} = {xo, y1};]];
If[x1 == x2 == x3 == x4,
If[Min[{y1, y2}] <= y3 <= Max[{y1, y2}], yo = y3; yz = True;];
If[Min[{y1, y2}] <= y4 <= Max[{y1, y2}], yo = y4; yz = True;];
If[yz, t7 = True; {xx, yy} = {x1, yo};]];
];
If[! t8,
t1 =
Min[{x1, x2}] <= xx <= Max[{x1, x2}] &&
Min[{y1, y2}] <= yy <= Max[{y1, y2}] &&
Min[{x3, x4}] <= xx <= Max[{x3, x4}] &&
Min[{y3, y4}] <= yy <= Max[{y3, y4}];
out = If[t1 || t2 || t3 || t4 || t5 || t6 || t7, True, False];
];
out
], RuntimeAttributes -> {Listable}, RuntimeOptions -> "Speed",
CompilationTarget -> "C"]
Test procedure is the same as above:
sgs = pool[[All, 1]];
sets = Subsets[Range@Length@sgs, {2}];
n8 = "Anton1";
{s1, s2} = {sets[[All, 1]], sets[[All, 2]]};
{t8, r8} = RepeatedTiming[interQanton[sgs[[s1]], sgs[[s2]]], tmax];
r0 == r1 == r2 == r3 == r4 == r5 == r6 == r7 == r8(*True*)
Transpose[{{n0, n1, n2, n3, n4, n5, n6, n7, n8}, {t0, t1, t2, t3, t4,
t5, t6, t7, t8}}] // Grid (*below*)
Results:
For larger segment sets this algo is also the fastest, I've checked with nlines = 900;
for example.
Line
objects in the plane (2D) only, or also in 3D or even nD? $\endgroup$