Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
tangent to circles curve fitting

Subject: tangent to circles curve fitting

From: kentavros babis

Date: 9 Mar, 2009 14:52:02

Message: 1 of 10

I have a series of consecutive circles with characteristics ([x,y],radius) for example circle1 ([0,0],0.1)
circle2 ([1.2,1],0.25)
circle3 ([2.3,2],0.7)
circle4 ([3.4,3],1.2)
circle5 ([4.5,4],1.8)
circle6 ([5.6,5],2.4)
Is it possible to create two curves that are tangent to all of this circles.
One above the circles and one below. If it is what should I do to create it?
Thanks in advance.

Subject: tangent to circles curve fitting

From: Roger Stafford

Date: 11 Mar, 2009 03:40:21

Message: 2 of 10

"kentavros babis" <akentavros@yahoo.com> wrote in message <gp3aei$l81$1@fred.mathworks.com>...
> I have a series of consecutive circles with characteristics ([x,y],radius) for example circle1 ([0,0],0.1)
> circle2 ([1.2,1],0.25)
> circle3 ([2.3,2],0.7)
> circle4 ([3.4,3],1.2)
> circle5 ([4.5,4],1.8)
> circle6 ([5.6,5],2.4)
> Is it possible to create two curves that are tangent to all of this circles.
> One above the circles and one below. If it is what should I do to create it?
> Thanks in advance.

  If I were doing that task carefully I would use three steps. The first would be to determine for each successive pair of circles, that line which is tangent to both circles and its two points of tangency. The second would be to find for each circle, except the end two, the half-way point on that circle between the two tangency points on it that were found in step one. For the two end circles the single point of tangency would have to suffice. The third task would be to construct a curve that runs through all these "midpoints" determined in step 2 with slopes equal to the slope of the corresponding circle at that point.

  Below are pieces of code that can be used in carrying out the first and second steps.

  Let [p1,q1] and [p2,q2] be two successive circle centers with r1 and r2 their respective radii. An "upper" line tangent to both circles will be tangent at the two points [x1,y1] and [x2,y2] of the respective circles as calculated by:

 d2 = (p2-p1)^2+(q2-q1)^2;
 r = sqrt(d2-(r2-r1)^2);
 s = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2;
 c = ((p2-p1)*r-(q2-q1)*(r2-r1))/d2;
 x1 = p1-r1*s;
 y1 = q1+r1*c;
 x2 = p2-r2*s;
 y2 = q2+r2*c;

This is more properly described as the line to the left as you travel from circle 1 to circle 2. The line to the right would be obtained by replacing r1 and r2 by reversing their signs to negative values in the same formulae.

  For step two, let P1 = [x1,y1] and P2 = [x2,y2] be two points of tangency on a given circle of radius r and center C = [p,q]. Then you can get the circular midpoint P = [x,y] lying on the circle with:

 P = (P1+P2)/2-C;
 P = C + P/norm(P);

and a line tangent to the circle at this point would be parallel to the vector P2-P1.

  For step three you need to use an interpolation method in which both the location and the slope values are prescribed for a discrete set of non-uniformly-spaced points. I leave that task to you.

Roger Stafford

Subject: tangent to circles curve fitting

From: Matt J

Date: 2 Aug, 2010 15:25:31

Message: 3 of 10

"kentavros babis" <akentavros@yahoo.com> wrote in message <gp3aei$l81$1@fred.mathworks.com>...

> Is it possible to create two curves that are tangent to all of this circles.
==================

Two curves meaning two lines? If so, see here for a very related thread.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/282681#764396

Subject: tangent to circles curve fitting

From: Jason Pearson

Date: 5 Aug, 2010 05:09:04

Message: 4 of 10

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 = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2;
c = ((p2-p1)*r-(q2-q1)*(r2-r1))/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 (q2-q1), and the length of the vertical would be (p2-p1). And for the other triangle, the length of one leg would be r, and the length of the other leg would be (r2-r1).

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 twenty-five years since I actively used my geometry/trig skills, and I've forgotten a lot...

Best wishes,

Jason Pearson

