PDA

View Full Version : Connecting two circles



Rambo
02-28-2008, 04:00 AM
Hi, Im writing a program in VBA excel that displays two circles in excel spreadsheet, these two circles have been set X and Y Co - ordinates. Using the X and Y Co - ordinates i want to connect a line from shape 1 to shape 2

i found this code that might help:


ActiveSheet.Shapes.AddLine(495.75, 234#, 682.5, 234#).Select

mikerickson
02-28-2008, 05:11 AM
You found code that helps.
Are you having a problem implimenting that code?
The line you want to draw, is it tangent to the circles or does it pass through the centers or....?

Rambo
02-28-2008, 05:17 AM
You found code that helps.
Are you having a problem implimenting that code?
The line you want to draw, is it tangent to the circles or does it pass through the centers or....?


its tangent, i just want it to connect the circles

mikerickson
02-28-2008, 05:45 AM
If you have two non-intersecting circles,
one circle centered at (C1,0) with radius R1 and
the other centered at (C2,0) with radius R2.
And C1 > C2

Let Th = ArcSin( (R1+R2) / (C1-C2) )

Then, the line segment with endpoints

(C1,0) + R1*(-Cos(Th), Sin(Th))

(C2,0) - R2*(-Cos(Th), Sin(Th))

is tangent to both circles.

(These are subscripts, NOT cell locations)

mikerickson
02-28-2008, 10:11 PM
Consider the situation where the circle's centers are at

(C1x, C1y) and (0, 0) . Let d1 = Sqrt( C1x^2 + C1y^2 ), the distance between those two points. We will also be using a1 = arctan(C1x/C1y)
There is an equivilent coordinate system (denoted by underline) where those two points are

(d1, 0) and (0, 0).

The conversion between those two systems is:

(x, y) = (x*cos(a)-y*sin(a), x*sin(a)+y*cos(a))
and
(x, y) =(x*cos(a)+y*sin(a), x*sin(a)-y*cos(a))

Applying that back to the two circles,

(C1x, C1x) (0,0) transforms to (d1,0) (0,0)
substituting into the previous post, the end points of the tangent line are

(d1,0) + R1*(-Cos(Th), Sin(Th)) = (d1-R1*cos(Th), R1*sin(Th))

(0,0) - R2*(-Cos(Th), Sin(Th)) = (R2*cos(Th), -R2*sin(Th))

where R1 & R2 are the radii and Th = ArcSin( (R1+R2) / (d2) )

Transfering back to the un-underlined coordinate system gives the result:
----
If you have two circles, one centered at (C1x, C1y) with radius R1 and the other centered at (0,0) with radius R2, the endpoints of the tangent line between them are:

(d1-R1*cos(Th))*cos(a) - R1*sin(Th)*sin(a) , (d1-R1*cos(Th))*sin(a) + R1*sin(Th)*cos(a)

R2*cos(Th)*cos(a) + R2*sin(Th)*sin(a) , R2*cos(Th)*sin(a) - R2*sin(Th)*cos(a)

where d2 = Sqrt(C1x^2 + C2x^2)
Th = ASin( (R1+R2) / d1 )
and a = arctan(C1x/C1y)

mikerickson
03-01-2008, 10:03 AM
I think the attached file might help.

It draws any of the four line that are tangent to two circles.

Note: A1:B16 are data output, not input. Size and location of the circles is changed with the mouse.

I hope this helps.