1
$\begingroup$

today I was writing a code which takes some line equation and a circle equation,finds intersection points that are on $II$ and $III$ quadrants,draw a tangent line to circle from that point and then draws perpendicular lie to that tangent line that passes intersection point.

Here is the codes:

intersectionPoint[eqLine_, eqCircle_] := 
 Module[{eq1 = eqLine, eq2 = eqCircle, pts, x1, x2, a}, 
  pts = NSolve[eq1 && eq2, {x, y}];
  x1 = x /. pts[[1]];
  x2 = x /. pts[[2]];
  If[x1 < 0, a = pts[[1]], a = pts[[2]]];
  a];

tangentToCircle[x_, y_] :=
Module[{a = {Null, Null}},
    a[[1]] = -1*(x/y);
    a[[2]] = (x^2/y) + y;
    a
];

perpendicular[m_, x_, y_] :=
Module[{a = {Null, Null}},
    a[[1]] = (-1/m);
    a[[2]] = -1*a[[1]]*x + y;
    a
]; 

but i stopped developing when i found a problem.If a line with equation $y=0$ intersects circle,then I will get errors about division by zero in tangentToCircle function.also if a tangent line has slope of $0$ then the perpendicular function will give me similar errors.

So my questions are:

  1. How to change my way of handling lines?(currently i just save values $m$ and $b$ for a line in $y=mx+b$ form)

  2. How can I change a module to return different values?(for example if the slope was going to become undefined then return something )

UPDATE

here is case which I run into trouble

In[130]:= b = intersectionPoint[y == 0*x + 5, x^2 + y^2 == 25]

Out[130]= {x -> 0., y -> 5.}

In[131]:= c = tangentToCircle[x /. b, y /. b]

Out[131]= {0., 5.}

In[132]:= d = perpendicular[c[[1]], x /. b, y /. b]

During evaluation of In[132]:= Power::infy: Infinite expression 1/0. encountered. >>

During evaluation of In[132]:= Infinity::indet: Indeterminate expression 0.               ComplexInfinity encountered. >>

 Out[132]= {ComplexInfinity, Indeterminate}
$\endgroup$
  • $\begingroup$ Please post a use case $\endgroup$ – Dr. belisarius Oct 30 '14 at 15:23
  • $\begingroup$ @belisarius,you mean post a code to show how i handle and how i get the error? $\endgroup$ – user2838619 Oct 30 '14 at 15:25
  • $\begingroup$ Just show how do you call your functions and how to get error, yes $\endgroup$ – Dr. belisarius Oct 30 '14 at 15:27
  • $\begingroup$ @belisarius,I added an example. $\endgroup$ – user2838619 Oct 30 '14 at 15:31
2
$\begingroup$

As you're trying to represent things that aren't functions, I suggest to change your representation to vectors (affine):

(* a few functions *)
line[{v_List, d_List}, a_] := a v + d
circle[{c_List, r_}, t_] := c + r {Cos@t, Sin@t}
intersection[{v_List, d_List}, {c_List, r_}] := 
                                 Solve[line[{v, d}, a] == circle[{c, r}, t], {t, a}] /. C[1] -> 1
tangent[{c_List, r_}, u_] := {{-Sin@u, Cos@u}, circle[{c, r}, u]}
perp[{v_List, d_List}, a_] := RotationMatrix[Pi/2].v a + d

(*now your problem*)

{v, d} = {{1, 1}, -{1, 0} 5}; (* some appropriate line *)
{c, r} = {{0, 0}, 5};         (* a circle *)
int = intersection[{v, d}, {c, r}];

Show[
 ParametricPlot[line[{v, d}, t], {t, -1, 6}],
 ParametricPlot[circle[{c, r}, t], {t, 0, 2 Pi}],
 ParametricPlot[line[tangent[{c, r}, t /. #], u] & /@ int, {u, -3, 3}, PlotStyle -> Red],
 ParametricPlot[perp[tangent[{c, r}, t /. #], u] & /@ int, {u, -3, 3}, PlotStyle -> {Thickness[.01], Green}],
 PlotRange -> All]

Mathematica graphics

$\endgroup$
  • $\begingroup$ sorry for pinging you but how can i find angle between blue line and and green line?is there any formula or something? $\endgroup$ – user2838619 Nov 21 '14 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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