1
$\begingroup$

This is a continuation of the problem I posted earlier with the line-crossing algorithm that I tried to implement in Mathematica.

I took Kuba's suggestion, removing the underscore on everything except the xis and yis and also using Which to reduce the number of nested Ifs, but I seem to be back at square one in terms of the outputs that I get.

Here is my code:

Slope[{{x1_, y1_}, {x2_, y2_}}] := If[x1 == x2, Undefined, (y1 - y2)/(x1 - x2)]

ParallelVertical[{{p1 : {x1_, y1_}, p2 : {x2_, y2_}}, {p3 : {x3_, y3_}, p4 : {x4_, y4_}}}] := 
If[(Slope[p1, p2] == Undefined) && (Slope[p3, p4] == Undefined), True, False]

SegInter2[{{p1 : {x1_, y1_}, p2 : {x2_, y2_}}, {p3 : {x3_, y3_}, p4 : {x4_, y4_}}}] :=
(*Assume Min[x1,x2]<Min[x3,x4]*)
Which[Max[x1, x2] < Min[x3, x4](*No common x-coordinate*), False,
      ParallelVertical[{p1, p2}, {p3, p4}](*Both lines vertical*), False,
      Slope[p1, p2] == Undefined(*Line 1 vertical, line 2 not*), 
      If[(x3 - x1)*(x4 - x1) > 0(*p3&p4 on the same side of line 1*), False, True],
      Slope[p3, p4] == Undefined(*Line 2 vertical, line 1 not*), 
      If[(x1 - x3)*(x2 - x3) > 0(*p1&p2 on the same side of line 2*), False, True],
      True(*Otherwise*), 
      Module[{a1, a2}(*Define slopes a1&a2*),
      a1 = Slope[{p1, p2}]; 
      a2 = Slope[{p3, p4}]; 
      If[a1 == a2(*Parallel*), False, 
      Module[{b1, b2, xa}(*Define y-intercepts b1&b2 and x-coordinate of intersection 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]])(*Intersection point not within range*), False, True]
](*End of module b1&b2&xa*)
](*End of outer If statement*)
](*End of module a1&a2*)
]

I've also attached the output as a picture below. I apologize if this is a simple question, I'm still very new to Mathematica and I've tried following the model set by the official Wolfram documentation but I'm clearly lost...

enter image description here

$\endgroup$

closed as off-topic by Jason B., user9660, MarcoB, m_goldberg, Yves Klett Jul 6 '16 at 6:55

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Jason B., Community, MarcoB, m_goldberg, Yves Klett
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ You have defined Slope and ParallelVertical to take a single argument but you are calling them with two arguments. $\endgroup$ – Simon Woods Jun 30 '16 at 20:54
  • $\begingroup$ Also note that the result of any equation containing Undefined is Undefined. So for example Undefined == Undefined returns Undefined rather than True. $\endgroup$ – Simon Woods Jun 30 '16 at 21:03
  • $\begingroup$ @SimonWoods D'oh! You're right, I really need to start using fewer brackets in my code to avoid confusion. What should I set as the output for Slope when I'm trying to specifically avoid having to deal with the 'dividing-by-0' error? Would ComplexInfinity work? $\endgroup$ – 2012ssohn Jun 30 '16 at 21:10
  • $\begingroup$ You could use a string like "undefined". $\endgroup$ – Simon Woods Jun 30 '16 at 21:18
  • $\begingroup$ @SimonWoods I just tried it, but the result is still not looking good. imgur.com/5c3fhlY $\endgroup$ – 2012ssohn Jun 30 '16 at 21:23