> Here is an explanation of the quantities I used in step one as per your request. Call the centers of the two circles C1 =(p1,q1) and C2 = (p2,q2) and the two corresponding points of tangency T1 = (x1,y1) and T2 = (x2,y2). Then d2 is the square of the distance between the centers, while r is the distance between T1 and T2. You can see this latter by sliding the line segment connecting T1 and T2 to a parallel line segment running through C1 and apply pythagoras theorem. The quantity s is the sine of the angle measured counterclockwise from the x-axis to the above line segment (parallel to line T1T2) while c is the cosine of that angle. This angle is the sum of the angle from the x-axis to the line C1C2 and the angle from C1C2 to the above segment. Then x1, y1, x2, and y2 are obtained in the obvious way from that.
>
> Let me know if this explanation is too brief and I'll go into more detail.
>
> Roger Stafford

Subject: tangent to circles curve fitting

From: Roger Stafford

Date: 5 Aug, 2010 07:08:05

Message: 5 of 10

"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 = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2;
> c = ((p2-p1)*r-(q2-q1)*(r2-r1))/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 (q2-q1), and the length of the vertical would be (p2-p1). And for the other triangle, the length of one leg would be r, and the length of the other leg would be (r2-r1).
>
> 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 twenty-five 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 hand-waved 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 r2-r1, then C1U must be of length r = sqrt(d^2-(r2-r1)^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 = (q2-q1)/d
 cos(alpha) = C1V/C1C2 = (p2-p1)/d
 sin(beta) = C2U/C1C2 = (r2-r1)/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)
  = (q2-q1)/d*r/d + (p2-p1)/d*(r2-r1)/d = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2
c = cos(alpha+beta) = cos(alpha)*cos(beta) - sin(alpha)*sin(beta)
  = (p2-p1)/d*r/d - (q2-q1)/d*(r2-r1)/d = ((p2-p1)*r-(q2-q1)*(r2-r1))/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

Subject: tangent to circles curve fitting

From: Jason Pearson

Date: 5 Aug, 2010 14:01:20

Message: 6 of 10

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 = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2;
> > c = ((p2-p1)*r-(q2-q1)*(r2-r1))/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 (q2-q1), and the length of the vertical would be (p2-p1). And for the other triangle, the length of one leg would be r, and the length of the other leg would be (r2-r1).
> >
> > 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 twenty-five 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 hand-waved 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 r2-r1, then C1U must be of length r = sqrt(d^2-(r2-r1)^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 = (q2-q1)/d
> cos(alpha) = C1V/C1C2 = (p2-p1)/d
> sin(beta) = C2U/C1C2 = (r2-r1)/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)
> = (q2-q1)/d*r/d + (p2-p1)/d*(r2-r1)/d = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2
> c = cos(alpha+beta) = cos(alpha)*cos(beta) - sin(alpha)*sin(beta)
> = (p2-p1)/d*r/d - (q2-q1)/d*(r2-r1)/d = ((p2-p1)*r-(q2-q1)*(r2-r1))/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

Subject: tangent to circles curve fitting

From: Jason Pearson

Date: 8 Aug, 2010 05:03:04

Message: 7 of 10

Roger,
I have completed the first phase of code development for the Sankey flow creator, using your excellent advice on tangents as a basis. I'm quite pleased with the results.
If you would like me to send you a ZIP of the resulting Java applet, just let me know. I tried to decipher your email address, but it gave me an error. Feel free to email me on mine - just remove the xy stuff and replace the domain suffix with the obvious.
Best,
Jason Pearson

Subject: tangent to circles curve fitting

From: Camp Camp

Date: 22 Dec, 2010 22:22:05

Message: 8 of 10

Hi guys,
I'm using this question as cue for my own...
i have also a series of consecutive circles, defined by someone the center and radius. But this must follow the rule thats is: the second circle has to have radius greater than first, and third has to have radius greater than second. I know that this could be a system with multiple solutions, but is there any possibilities to have a tangent to those 3 circles? Something like a fit linear.

thanks in advance!



"kentavros babis" wrote in message <gp3aei$l81$1@fred.mathworks.com>...
> I have a series of consecutive circles with characteristics ([x,y],radius) for example circle1 ([0,0],0.1)
> circle2 ([1.2,1],0.25)
> circle3 ([2.3,2],0.7)
> circle4 ([3.4,3],1.2)
> circle5 ([4.5,4],1.8)
> circle6 ([5.6,5],2.4)
> Is it possible to create two curves that are tangent to all of this circles.
> One above the circles and one below. If it is what should I do to create it?
> Thanks in advance.

Subject: tangent to circles curve fitting

From: John D'Errico

Date: 22 Dec, 2010 23:12:04

