Roger,
Thank you for this wonderfully clear response. This makes perfect sense, and it does, indeed, equip me with the necessary tools to experiment with code variations to produce the different tangents that I want. I'm using these calculations to correctly locate the curved portions of the shapes for a dynamic Sankey flow diagram (en.wikipedia.org/wiki/Sankey_diagram), which I am initially coding in Processing (www.processing.org) and then exporting as a Java applet. If it works out as I hope, I'll send you a copy or link to see the final result.
Thank you again!
Best,
Jason Pearson
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i3do0l$8pk$1@fred.mathworks.com>...
> "Jason Pearson" <jason.c.pearson.xyxy@gmail.replace> wrote in message <i3dh1g$i8g$1@fred.mathworks.com>...
> > Hi Roger,
> > Thanks very much for taking the time to explain this in greater detail. Perhaps I'm missing something obvious, but how did you derive the algebraic formulas for the values of s and c? You show these formulas as:
> >
> > s = ((q2q1)*r+(p2p1)*(r2r1))/d2;
> > c = ((p2p1)*r(q2q1)*(r2r1))/d2;
> >
> > I have drawn out the diagram as you suggest, sliding the tangent down until it intersects with C1. I have also drawn a horizontal line from C1 across to above/below C2, and a vertical line from the end of that horizontal to C2. This gives me two right triangles which share a hypotenuse (d).
> >
> > From your formulas, I am assuming that these triangles somehow figure into your calculations, since for one triangle, the length of the horizontal would be (q2q1), and the length of the vertical would be (p2p1). And for the other triangle, the length of one leg would be r, and the length of the other leg would be (r2r1).
> >
> > Since these are the basic components of your formulas, I am assuming that there is some geometrical law that I am forgetting or never learned that says that, for two right triangles that share a hypotenuse (h), and whose legs are of length L1,W1,L2,W2, if the combined angle that they form is a, then sin(a) = ((H2*L1)+(H1*L2))/(h^2). And that cos(a) = ((H2*L1)(H1*L2))/(h^2).
> >
> > Am I inferring correctly that these are the geometrical laws upon which you are drawing? If so, do these laws/theorems have a specific name that I could Google so I can better understand why they are true (and how to tweak them for, say, an internal tangent)?
> >
> > Thank you for any help that you could provide. You humbly pleaded age for any errors, and I will plead extreme rustiness in return! It has been over twentyfive years since I actively used my geometry/trig skills, and I've forgotten a lot...
> >
> > Best wishes,
> >
> > Jason Pearson
>           
> I was afraid I might have handwaved over that stuff too fast. I think you have the two right triangles I had in mind, but I'll start from the beginning.
>
> Assume for our purposes that r2 > r1 and that the center C2 is above and to the right of C1. Assume further that we are talking about the tangent line being to the left of the two circles. Then draw the line through C1 parallel to the T1T2 line and call point U where it intersects with line C2T2. Finally drop a line vertically down from C2 until it intersects a horizontal line from C1 and call that intersection V. Then, as you said, triangles C1UC2 and C1VC2 are right triangles that share a common hypotenuse in segment C1C2 which is of length d = sqrt(d2) (in the code.) Since C2U is of length r2r1, then C1U must be of length r = sqrt(d^2(r2r1)^2), (again as in the code.)
>
> Call angle VC1C2 alpha and call angle C2C1U beta. Because these are right triangles we have
>
> sin(alpha) = C2V/C1C2 = (q2q1)/d
> cos(alpha) = C1V/C1C2 = (p2p1)/d
> sin(beta) = C2U/C1C2 = (r2r1)/d
> cos(beta) = C1U/C1C2 = r/d
>
> Recall that I said that s and c are to be the sine and cosine respectively of the sum of these two angles alpha and beta, so using the trig summation formulas we have
>
> s = sin(alpha+beta) = sin(alpha)*cos(beta) + cos(alpha)*sin(beta)
> = (q2q1)/d*r/d + (p2p1)/d*(r2r1)/d = ((q2q1)*r+(p2p1)*(r2r1))/d2
> c = cos(alpha+beta) = cos(alpha)*cos(beta)  sin(alpha)*sin(beta)
> = (p2p1)/d*r/d  (q2q1)/d*(r2r1)/d = ((p2p1)*r(q2q1)*(r2r1))/d2
>
> which are carried out in the code.
>
> Finally for angle VC1T1 = alpha + beta + pi/2 we have
>
> x1 = p1 + r1*cos(alpha+beta+pi/2)
> = p1  r1*sin(alpha+beta) = p1  r1*s
> y1 = q1 + r1*sin(alpha+beta+pi/2)
> = q1 + r1*cos(alpha+beta) = q1 + r1*c
> x2 = p2 + r2*cos(alpha+beta+pi/2) = p2  r2*s
> y2 = q2 + r2*sin(alpha+beta+pi/2) = q2 + r2*c
>
> Of course a rigorous proof of this would take into consideration other possible orientations of the circles and having r2 < r1. It's a lot of additional work but it all comes out fine. Some of the angles would then be negative but the same formulas still give the proper answer. For our purposes here it should probably suffice to generate random circles in all possible orientations and relative sizes, and check the results with appropriate plots, which is what I did for a check back in March of 2009 when I wrote those formulas.
>
> As you guessed earlier in this thread, it is possible to make small changes in these formulas to get the other three possible tangents to the circles: the outside one to the right, the one that is to the left of C1 and to the right of C2, and the one that is to the right of C1 and left of C2. However, I'll confess I never did bother to work them out. I encourage you to see if you can do it yourself after being armed with all the above techniques. If you do, be sure to write plotting routines that test them thoroughly in all possible orientations if you want to make serious use of them. It will also be good practice for writing matlab code in general.
>
> Roger Stafford
