# handling division by zeros and line equations

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[];
x2 = x /. pts[];
If[x1 < 0, a = pts[], a = pts[]];
a];

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

perpendicular[m_, x_, y_] :=
Module[{a = {Null, Null}},
a[] = (-1/m);
a[] = -1*a[]*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:= b = intersectionPoint[y == 0*x + 5, x^2 + y^2 == 25]

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

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

Out= {0., 5.}

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

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

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

Out= {ComplexInfinity, Indeterminate}

• Please post a use case – Dr. belisarius Oct 30 '14 at 15:23
• @belisarius,you mean post a code to show how i handle and how i get the error? – user2838619 Oct 30 '14 at 15:25
• Just show how do you call your functions and how to get error, yes – Dr. belisarius Oct 30 '14 at 15:27
• @belisarius,I added an example. – user2838619 Oct 30 '14 at 15:31

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
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

{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] • sorry for pinging you but how can i find angle between blue line and and green line?is there any formula or something? – user2838619 Nov 21 '14 at 7:10