Message: 9 of 10

"Camp Camp" wrote in message <iettmd$9k4$1@fred.mathworks.com>...
> Hi guys,
> I'm using this question as cue for my own...
> i have also a series of consecutive circles, defined by someone the center and radius. But this must follow the rule thats is: the second circle has to have radius greater than first, and third has to have radius greater than second. I know that this could be a system with multiple solutions, but is there any possibilities to have a tangent to those 3 circles? Something like a fit linear.
>

So what stops you from finding the lines that are tangent
to consecutive circles? Since you tell us that already KNOW
the radii for these circles, then just check to see if the lines
are collinear. If you don't know the radii of the circles,
then the problem is trivial.

What is the problem here?

John

Subject: tangent to circles curve fitting

From: Camp Camp

Date: 23 Dec, 2010 09:26:04

Message: 10 of 10

Well,
im having problems when those points are not collinear. Probably ive wrote it wrong.
For example,

%Points and radius
%b1 = [p1,q1] r1;
%b2 = [p2,q2] r2;
%b3 = [p3,q3] r3;

p1 = 1;
p2 = 4;
p3 = 10;
q1 = 0;
q2 = 0;
q3 = 0;
r1 = 0.5;
r2 = 2;
r3 = 3;

%Get tangent for consecutive circles
d2 = (p2-p1)^2+(q2-q1)^2;
r = sqrt(d2-(r2-r1)^2);
s = ((q2-q1)*r+(p2-p1)*(r2-r1))/d2;
c = ((p2-p1)*r-(q2-q1)*(r2-r1))/d2;
x1 = p1-r1*s;
y1 = q1+r1*c;
x2 = p2-r2*s;
y2 = q2+r2*c;

d2a = (p3-p2)^2+(q3-q2)^2;
ra = sqrt(d2a-(r3-r2)^2);
sa = ((q3-q2)*ra+(p3-p2)*(r3-r2))/d2a;
ca = ((p3-p2)*ra-(q3-q2)*(r3-r2))/d2a;
x2a = p2-r2*sa;
y2a = q2+r2*ca;
x3 = p3-r3*sa;
y3 = q3+r3*ca;

%Draw the circle
THETA=linspace(0,pi,100);
RHO=ones(1,100)*r1;
[X,Y] = pol2cart(THETA,RHO);
X=X+p1;
Y=Y+q1;
H=plot(X,Y,'r-');
axis square;

hold on

THETA=linspace(0,pi,100);
RHO=ones(1,100)*r2;
[X,Y] = pol2cart(THETA,RHO);
X=X+p2;
Y=Y+q2;
H=plot(X,Y,'g-');
axis square;

hold on

THETA=linspace(0,pi,100);
RHO=ones(1,100)*r3;
[X,Y] = pol2cart(THETA,RHO);
X=X+p3;
Y=Y+q3;
H=plot(X,Y,'g-');
axis square;

hold on

%XT = [x1 x2];
%YT = [y1 y2];
%line(XT,YT);


%Draw the tangent
Y = 0:0.1:10;
Q = [x1 1; x2 1];
E = [y1;y2];
T = Q\E;
Z = T(1)*Y + T(2);

plot(Y,Z);

hold on

Y1 = 0:0.1:10;
Q1 = [x2a 1; x3 1];
E1 = [y2a;y3];
T1 = Q1\E1;
Z1 = T1(1)*Y1 + T1(2);

plot(Y1,Z1);

hold on

%Plot all the curves
axis equal;
axis([0 12 0 5]);
grid on
plot(l1,l2,Z,Z1,'g.-');
 
thanks in advance

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ieu0k4$itm$1@fred.mathworks.com>...
> "Camp Camp" wrote in message <iettmd$9k4$1@fred.mathworks.com>...
> > Hi guys,
> > I'm using this question as cue for my own...
> > i have also a series of consecutive circles, defined by someone the center and radius. But this must follow the rule thats is: the second circle has to have radius greater than first, and third has to have radius greater than second. I know that this could be a system with multiple solutions, but is there any possibilities to have a tangent to those 3 circles? Something like a fit linear.
> >
>
> So what stops you from finding the lines that are tangent
> to consecutive circles? Since you tell us that already KNOW
> the radii for these circles, then just check to see if the lines
> are collinear. If you don't know the radii of the circles,
> then the problem is trivial.
>
> What is the problem here?
>
> John

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us