I'm trying to implement the line-crossing algorithm on Mathematica and I seem to be having trouble getting it to work. More specifically, I would like to know what is causing the function to not evaluate, and instead reproduce the code for the function as the output like in Out[142].
In case you would like to try the code on your own Mathematica to further analyze the issue, here's the code:
SegInter[{l1_: {p1_: {x1_, y1_}, p2_: {x2_, y2_}}, l2_: {p3_: {x3_, y3_},
p4_: {x4_, y4_}}}] :=
(*Assume that Min[x1,x2]<Min[x3,x4]*)
If[Max[x1, x2] < Min[x3, x4],
If[(x1 == x2) && (x3 == x4),
If[x1 == x2,
(*Line 1 is vertical but not line 2*) If[(x3 - x1)*(x4 - x1) > 0, False, True],
If[x3 == x4,
(*Line 2 is vertical but not line 1*) If[(x1 - x3)*(x2 - x3) > 0, False, True],
(*Neither line is vertical*) Module[{a1, a2},
a1 = (y1 - y2)/(x1 - x2);
a2 = (y3 - y4)/(x3 - x4);
If[a1 == a2,(*Parallel*)False, Module[{b1, b2, xa},
b1 = y1 - a1*x1;
b2 = y3 - a2*x3;
xa = (b2 - b1)/(a1 - a2);
If[(xa < Max[Min[x1, x2], Min[x3, x4]]) || (xa > Min[Max[x1, x2], Max[x3, x4]]), False, True]
]
]
]
]
],(*Parallel vertical*)False], False]
RegionIntersection
and the other geometric calculation functions? If you just need the functionality, that may be a more direct route. Of course, there's nothing wrong in reimplementing the algorithm though. $\endgroup$SegInter[{{x1_, y1_}, {x2_, y2_}}, {{x3_, y3_}, {x4_, y4_}}] := (* stuff *)
instead... but you still have a few bugs to catch. BTW: you might want to look at this Graphics Gems entry. $\endgroup$RegionIntersection
function! I only recently started using Mathematica so I'm still pretty clueless about a lot of functions. $\endgroup$