# MATLAB: Finding equations of tangent lines to a circle

circlelinepointpoints of tangencytangenttangent points

Given the position vectors of a point A and the center of a circle B of radius r,
pA = [pAx; pAy];pB = [pBx; pBy];r  = 2;
Using this data alone, is it possible to find the position vectors of tangent points H and K drawn from point A to the circle B? I would then use this information to form equations of tangent lines AH and AK

WTP? When all else fails, apply Pythagoras. I'll be lazy, and just throw it into solve.
P1 = [22; 20];P2 = [31; 26];r = 2.5; syms xh yhQ = [xh;yh];% Q lies on the circle.E1 = r^2 - [1 1]*(P2 - Q).^2 == 0;% Pythagoras appliesE2 = [1 1]*(P1 - P2).^2 == r^2 + [1 1]*(Q - P1).^2res = solve(E1,E2);res.xhans = (5*443^(1/2))/78 + 1587/52 1587/52 - (5*443^(1/2))/78vpa(res.xh)ans = 31.868433665374947976737252727146 29.170027873086590484801208811316res.yhans = 2003/78 - (5*443^(1/2))/52 (5*443^(1/2))/52 + 2003/78vpa(res.yh)ans = 23.655682835270911368227454242615  27.70329152370344760613152011636
Two solutions, because there are two tangents. Surely you can do the rest.
Be careful when you plot things, because if the x and y axes do not use the same scale, the lines won't appear to be tangent. Of course then the circle won't appear to be circular either.
t = linspace(0,2*pi,100);plot([P1(1),P2(1)],[P1(2),P2(2)],'-ro')hold onaxis equalgrid onplot(P2(1) + r*cos(t),P2(2) + r*sin(t),'b-')plot(double(res.xh(1)),double(res.yh(1)),'go')plot(double(res.xh(2)),double(res.yh(2)),'go')
Looks right to me. So WTP?
Just for kicks, there is another approach we can try.
Given the two points P1 and P2, and a radius r, solve for the point Q=(x,y), such that the vectors formed from (Q-P1) and (Q-P2) are orthogonal, as well, we need the distance, norm(Q-P2) to be r.
That is, we have one equation
(Q-P1).'*(Q-P2) == 0
as well as
norm(Q-P2) == r
The latter can be written as
(Q-P2).'*(Q-P2) == r^2
Again, solve could be used here to solve the problem, and we should recover the same two solutions.
Hmm. How about a simple alternative solution that has no need for the symbolic toolbox? Again, we rely on the presence of a right triangle. But this time, we will use trig.
P1 = [22; 20];P2 = [31; 26];r = 2.5;
Assume the angle between the line connecting P1 and P2 and the tangent line is theta. Assume the tangent line touches the circle at point Q. Then {P1,Q,P2} forms a right triangle, with hypotenuse as the edge {P1,P2}. We get theta from the sine relation:
sin(theta) = r/norm(P2-P1)
Therefore
theta = asin(r/norm(P2-P1))
By default here, theta will be positive, but -theta will also suffice to get the other tangent line. So what is the angle of the tangent line? The two tangent lines fall at angles:
psi = atan2(P2(2)-P1(2),P2(1)-P1(1)) + [-1 1]*thetapsi =      0.35477      0.82124
How far along those tangent lines do they touch the circle? So what is the length of that segment?
L = cos(theta)*norm(P2-P1)L =       10.524
This makes perfect sense, since remember the distance between points P1 and P2 was
norm(P1-P2)ans =       10.817Q = [P1,P1] + L*[cos(psi);sin(psi)]Q =       31.868        29.17       23.656       27.703
Q has two columns, corresponding to the two tangent points. Happily, they are the same that we found using the symbolic toolbox. Note that this last expression can be made slightly simpler if we are using the latest (R2017) MATLAB release. I could then have written it as
Q = P1 + L*[cos(psi);sin(psi)]Q =       31.868        29.17       23.656       27.703