1
$\begingroup$

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]

$\endgroup$
5
  • 1
    $\begingroup$ Copy your code into the question. $\endgroup$
    – ciao
    Jun 30, 2016 at 4:24
  • $\begingroup$ I just did, sorry it took a while to format $\endgroup$
    – 2012ssohn
    Jun 30, 2016 at 4:27
  • $\begingroup$ Are you aware of 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$
    – MarcoB
    Jun 30, 2016 at 5:08
  • $\begingroup$ Hmm, what a mess; try using the definition 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$ Jun 30, 2016 at 5:09
  • $\begingroup$ @MarcoB I'll look into the RegionIntersection function! I only recently started using Mathematica so I'm still pretty clueless about a lot of functions. $\endgroup$
    – 2012ssohn
    Jun 30, 2016 at 11:46

1 Answer 1

3
$\begingroup$

I stopped debugging after the first line.

The problem is you need p1 : {x1_, x2_} and you have p1_ : {x1_, x2_} where {x1_, x2_} is Optional for missing entry.

Which makes xi_ not associated with an input at all, they will appear only when the input is missing. Unless you fix your code you can only use pi references. But the very outer statement is If which can't verify the condition.

A minimal example:

f[x_: {x1_, x2_}] := {x, {x1, x2}}

f[{a, b}]
{{a, b}, {x1, x2}}
f[]
{{x1_, x2_}, {x1, x2}}

ClearAll[f]
f[x : {x1_, x2_}] := {x, {x1, x2}}

f[{a, b}]
{a, b}
f[]
f[]
$\endgroup$
3
  • $\begingroup$ I tried removing all the pi and li references to make SegInter[{{{x1_, y1_}, {x2_, y2_}}, {{x3_, y3_}, {x4_, y4_}}}], and now it seems to at least give True or False like I intended it to. Ofc it still needs to be fixed since it claims that (-1,0),(1,0) and (0,-1),(0,1) don't intersect, but that's probably because I implemented it incorrectly. Thank you! $\endgroup$
    – 2012ssohn
    Jun 30, 2016 at 11:50
  • $\begingroup$ @2012ssohn I'm glad I could help, p.s. consider using Which instead of nested Ifs. $\endgroup$
    – Kuba
    Jun 30, 2016 at 11:51
  • $\begingroup$ @2012, that was what I meant by "you still need to catch a few bugs" in my previous comment. ;) $\endgroup$ Jun 30, 2016 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.